refactor remarkToRehypeShortcodes
This commit is contained in:
parent
be7385de29
commit
9174e56414
@ -202,19 +202,49 @@ const remarkShortcodes = ({ plugins }) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This plugin doesn't actually transform Remark (MDAST) nodes to Rehype
|
||||||
|
* (HAST) nodes, but rather, it prepares an MDAST shortcode node for HAST
|
||||||
|
* conversion by replacing the shortcode text with stringified HTML for
|
||||||
|
* previewing the shortcode output.
|
||||||
|
*/
|
||||||
const remarkToRehypeShortcodes = ({ plugins, getAsset }) => {
|
const remarkToRehypeShortcodes = ({ plugins, getAsset }) => {
|
||||||
return transform;
|
return transform;
|
||||||
|
|
||||||
function transform(node) {
|
function transform(root) {
|
||||||
const children = node.children ? node.children.map(transform) : node.children;
|
const transformedChildren = map(root.children, processShortcodes);
|
||||||
if (!has(node, ['data', 'shortcode'])) {
|
return { ...root, children: transformedChildren };
|
||||||
return { ...node, children };
|
}
|
||||||
}
|
|
||||||
|
/**
|
||||||
|
* Mapping function to transform nodes that contain shortcodes.
|
||||||
|
*/
|
||||||
|
function processShortcodes(node) {
|
||||||
|
/**
|
||||||
|
* If the node doesn't contain shortcode data, return the original node.
|
||||||
|
*/
|
||||||
|
if (!has(node, ['data', 'shortcode'])) return node;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get shortcode data from the node, and retrieve the matching plugin by
|
||||||
|
* key.
|
||||||
|
*/
|
||||||
const { shortcode, shortcodeData } = node.data;
|
const { shortcode, shortcodeData } = node.data;
|
||||||
const plugin = plugins.get(shortcode);
|
const plugin = plugins.get(shortcode);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Run the shortcode plugin's `toPreview` method, which will return either
|
||||||
|
* an HTML string or a React component. If a React component is returned,
|
||||||
|
* render it to an HTML string.
|
||||||
|
*/
|
||||||
const value = plugin.toPreview(shortcodeData, getAsset);
|
const value = plugin.toPreview(shortcodeData, getAsset);
|
||||||
const valueHtml = typeof value === 'string' ? value : renderToString(value);
|
const valueHtml = typeof value === 'string' ? value : renderToString(value);
|
||||||
return { ...node, value: valueHtml };
|
|
||||||
|
/**
|
||||||
|
* Return a new 'html' type node containing the shortcode preview markup.
|
||||||
|
*/
|
||||||
|
return u('html', valueHtml);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user