文档

highlight

highlightjs
Github

prism

prism
Github

示例

  1. /**
  2. * @desc: 以 json 为例
  3. */
  4. import React, { useState, useRef, useEffect } from 'react';
  5. import hljs from 'highlight.js/lib/highlight';
  6. import json from 'highlight.js/lib/languages/json';
  7. import 'highlight.js/styles/github.css';
  8. hljs.registerLanguage('json', json);
  9. function HighLightCode({ text }) {
  10. const [codeInts] = useState(useRef(null));
  11. useEffect(() => {
  12. if (codeInts.current) {
  13. hljs.highlightBlock(codeInts.current);
  14. }
  15. return () => {};
  16. }, [codeInts]);
  17. return (
  18. <code ref={codeInts}>{text}</code>
  19. );
  20. }
  21. export default HighLightCode;

更多参考

CodeMirror

CodeMirror
Github