Skip to content
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

Re-evaluate shortcode blocks for more specific shortcode transforms #8569

Open
ccprog opened this issue Aug 6, 2018 · 2 comments
Open

Re-evaluate shortcode blocks for more specific shortcode transforms #8569

ccprog opened this issue Aug 6, 2018 · 2 comments
Labels
[Feature] Shortcodes Related to shortcode functionality [Type] Enhancement A suggestion for improvement.

Comments

@ccprog
Copy link

ccprog commented Aug 6, 2018

We're currently in a situation where some plugins using shortcodes do not yet convert them to blocks, but might do so at a later time.

This leads to the following scenario: A user has content containing shortcodes. He/she opens it in the Gutenberg editor and converts it to blocks. The shortcodes will be converted to a core/shortcode block. Later (and this might happen for a long time) the plugin developer decides to provide a conversion specific to his/her shortcodes. This transformation will never match the shortcode block, because it is only considered for raw handling.

For my plugin, I was able to solve this with a second from-transform:

{
    type: 'block',
    blocks: ['core/shortcode'],
    isMatch: ( {text} ) => {
        const re = wp.shortcode.regexp('crosswordsearch');
        return re.test(text);
    },
    transform: ( {text} ) => {
        return wp.blocks.rawHandler({
            HTML: '<p>' + text + '</p>',
            mode: 'BLOCKS'
        });
    },
    priority: 15
}

Instead of every developer implementing something like this, it would be much better to define this as a to-transform on the core/shortcode block with

  • a blocks list of all block type names that offer type: 'shortcode' from-transforms.
  • isMatch testing for all the tags named in those block types
@aduth
Copy link
Member

aduth commented May 31, 2019

If I understand correctly, is the issue that a block can define that it be transformed from a shortcode, but if the content is already migrated to a "Shortcode" block, then these won't be considered as candidates?

Example Gallery shortcode transform:

{
type: 'shortcode',
tag: 'gallery',
attributes: {
images: {
type: 'array',
shortcode: ( { named: { ids } } ) => {
return parseShortcodeIds( ids ).map( ( id ) => ( {
id,
} ) );
},
},
ids: {
type: 'array',
shortcode: ( { named: { ids } } ) => {
return parseShortcodeIds( ids );
},
},
columns: {
type: 'number',
shortcode: ( { named: { columns = '3' } } ) => {
return parseInt( columns, 10 );
},
},
linkTo: {
type: 'string',
shortcode: ( { named: { link = 'attachment' } } ) => {
return link === 'file' ? 'media' : link;
},
},
},
},

It would not identify a possible transform if, for example, a user had content:

<!-- wp:shortcode -->
[gallery]
<!-- /wp:shortcode -->

This makes sense to me as an area for improvement. In particular, we may consider to treat shortcode blocks as their raw shortcode counterparts when trying to determine transforms for Shortcode block.

In the UI then:

image

... should present transform options to convert this Shortcode block to Gallery.

@mcsf
Copy link
Contributor

mcsf commented Nov 12, 2019

This seems like a quick win. /cc @ellatrix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Shortcodes Related to shortcode functionality [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

No branches or pull requests

4 participants