Skip to content

Filterable behavior for Image Block raw transformations #8473

@danielbachhuber

Description

@danielbachhuber

One of the plugins I work on, Tasty Pins, offers the ability to add "Pinterest Text" to an image (related #8472). When the image ends up in the Classic Editor, its markup looks like this:

image

Notice the inclusion of data-pin-description="This cashew coffee is delicious, creamy, and easy. Perfect for a chilly morning! | pinchof yum.com".

When this image is transformed to an Image Block, the data-pin-description attribute is lost, even though I've registered it as a custom attribute to the block (related #8470, #6878).

pinterestconversion

What's the best way to filter the Image Block transform behavior? It doesn't appear plugins have much control over the behavior:

transform: ( node ) => {
// Search both figure and image classes. Alignment could be
// set on either. ID is set on the image.
const className = node.className + ' ' + node.querySelector( 'img' ).className;
const alignMatches = /(?:^|\s)align(left|center|right)(?:$|\s)/.exec( className );
const align = alignMatches ? alignMatches[ 1 ] : undefined;
const idMatches = /(?:^|\s)wp-image-(\d+)(?:$|\s)/.exec( className );
const id = idMatches ? idMatches[ 1 ] : undefined;
const anchorElement = node.querySelector( 'a' );
const linkDestination = anchorElement && anchorElement.href ? 'custom' : undefined;
const href = anchorElement && anchorElement.href ? anchorElement.href : undefined;
const blockType = getBlockType( 'core/image' );
const attributes = getBlockAttributes( blockType, node.outerHTML, { align, id, linkDestination, href } );
return createBlock( 'core/image', attributes );

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions