gpt4 book ai didi

reactjs - Tailwind/排版如何在 React 项目中与 markdown-it 良好配合?

转载 作者:行者123 更新时间:2023-12-02 18:35:05 25 4
gpt4 key购买 nike

我尝试为我的博客开发一项新功能,即用于撰写文章的 Markdown 编辑器。

我选择了 @tailwindcss/typography markdown-it为此,这就是我的全部依赖项:

package.json

{
"dependencies": {
"firebase": "^9.0.0-beta.7",
"markdown-it": "^12.2.0",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-router-dom": "^5.2.0"
},
"devDependencies": {
"@babel/core": "^7.15.0",
"@babel/preset-env": "^7.15.0",
"@babel/preset-react": "^7.14.5",
"@tailwindcss/typography": "^0.4.1",
"autoprefixer": "^10.3.2",
"babel-loader": "^8.2.2",
"css-loader": "^6.2.0",
"dotenv-webpack": "^7.0.3",
"html-webpack-plugin": "^5.3.2",
"postcss": "^8.3.6",
"postcss-cli": "^8.3.1",
"postcss-loader": "^6.1.1",
"style-loader": "^3.2.1",
"tailwindcss": "^2.2.7",
"webpack": "^5.51.1",
"webpack-cli": "^4.8.0",
"webpack-dev-server": "^4.0.0"
}
}

下面的代码是该功能的组件,包括编辑区域和预览区域。然而,这并没有奏效。

当我运行此代码时,它呈现为 this ,无需打印 <h1>标签。

但是,如果我替换 md.render(markdown)<h1>hello</h1> (markdown-它的渲染结果),看起来“有效”,看起来像 this .

编辑器.jsx
import React, { useState } from "react";
const md = require("markdown-it")('commonmark');


const Editor = () => {
const [markdown, setMarkdown] = useState("# hello");
const onTextChange = (e) => {
setMarkdown(e.target.value);
}

return (
<div>
<form>
<textarea onChange={(e) => onTextChange(e)}>
{markdown}
</textarea>
</form>

<div id="preview" className="prose">
{md.render(markdown)} {/* <h1>hello</h1> */}
</div>

</div>
)
}

export default Editor;

为什么会发生这些事情?我怎样才能让它按预期运行?

最佳答案

使用react-markdown而不是 markdown-it

这是一个例子:

import ReactMarkdown from "react-markdown";

<div className="prose">
<ReactMarkdown>{markdown}</ReactMarkdown>
</div>

它将在页面中将提供的内容呈现为 DOM,并且 Tailwind/typography 完美地设置这些元素的样式。

关于reactjs - Tailwind/排版如何在 React 项目中与 markdown-it 良好配合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68897988/

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