我正在使用React Markdown (https://github.com/rexxars/react-markdown)来呈现markdown内容。
我想问一下如何呈现我们在markdown中定义的特定短码。
例如,在我的markdown内容中,我可以插入这个短码[[ table product="mask" ]]
然后,React Markdown组件可以获取它,并呈现我之前定义的自定义组件(如<Table product="mask" />
)。
我已经通读了文档,但没有找到任何文档。如果你之前有这样做的经验,请让我知道。
非常感谢。
发布于 2020-06-08 09:48:47
您将需要remark-shortcodes
插件并定义一个renderers
字段来告诉ReactMarkdown如何处理您的快捷码:
const YourComponent = (content: string) => (
<ReactMarkdown
source={content}
plugins={[
[require("remark-shortcodes"), {startBlock: "[[", endBlock: "]]", inlineMode: true }]
]}
renderers={{ shortcode: Shortcode }}
/>
)
const Shortcode = (props: any) => {
/*
props will looks something like:
{
"type": "shortcode",
"identifier": "MailchimpForm",
"attributes": { "id": "chfk2" }
}
see: https://github.com/djm/remark-shortcodes
*/
switch (props.identifier) {
case 'table':
return <Table .../>;
default:
throw new Error('unknown shortcode')
}
};
您可能需要也可能不需要startBlock
、endBlock
和inlineMode
选项,具体取决于您正在构建的内容。
https://stackoverflow.com/questions/60933826
复制相似问题