【发布时间】:2020-07-30 07:45:56
【问题描述】:
这是我的代码
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import MarkdownRenderer from 'react-markdown-renderer';
export default class Content extends Component {
constructor(props) {
super(props);
this.state = {
content: ''
};
}
componentWillMount() {
let file_path = this.props.mdfPath;
fetch(file_path)
.then(response => response.text())
.then(content => {
this.setState({ content })
})
}
render() {
return(
<div>
<MarkdownRenderer markdown={this.state.content}/>
</div>
)
}
}
该组件获取路径传递给它的任何 Markdown 文件的内容(通过 props),然后利用 react-markdown-renderer 将该 Markdown 转换为 HTML。
我已经下载了 hihglight.js 文件并在我的 index.html 文件中指向它们。我还在 index.html 中运行了函数initHighlightingOnLoad()。但是,当网站加载时,我的代码块没有突出显示。我不确定发生了什么...有人可以帮忙吗?
这是 <MarkdownRenderer markdown={this.state.content} /> 输出到 DOM 的内容
<div>
<h1>My Site</h1>
<p>This is my site...</p>
<pre>
<code class="language-js">
const msg = 'Welcome to My Site';
console.log(msg); // Welcome to My SIte
</code>
</pre>
</div>
【问题讨论】:
标签: reactjs markdown syntax-highlighting highlight.js