-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Block Library: Add features to the Post Excerpt block. #19715
Conversation
"category": "layout" | ||
"category": "layout", | ||
"attributes": { | ||
"excerptLength": { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not just "length"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's clearer that it refers to the excerpt part. The block also has the read more text and could evolve to have other things in the future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should probably be wordCount
or wordLength
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wordCount sounds like the most accurate to me. Let's change it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I disagree because of this being a "max length" and not a word count but I don't think it's a big deal so I've updated it.
"moreText": { | ||
"type": "string" | ||
}, | ||
"showMoreOnNewLine": { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be an attribute or just something a theme can tweak with CSS?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Making it an attribute allows us to leverage different markup.
For instance adding a post excerpt block on the post itself would mean that this specific block would not show up on the frontend when viewing the post. Meaning any block such as the post excerpt block that does not show up in the frontend post/page layout but has another specific feature could perhaps have a visual marker in the backend. To give it a clear difference between blocks used in the post/page layout and blocks that have a specific purpose not included in the regular layout. |
It does render the excerpt for that post. In any case, limiting things like this is what |
I think this might be where CSS could be used. I could see exploring style variations but that feels like down to the theme almost. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work here :) One thing I think should be changed is the fact that usePostContentExcerpt
has a custom logic that trims and cuts the post content in order to get the excerpt preview, while the server side render_block_core_post_excerpt
uses the normal get_the_excerpt
.
This will lead to things showing up in the editor which will not show up in the front.
As far as I can tell, I may be wrong, the REST API returns an element ['excerpt']['raw']
which is the result of some filters including wp_trim_excerpt
. This filter/function takes care of generating an excerpt from the post data, so we shouldn't need to redo this logic. Also, the best thing it does, is to call strip_shortcodes
and excerpt_remove_blocks
which avoids bumping into issues like #19418
Do you have an example? They should be aligned.
Yes, we do because we are dynamically changing the excerpt length in the client and we wish to preview it while we do so.
That's why I use rendered post content. |
Again, I am not 100% sure, but it appears that there are different sets of operations happening on each of the filters used for content or excerpt:
Most notably the_excerpt does not |
I meant in this PR. A scenario where it breaks.
I am taking the same approach the latest posts block takes and it seems to
work well.
wp_trim_excerpt also applies the_content.
excerpt_remove_blocks just removes blocks that won’t contribute to the
excerpt like images. This is achieved in the client by using innerHTML and
extracting text content only.
|
So I have tried to reproduce the bug in #19418 and it worked: The point in the image above is that the excerpt now contains a link, and it should in fact be empty. To reproduce this:
|
I think the correct fix here is not rendering that URL as visible text like that. |
What's blocking this? |
const [ excerpt, setExcerpt ] = useEntityProp( 'postType', 'post', 'excerpt' ); | ||
return <PlainText value={ excerpt } onChange={ setExcerpt } />; | ||
const postContentExcerpt = usePostContentExcerpt( excerptLength ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this because the backend defaults to the post content if the excerpt is empty?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, and you can't control the max length dynamically.
|
||
function PostExcerptDisplay() { | ||
function usePostContentExcerpt( excerptLength ) { | ||
const [ , , { raw: rawPostContent } ] = useEntityProp( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we have three returned values in the array, is the first one normalized or something like that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The first one is the edited value, yes.
keepPlaceholderOnFocus | ||
/> | ||
{ ! showMoreOnNewLine && ' ' } | ||
<RichText |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be inside a paragraph if "showMoreOnNewLine" is true (to match the backend)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, updated.
87dc653
to
2e35754
Compare
lib/demo-block-templates/single.html
Outdated
@@ -0,0 +1,2 @@ | |||
<!-- wp:post-title /--> | |||
<!-- wp:post-content /--> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like this is an unrelated change, do we want it or should we remove it from the PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, deleted.
3502985
to
307facc
Compare
307facc
to
ece6591
Compare
attributes, | ||
setAttributes, | ||
isSelected, | ||
} ) { | ||
if ( ! useEntityId( 'postType', 'post' ) ) { | ||
return 'Post Excerpt Placeholder'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This text cannot be translated if it's defined statically.
$filter_excerpt_length | ||
); | ||
|
||
$output = '<p>' . get_the_excerpt( $post ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This element should have a class name, so that it can be targeted by Theme and Plugin developers.
Closes #19698
Description
This PR implements the design from #19698.
How to test this?
Verify all the features from #19698 work as expected with a Post Excerpt block inserted in a regular post.
Screenshots
Types of Changes
New Feature: The Post Excerpt block now has a more rich editing experience and allows you to customize the excerpt length and read more link directly.
Checklist: