【问题标题】:How to transform HTML code block in Gatsby with syntax highlighting using Prism.js?如何使用 Prism.js 在 Gatsby 中使用语法高亮转换 HTML 代码块?
【发布时间】:2018-08-06 15:30:18
【问题描述】:

我想对具有<code> 标签的 HTML 内容应用语法高亮;例如

<pre>
  <code>
    function hello () {
      console.log("Hello!");
    }
  </code>
</pre>

gatsby-remark-prismjs 等 Gatsby 插件仅适用于 Markdown 文件。我正在尝试使用gatsby-node.js 以编程方式生成页面。

【问题讨论】:

标签: gatsby prismjs


【解决方案1】:

你可以使用

import Prism from 'prismjs'

const Component = () => {
 let code_example = `let myvar = 'hello'`
 let html_example = Prism.highlight(code_example, Prism.languages.javascript, 'javascript{numberLines: true}')

 //...

 return (
  <pre class="language-js">
   <code class="language-js" dangerouslySetInnerHTML={{ __html: html_example }}></code>
  </pre>
 )
}

但我无法在此处使用行号

【讨论】:

    猜你喜欢
    • 2014-04-10
    • 1970-01-01
    • 2021-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-08
    • 2015-09-28
    • 2010-09-14
    相关资源
    最近更新 更多