Skip to content

Commit

Permalink
Improve list display when content is transformed for Mastodon
Browse files Browse the repository at this point in the history
Fixes #33
  • Loading branch information
jeremyfelt committed Jul 15, 2023
1 parent f97acc5 commit d6045ff
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
10 changes: 10 additions & 0 deletions includes/post-type-note.php
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,16 @@ function transform_block( array $block ): string {
trim( $block['innerHTML'] )
)
);
} elseif ( 'core/list' === $block['blockName'] ) {
$list_content = '';

foreach ( $block['innerBlocks'] as $inner_block ) {
$list_content .= transform_block( $inner_block );
}

$content .= rtrim( $list_content, "\n" );
} elseif ( 'core/list-item' === $block['blockName'] ) {
$content .= '- ' . strip_html( trim( $block['innerHTML'] ) ) . "\n";
} else {
$content .= strip_html( trim( $block['innerHTML'] ) );
}
Expand Down
36 changes: 36 additions & 0 deletions tests/test-post-type-note.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,42 @@ public function test_convert_preformatted_text() {
$this->assertEquals( $expected_text, $transformed_text );
}

/**
* A list block should render as text with a dash and a space at the start of each
* list item.
*/
public function test_convert_list_block() {
ob_start();
?>
<!-- wp:paragraph -->
<p>This is a list that should be preserved on Mastodon:</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>Test item one</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Test item two</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Test item three</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>A dash and a space should be added to the front of each line.</p>
<!-- /wp:paragraph -->
<?php
$pre_html = ob_get_clean();
$expected_text = "This is a list that should be preserved on Mastodon:\n\n- Test item one\n- Test item two\n- Test item three\n\nA dash and a space should be added to the front of each line.";
$transformed_text = Note\transform_content( $pre_html );

$this->assertEquals( $expected_text, $transformed_text );
}

/**
* A single quote block containing a verse block and a citation should
* render as text surrounded with curly quotes, and the link breaks from
Expand Down

0 comments on commit d6045ff

Please sign in to comment.