react解析markdown文件
<p>当当当又get到了一个新技能,使用react-markdown来直接解析markdown文件(咳咳,小菜鸟的自娱自乐)</p><p>项目中遇到了一个API的那种展示方式,类似于入门手册啥的那种,如果是一个个调用接口,改样式很复杂,所以用了直接解析后台给的markdown文件</p>
<p>首先我们需要安装一个react的网页语法高亮插件,(我最初并没有安装这个,结果导致解析文件是出来了,但是样式还挺丑的)</p>
<div class="cnblogs_code">
<pre>npm install react-syntax-highlighter --save <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">相关介绍在这里https://www.javascriptcn.com/read-43226.html</span></pre>
</div>
<p> 1.高亮的js codeBlock.js</p>
<div class="cnblogs_code">
<pre>import React from 'react'<span style="color: rgba(0, 0, 0, 1)">;
import SyntaxHighlighter from </span>'react-syntax-highlighter'<span style="color: rgba(0, 0, 0, 1)">;
import { tomorrowNightEighties } from </span>'react-syntax-highlighter/dist/esm/styles/hljs'<span style="color: rgba(0, 0, 0, 1)">; //我这边使用的是夜晚模式的css,你也可以在</span>react-syntax-highlighter/dist/esm/styles/hljs里面找你自己喜欢的css,把名字替换就行 eg:</pre>
<pre>import { monokai } from 'react-syntax-highlighter/dist/esm/styles/hljs';</pre>
<pre><span style="color: rgba(0, 0, 0, 1)">import { Form } from </span>'antd'<span style="color: rgba(0, 0, 0, 1)">; class CodeBlock extends React.PureComponent { render() { const { value } </span>= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.props; </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> ( </span><SyntaxHighlighter language="" style={tomorrowNightEighties}><span style="color: rgba(0, 0, 0, 1)"> {value} </span></SyntaxHighlighter> <span style="color: rgba(0, 0, 0, 1)"> ); } } export </span><span style="color: rgba(0, 0, 255, 1)">default</span> Form.create()(CodeBlock);</pre>
</div>
<p>2.然后解析文件的js</p>
<p>我这边直接从网上找了个.md文件如下</p>
<div class="cnblogs_code">
<pre><p align="center">
<a href="https://github.com/uiwjs/react-markdown-editor/issues">
<img src="https://img.shields.io/github/issues/uiwjs/react-markdown-editor.svg">
</a>
<a href="https://github.com/uiwjs/react-markdown-editor/network">
<img src="https://img.shields.io/github/forks/uiwjs/react-markdown-editor.svg">
</a>
<a href="https://github.com/uiwjs/react-markdown-editor/stargazers">
<img src="https://img.shields.io/github/stars/uiwjs/react-markdown-editor.svg">
</a>
<a href="https://github.com/uiwjs/react-markdown-editor/releases">
<img src="https://img.shields.io/github/release/uiwjs/react-markdown-editor.svg">
</a>
<a href="https://www.npmjs.com/package/@uiw/react-markdown-editor">
<img src="https://img.shields.io/npm/v/@uiw/react-markdown-editor.svg">
</a>
</p>
<p align="center"><span style="color: rgba(0, 0, 0, 1)">
A markdown editor </span><span style="color: rgba(0, 0, 255, 1)">with</span> preview, implemented <span style="color: rgba(0, 0, 255, 1)">with</span><span style="color: rgba(0, 0, 0, 1)"> React.js and TypeScript.
</span></p>
<span style="color: rgba(0, 0, 0, 1)">
## Install
```bash
npm i @uiw</span>/react-markdown-editor
<span style="color: rgba(0, 0, 0, 1)">```
## Document
Official document (https:</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">uiwjs.github.io/react-markdown-editor/) ([🇨🇳中国镜像网站](http://uiw.gitee.io/react-markdown-editor/))</span>
<span style="color: rgba(0, 0, 0, 1)">
## Basic Usage
```jsx
import MarkdownEditor from </span>'@uiw/react-markdown-editor'<span style="color: rgba(0, 0, 0, 1)">;
import React from </span>'react'<span style="color: rgba(0, 0, 0, 1)">;
import ReactDOM from </span>'react-dom'<span style="color: rgba(0, 0, 0, 1)">;
const Dome </span>= () =><span style="color: rgba(0, 0, 0, 1)"> (
</span><<span style="color: rgba(0, 0, 0, 1)">MarkdownEditor
value</span>={<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.state.markdown}
onChange</span>={<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.updateMarkdown}
</span>/>
<span style="color: rgba(0, 0, 0, 1)">);
```
controlled usage
```jsx
import MarkdownEditor from </span>'@uiw/react-markdown-editor'<span style="color: rgba(0, 0, 0, 1)">;
import React from </span>'react'<span style="color: rgba(0, 0, 0, 1)">;
import ReactDOM from </span>'react-dom'<span style="color: rgba(0, 0, 0, 1)">;
class App extends React.Component {
constructor() {
super();
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.state =<span style="color: rgba(0, 0, 0, 1)"> {
markdown: </span>'# This is a H1\n## This is a H2\n###### This is a H6'<span style="color: rgba(0, 0, 0, 1)">,
};
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.updateMarkdown = <span style="color: rgba(0, 0, 255, 1)">this</span>.updateMarkdown.bind(<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">);
}
updateMarkdown(editor, data, value) {
</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.setState({ markdown: value });
}
render() {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (
</span><<span style="color: rgba(0, 0, 0, 1)">MarkdownEditor
value</span>={<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.state.markdown}
onChange</span>={<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.updateMarkdown}
</span>/>
<span style="color: rgba(0, 0, 0, 1)"> );
}
}
ReactDOM.render(
</span><App />,
document.getElementById('app'<span style="color: rgba(0, 0, 0, 1)">)
);
```
## Props
</span>- value (*string*) - the raw markdown that will be converted to html (**required**<span style="color: rgba(0, 0, 0, 1)">)
</span>- `visble?:<span style="color: rgba(0, 0, 255, 1)">boolean</span>` -<span style="color: rgba(0, 0, 0, 1)"> Shows a preview that will be converted to html.
</span>- `toolbars?:array` -<span style="color: rgba(0, 0, 0, 1)"> Tool display settings.
</span>- `toolbarsMode?:array` -<span style="color: rgba(0, 0, 0, 1)"> Tool display settings.
</span>- onChange (*<span style="color: rgba(0, 0, 255, 1)">function</span>(editor: IInstance, data: CodeMirror.EditorChange, value: string)*) - called when a change is made (**required**<span style="color: rgba(0, 0, 0, 1)">)
</span>> (https:<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">github.com/uiwjs/react-markdown-editor/blob/8de6abbf628b6d272d7da1c28e985fbbcba71b93/src/components/CodeMirror/index.tsx#L21-L60)</span>
<span style="color: rgba(0, 0, 0, 1)">
### Development
```bash
npm run dev
npm run type</span>-<span style="color: rgba(0, 0, 0, 1)">check:watch
npm run doc
```
## License
(.</span>/LICENSE)</pre>
</div>
<p>照例先安装react-markdown</p>
<div class="cnblogs_code">
<pre>npm install --save react-markdown<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">具体的介绍在这里https://www.javascriptcn.com/read-34344.html</span></pre>
</div>
<p>解析markdown文件的home.js</p>
<div class="cnblogs_code">
<pre>import React, {Component} from 'react'<span style="color: rgba(0, 0, 0, 1)">;
import ReactMarkdown from </span>'react-markdown/with-html'<span style="color: rgba(0, 0, 0, 1)">;
import AppMarkdown from </span>'./test.md'<span style="color: rgba(0, 0, 0, 1)">;
import CodeBlock from </span>'./codeBlock'<span style="color: rgba(0, 0, 0, 1)">;
class Home extends React.Component {
constructor(props) {
super(props);
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.state =<span style="color: rgba(0, 0, 0, 1)"> {
markdown: </span>''<span style="color: rgba(0, 0, 0, 1)">,
}
}
componentWillMount() {
fetch(AppMarkdown)
.then(res </span>=><span style="color: rgba(0, 0, 0, 1)"> res.text())
.then(text </span>=> <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.setState({markdown: text}));
}
render() {
const {markdown} </span>= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.state;
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (
</span><div>
<div >
<<span style="color: rgba(0, 0, 0, 1)">ReactMarkdown
className</span>="markdown-body"<span style="color: rgba(0, 0, 0, 1)">
source</span>=<span style="color: rgba(0, 0, 0, 1)">{markdown}
escapeHtml</span>={<span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">}
renderers</span>=<span style="color: rgba(0, 0, 0, 1)">{{
code: CodeBlock,
}}
</span>/>
</div>
</div>
<span style="color: rgba(0, 0, 0, 1)"> )
}
}
export </span><span style="color: rgba(0, 0, 255, 1)">default</span> Home;</pre>
</div>
<p>然后就可以出来效果了</p>
<p><img src="https://img2018.cnblogs.com/blog/1117603/201906/1117603-20190610164244766-748474336.png"></p>
<p> 注:这个时候你看你的f12中属性都是添加到各个元素里面的,如果你想要的自己定义css,这个时候可以在这里添加个属性,然后再引入你想要的css文件就可以了</p>
<div class="cnblogs_code">
<pre>import './mark.css'; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">自己定义的css</span>
<SyntaxHighlighter language="" style={tomorrowNightEighties} useInlineStyles={<span style="color: rgba(0, 0, 255, 1)">false</span>}> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">userInlineStyles可以给元素添加classname</span>
<span style="color: rgba(0, 0, 0, 1)"> {value}
</span></SyntaxHighlighter></pre>
</div>
<p> </p><br><br>
来源:https://www.cnblogs.com/yesu/p/10998594.html
頁:
[1]