Created
December 13, 2018 19:38
-
-
Save braco/3d916a2cff8e62b046128ef042038c1c to your computer and use it in GitHub Desktop.
Gatsby + Wordpress reparser
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
import React from 'react'; | |
import ReactHtmlParser, { processNodes } from 'react-html-parser'; | |
import generatePropsFromAttributes from 'react-html-parser/lib/utils/generatePropsFromAttributes'; | |
import ModuleName from 'mymodules/ModuleName'; | |
// This module lowercases tags, why? | |
const overrideComponents = { | |
modulename: ModuleName, | |
} | |
const transform = (node, index) => { | |
if (node.type === 'tag' && overrideComponents[node.name]) { | |
return React.createElement( | |
overrideComponents[node.name], | |
generatePropsFromAttributes(node.attribs, index), | |
processNodes(node.children, transform) | |
) | |
} | |
} | |
const ParseHTML = html => ReactHtmlParser(html, { transform }); | |
export default ParseHTML; |
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
// Gatsby Wordpress page template | |
import ParseHTML from './ParseHTML'; | |
// ... | |
{ ParseHTML(content) } | |
// ... |
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
import ModuleName from 'mymodules/ModuleName'; | |
const dynamicBlocks = { | |
'ModuleName': { | |
title: 'Module Name', | |
component: () => <ModuleName />, | |
componentName: 'ModuleName', | |
} | |
} | |
Object.keys(dynamicBlocks).map(k => | |
wp.blocks.registerBlockType(k, { | |
icon: 'shield', | |
category: 'common', | |
keywords: [], | |
edit: dynamicBlocks[k].component, | |
save: () => React.createElement(dynamicBlocks[k].componentName), | |
title: dynamicBlocks[k].title, | |
}) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment