• Resolved jakeparis

    (@jakeparis)


    I am creating a block which is created when transformed from a shortcode. In other words, this:

    [example att1="foo" att2="bar"]
        <p>Inner.</p>
        <p>Content.</p>
    [/example]

    should transform into this a myplugin/example block with inner blocks of the two p tags.

    So far I’ve got this, which works fine to get the shortcode attributes.

    
    (function(wp){
    
        wp.blocks.registerBlockType( 'myplugin/example', {
            ...
    
            attributes: {
                att1 {
                    type: 'string',
                    default: '',
                },
                att2: {
                    type: 'string',
                    default: '',
                },
            }, 
    
            transforms: {
                from: [
                    {
                        type: 'shortcode',
                        tag: 'example',
                        attributes: {
                            att1: {
                                type: 'string',
                                shortcode: attributes => {
                                    return attributes.named.att1 || '';
                                }
                            },
                            att2: {
                                type: 'string',
                                shortcode: attributes => {
                                    return attributes.named.att2 || '';
                                },
                            },
                        },
                    },
                ],
            },
            ...

    I can get the content by looking at the second parameter in one of those attributes shortcode functions:

    attributes: {
        att1: {
            shortcode: (attributes, data) => {
                 // data.shortcode.content is the shortcode's inner content
            }
        }
    }

    But I don’t know how to insert that content as innerBlocks in the output block. The block registration “transforms” attribute documentation is not helpful in this area.

    I’ve tried using the transform function with type: 'shortcode', but those don’t seem to work together

    • This topic was modified 5 years, 1 month ago by jakeparis.
    • This topic was modified 5 years, 1 month ago by jakeparis.
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘In a block transform (type: shortcode), how do I insert innerBlocks?’ is closed to new replies.