Created
June 10, 2021 14:17
-
-
Save markhowellsmead/a01e30e3589b5be0c4886a3f2c207a76 to your computer and use it in GitHub Desktop.
WordPress Gutenberg - transform core list block to custom block with multiple InnerBlocks.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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