Skip to content

Commit

Permalink
Merge pull request #37 from jeremyfelt/fix/list-items
Browse files Browse the repository at this point in the history
Improve handling of lists and list items when transforming for Mastodon
  • Loading branch information
jeremyfelt authored Jul 15, 2023
2 parents e735c14 + d6045ff commit 338380c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
11 changes: 11 additions & 0 deletions includes/post-type-note.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ function filter_allowed_block_types( $allowed_block_types, \WP_Post $post ) {
'core/gallery',
'core/image',
'core/list',
'core/list-item',
'core/paragraph',
'core/preformatted',
'core/pullquote',
Expand Down Expand Up @@ -487,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 338380c

Please sign in to comment.