简体   繁体   English

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

[英]Any way to render HTML in react-markdown

I am using tinymce to accept markdown data from user.我正在使用 tinymce 接受来自用户的 markdown 数据。 The output data is html. I want to display that data on a page. output 数据是 html。我想在页面上显示该数据。 I am using react-markdown for it.我正在为此使用 react-markdown。 I can see data on page but it's HTML tags.我可以在页面上看到数据,但它是 HTML 标签。 Is there any way to show HTML page not tags?有没有办法显示 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} /> </> ); }

Yes you can use react-render-markup see example:是的,您可以使用react-render-markup参见示例:

import { Markup } from "react-render-markup";
 export default function App(){
 return(<Markup markup={markdown.description} />)
 }

yes you can;是的你可以;

const [html, setHtml] = useState("<p>Hello from the other </p>\n<p><strong>side</strong></p>");

use it like this像这样使用它

  <div
    dangerouslySetInnerHTML={{
      __html: html,
    }}
 
  ></div>

The ReactMarkdown component is for rendering mark down , not HTML mark up . ReactMarkdown组件用于渲染标记,不是HTML标记。 Given HTML input it just escapes it, and that's why you see it as "source", not formatted text.鉴于 HTML 输入它只是转义它,这就是为什么您将其视为“源”,而不是格式化文本。

If you need to use it with HTML you need to apply a plugin, such as rehypeRaw :如果您需要将它与 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]} />
    </>
  );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM