gpt4 book ai didi

reactjs - 在 react-markdown 中呈现 HTML 的任何方式

转载 作者:行者123 更新时间:2023-12-05 00:43:15 26 4
gpt4 key购买 nike

我正在使用 tinymce 接受来自用户的 Markdown 数据。输出数据是html。我想在页面上显示该数据。我正在使用 react-markdown 。我可以在页面上看到数据,但它是 HTML 标签。有什么方法可以显示 HTML 页面而不是标签?

export default function ThePage() {
const markdown = {
description: "<p>Hello from the other </p>\n<p><strong>side</strong></p>",
}

return (
<>
<ReactMarkdown children={markdown.description} />
</>
);
}

最佳答案

ReactMarkdown 组件用于渲染标记 down,而不是 HTML 标记 up 😏。给定 HTML 输入,它只是将其转义,这就是为什么您将其视为“源”而不是格式化文本的原因。

如果需要与 HTML 一起使用,则需要应用插件,例如 rehypeRaw :

import ReactMarkdown from "react-markdown";
import rehypeRaw from "rehype-raw";

//...

export default function ThePage() {
const markdown = {
description: "<p>Hello from the other </p>\n<p><strong>side</strong></p>"
}

return (
<>
<ReactMarkdown children={markdown.description} rehypePlugins={[rehypeRaw]} />
</>
);
}

关于reactjs - 在 react-markdown 中呈现 HTML 的任何方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70548725/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com