Skip to content

Commit

Permalink
use a regex to improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
scruffian committed Dec 8, 2023
1 parent 371ccc7 commit 2d3ba7f
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions packages/block-library/src/post-featured-image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,17 @@ export default function PostFeaturedImageEdit( {
);

if ( ! featuredImage && useFirstImageFromPost && postContent ) {
const blocks = parse( postContent );
const imageBlock = blocks.find(
( block ) => block.name === 'core/image'
const firstImageCloser = /<!--\s+\/wp:(?:core\/)?image\s+-->/.exec(
postContent
);
const content = firstImageCloser
? postContent.slice(
0,
firstImageCloser.index + firstImageCloser[ 0 ].length
)
: '';
const blocks = parse( content );
const imageBlock = blocks.find( ( { name } ) => name === 'core/image' );
if ( imageBlock?.attributes?.id ) {
featuredImage = imageBlock.attributes.id;
}
Expand Down

0 comments on commit 2d3ba7f

Please sign in to comment.