首页 > 解决方案 > 想在 react-markdown 文件中编写脚本,但所有字符都已转义

问题描述

我在使用 Next 和 react-markdown 的降价网站上遇到问题。我想添加一些脚本,例如 Github gist 的嵌入标签,但所有字符都已转义。有没有解决脚本标签内的字符转义的方法?

我添加rehypePlugins={[rehypeRaw]}到启用的 HTML 中:

// pages/post/[id].tsx
...
<ReactMarkdown components={components} rehypePlugins={[rehypeRaw]}>
   {markdownBody}
</ReactMarkdown>
...

我试过了:

// posts/somemarkdwon.md

## H1 title here

normal text here

<p>hi<br />hi</span></p>

<script>const hi = "3"; alert(hi);</script>

输出:

<h2>H1 title here</h2>
<p>normal text here</p>
<p>hi<br/>hi</p>
<script>const hi = &quot;3&quot;; alert(hi);</script>

“3”转换为&quot;3&quot;,脚本出错。

有没有人有好的解决方案?

标签: javascriptescapingmarkdownreact-markdown

解决方案


推荐阅读