Skip to content

Commit

Permalink
fix: handle rendering of list in post experpt for excerpt block
Browse files Browse the repository at this point in the history
  • Loading branch information
dhananjaykuber committed Jan 28, 2025
1 parent 2abe6fa commit b55468b
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions packages/block-library/src/post-excerpt/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,33 @@ function render_block_core_post_excerpt( $attributes, $content, $block ) {
return '';
}

$post = get_post( $block->context['postId'] );

/*
* The purpose of the excerpt length setting is to limit the length of both
* automatically generated and user-created excerpts.
* Because the excerpt_length filter only applies to auto generated excerpts,
* wp_trim_words is used instead.
*/
$excerpt_length = $attributes['excerptLength'];
$excerpt = get_the_excerpt( $block->context['postId'] );
if ( isset( $excerpt_length ) ) {
$excerpt = wp_trim_words( $excerpt, $excerpt_length );
if ( ! $post->post_excerpt ) {
$content = $post->post_content;

// Convert list items to text with proper spacing
$content = preg_replace( '/<li[^>]*>(.*?)<\/li>/i', "• $1\n", $content );

// Strip remaining HTML tags
$content = wp_strip_all_tags( $content );

// Clean up whitespace
$content = preg_replace( '/\s+/', ' ', trim( $content ) );

// Create excerpt
$excerpt = wp_trim_words( $content, $attributes['excerptLength'] );
} else {
$excerpt = get_the_excerpt( $block->context['postId'] );
if ( isset( $attributes['excerptLength'] ) ) {
$excerpt = wp_trim_words( $excerpt, $attributes['excerptLength'] );
}
}

$more_text = ! empty( $attributes['moreText'] ) ? '<a class="wp-block-post-excerpt__more-link" href="' . esc_url( get_the_permalink( $block->context['postId'] ) ) . '">' . wp_kses_post( $attributes['moreText'] ) . '</a>' : '';
Expand Down

0 comments on commit b55468b

Please sign in to comment.