react使用markdown进行展示

有一些文档非常长,但是又要挨个设置样式,直接用

组件库 - marked

注意文档要放在public下才能读取。但非常方便

复制代码
import { marked, Renderer } from "marked"


.....

 const [html, setHtml] = useState<any>("")
  const renderer: Renderer = new marked.Renderer()

  const getMarkdownFile = () => {
    const path = "/PrivacyPolicy.md"
    if (!path) {
      return
    }

    fetch(path)
      .then(function (response) {
        return response.text()
      })
      .then(function (data) {
        setHtml(marked(data, { renderer }))
      })
  }

  useEffect(() => {
    getMarkdownFile()
  }, [])


......

<div id="markdown">
  <div dangerouslySetInnerHTML={{ __html: html }} />
</div>

然后css不用全局,用id即可

最后是我的markdwon样式

复制代码
#markdown {
    overflow: scroll;
    color: rgba(255, 255, 255, 0.80);
    padding: 0 20px;
    font-family: "PingFang SC";
    font-size: 14px;
    font-style: normal;
    font-weight: 400;
    line-height: 200%;
}
#markdown .effectiveDate {
    color:rgba(255, 255, 255, 0.50); ;
}

#markdown strong {
    color: white;
}

#markdown p {
    margin: 20px 0;
}
#markdown .no-wrap{
    margin: 20px 0;
}
#markdown h5 {
    padding: 20px 0 10px 0;
}
#markdown h5 strong{
    color: #998D76;
}
#markdown h1 strong{
    color: #998D76;
    /* position: relative; */
    /* top: 15px; */
}
#markdown a {
    color: #83574E;
}


#markdown li p {
    margin-bottom: 0px;
    padding: 0px;
}

#markdown ul {
    list-style: disc;
    padding-left: 24px;
}

#markdown li {
    list-style: disc;
}

#markdown ol {
    padding-left: 20px;
}
#markdown ol li {
    list-style: auto;
}
相关推荐
yingyima1 分钟前
Go 语言正则表达式速查手册:30 分钟掌握核心语法与实战技巧
前端
大蝴蝶博努奇a4 分钟前
使用ChatGPT 解决各类代码报错
前端
胡志辉11 分钟前
深入浅出 call、apply、bind
前端·javascript·后端
iccb10131 小时前
5年,一个程序员是如何把私有化在线客服系统做到第一名的
前端·后端·github
假如让我当三天老蒯1 小时前
回归基本功:Map/Set 与 WeakMap/WeakSet 的区别
前端·面试
IT乐手1 小时前
48队都装不下你|国足第24次让全世界失望
前端
SoaringHeart2 小时前
Flutter最佳实践:IM聊天文字链接自动识别跳转
前端·flutter
掘金一周3 小时前
企业中要做智能体,最佳的方案是什么? | 沸点周刊 6.18
前端·人工智能·ai编程
Darling噜啦啦3 小时前
CSS 3D 变换与 Flex 布局实战:从零打造旋转立方体
前端·css
秃头网友小李3 小时前
前端难点:keep-alive 缓存什么?RouterView 的 key 为什么要带 scopeId?
前端·vue.js