reactjs - Highlight.js 不适用于 react-markdown
问题描述
我正在使用react-syntax-highlighter@15.4.3
语法突出显示。下面是代码:
import React, { PureComponent } from "react";
import PropTypes from "prop-types";
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
import { docco } from "react-syntax-highlighter/dist/cjs/styles/hljs";
class CodeBlock extends PureComponent {
static propTypes = {
value: PropTypes.string.isRequired,
language: PropTypes.string
};
static defaultProps = {
language: null
};
render() {
const { language, value } = this.props;
return (
<SyntaxHighlighter style={docco}>
{value}
</SyntaxHighlighter>
);
}
}
export default CodeBlock;
<ReactMarkdown source={this.state.post.description} renderers={{CodeBlock}}/>
我希望它能够检测到由 自动提供给它的语言react-markdown
,但它没有检测到语言,因此代码没有被美化。
我该怎么做才能开始自行检测语言?
解决方案
我在以下文档中找到了以下答案react-markdown
:
import React from 'react'
import ReactMarkdown from 'react-markdown'
import SyntaxHighlighter from 'react-syntax-highlighter'
import {docco} from 'react-syntax-highlighter/dist/esm/styles/hljs'
import {render} from 'react-dom'
export const renderers = {
code: ({language, value}) => {
return <SyntaxHighlighter style={docco} language={language} children={value} />
}
}
推荐阅读
- svg - SVG xlink:href 在 Chrome 中不起作用
- php - NelmioApiDocBundle 不生成文档
- excel - 使用或在excel中的平均ifs
- c# - Microsoft Bot 框架程序集参考
- c# - 如何使用 C# 和 XNA 进行双跳?
- c# - Breeze.net:在端点返回之前应用 ODATA 过滤器
- ms-access - 尝试查询多选列表框时出现错误 3131
- c++ - Microsoft C++ 异常:内存位置的 boost::archive::archive_exception(通过客户端服务器 Boost Udp 解析向量)
- android - 使用 ionic 3 编译 apk
- html - CSS和HTML定位的解释