Skip to content

Instantly share code, notes, and snippets.

@markhowellsmead
Created June 10, 2021 14:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markhowellsmead/a01e30e3589b5be0c4886a3f2c207a76 to your computer and use it in GitHub Desktop.
Save markhowellsmead/a01e30e3589b5be0c4886a3f2c207a76 to your computer and use it in GitHub Desktop.
WordPress Gutenberg - transform core list block to custom block with multiple InnerBlocks.
transforms: {
from: [
{
type: 'block',
blocks: ['core/list'],
transform: ({ values }) => {
let entries = values.split('</li><li>');
const link_pattern = /<a href=['"]#endnote[0-9]+['"]>([0-9]+)<\/a>/gi;
entries.forEach((entry, index) => {
entries[index] = entry.replace('<li>', '').replace('</li>', '');
entries[index] = entries[index].replaceAll(link_pattern, '');
});
const innerBlocks = [];
innerBlocks.push(
createBlock('core/heading', {
level: 2,
content: _x('Fussnoten', 'Default content', 'sha'),
})
);
entries.forEach(entry => {
innerBlocks.push(
createBlock('core/paragraph', {
content: entry,
})
);
});
return createBlock('sht/footnotes', {}, innerBlocks);
},
},
],
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment