Skip to content

Commit

Permalink
simplify regex
Browse files Browse the repository at this point in the history
  • Loading branch information
scruffian committed Jan 4, 2024
1 parent afd950b commit df6a00c
Showing 1 changed file with 8 additions and 17 deletions.
25 changes: 8 additions & 17 deletions packages/block-library/src/post-featured-image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { parse } from '@wordpress/blocks';
import { useEntityProp, store as coreStore } from '@wordpress/core-data';
import { useSelect, useDispatch } from '@wordpress/data';
import {
Expand Down Expand Up @@ -94,22 +93,14 @@ export default function PostFeaturedImageEdit( {
return;
}

const firstImageCloser = /<!--\s+\/wp:(?:core\/)?image\s+-->/.exec(
postContent
);

if ( ! firstImageCloser ) {
return;
}

const content = postContent.slice(
0,
firstImageCloser.index + firstImageCloser[ 0 ].length
);

const blocks = parse( content );
const imageBlock = blocks.find( ( { name } ) => name === 'core/image' );
return imageBlock?.attributes?.id;
const imageOpener =
/<!--\s+wp:(?:core\/)?image\s+(?<attrs>{(?:(?:[^}]+|}+(?=})|(?!}\s+\/?-->).)*)?}\s+)?-->/.exec(
postContent
);
const imageId =
imageOpener?.groups?.attrs &&
JSON.parse( imageOpener.groups.attrs )?.id;
return imageId;
}, [ storedFeaturedImage, useFirstImageFromPost, postContent ] );

const { media, postType, postPermalink } = useSelect(
Expand Down

0 comments on commit df6a00c

Please sign in to comment.