Skip to content

Commit

Permalink
trim to periods if they exist in the title
Browse files Browse the repository at this point in the history
  • Loading branch information
roo2 committed Dec 18, 2022
1 parent aed7ad0 commit f9a12d0
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions packages/block-library/src/rss/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,30 @@ function trim_to_word_boundary( $to_trim, $character_count, $more = ' […]
return $to_trim;
}

preg_match( '/^(.{0,' . $character_count . '})\s/', $to_trim, $parts );
return $parts[1] . $more;
preg_match( '/^.{0,' . $character_count . '}\s/', $to_trim, $parts );
return $parts[0] . $more;
}

/**
* Helper funtion to trim text up to the first period (.) followed by a space.
* If no period is found, the text is trimmed to the nearest word boundary.
* If multiple periods are found, the last one within $title_length is used.
*
* @param string $to_trim The text to trim.
* @param int $title_length The maximum number of characters to return.
* @param string $more The string to append to the trimmed text.
*/
function trim_title_from_excerpt( $to_trim, $title_length, $more = ' […]' ) {
$to_trim = trim_to_word_boundary($to_trim, $title_length, $more );

// Match to the first period followed by a space.
preg_match( '/^.{0,' . $title_length . '}\.\s/', $to_trim, $parts );

if ( empty( $parts[0] ) ) {
return $to_trim;
}

return $parts[0] . $more;
}

/**
Expand Down Expand Up @@ -57,7 +79,7 @@ function render_block_core_rss( $attributes ) {

if ( empty( $title ) ) {
// If the title is empty, use the begining of the excerpt instead.
$title = trim_to_word_boundary( $excerpt_trimmed, $attributes['titleLength'], '' );
$title = trim_title_from_excerpt( $excerpt_trimmed, $attributes['titleLength'], '' );
if ( $attributes['displayExcerpt'] ) {
// Trim out the words that were used for the title.
$excerpt_trimmed = trim( substr( $excerpt_trimmed, strlen( $title ) ) );
Expand Down

1 comment on commit f9a12d0

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/3727063905
📝 Reported issues:

Please sign in to comment.