gatsby-remark-prismjs不适用于html

时间:2019-03-04 23:55:08

标签: graphql gatsby

gatsby-remark-prismjs无法在我的设置上运行。

我正在尝试突出显示诸如javascript和swift之类的代码。

我的博客内容是从wordpress.com获取的

enter image description here

这是我的gatsby.config.js

,
      {
        resolve: `gatsby-transformer-remark`,
        options: {
          plugins: [
            {
              resolve: `gatsby-remark-prismjs`,
              options: {
                // Class prefix for <pre> tags containing syntax highlighting;
                // defaults to 'language-' (eg <pre class="language-js">).
                // If your site loads Prism into the browser at runtime,
                // (eg for use with libraries like react-live),
                // you may use this to prevent Prism from re-processing syntax.
                // This is an uncommon use-case though;
                // If you're unsure, it's best to use the default value.
                classPrefix: "language-",
                // This is used to allow setting a language for inline code
                // (i.e. single backticks) by creating a separator.
                // This separator is a string and will do no white-space
                // stripping.
                // A suggested value for English speakers is the non-ascii
                // character '›'.
                inlineCodeMarker: null,
                // This lets you set up language aliases.  For example,
                // setting this to '{ sh: "bash" }' will let you use
                // the language "sh" which will highlight using the
                // bash highlighter.
                aliases: {},
                // This toggles the display of line numbers alongside the code.
                // To use it, add the following line in src/layouts/index.js
                // right after importing the prism color scheme:
                //  `require("prismjs/plugins/line-numbers/prism-line-numbers.css");`
                // Defaults to false.
                showLineNumbers: false,
                // If setting this to true, the parser won't handle and highlight inline
                // code used in markdown i.e. single backtick code like `this`.
                noInlineHighlight: false,
              },
            },
            ],
          },
      },

gatsby.browser.js

require("prismjs/themes/prism-okaidia.css")

这是index.js

import React, { Component } from 'react'
import { Link } from 'gatsby'
import Layout from "../layouts"
import Headline from '../components/headline'
import "../styles/main.scss"
import { redirectTo } from '@reach/router'
import { graphql } from 'gatsby'

class IndexPage extends Component {
  render() {
    const data = this.props.data.wordpressPage
    var codeTest = `
      var _self = (typeof window !== 'undefined')
    ? window   // if in browser
    : (
      (typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope)
      ? self // if in worker
      : {}   // if in node js
    );
    `
    var swift = `
      let test = "hellow"
      func test() -> Bool {
        return false
      }
    `
    return (
      <Layout>
      <Headline title={"I'm Shawn Baek"} subTitle={"iOS Developer"}/>
      <div
         style={{
           margin: '0 auto',
           maxWidth: 800,
           padding: '0px 1.0875rem 1.45rem',
           paddingTop: '1.45rem',
         }}
      >
      <div>
          <h1 style={{color:'rgb(76, 76, 76)'}}>{data.title}</h1>
          <div style={{color:'rgb(76, 76, 76)'}} dangerouslySetInnerHTML={{ __html: data.content }}></div>
      </div>
      <pre className="javascript">
          <code >
              {codeTest}
          </code>
      </pre>
      <pre className="language-swift">
          <code >
              {swift}
          </code>
      </pre>
      </div>
    </Layout>
    )
  }
}

export const IndexPageQuery = graphql`
  query IndexPageQuery {
    wordpressPage(slug: { eq: "about" }) {
      title
      content
      date(formatString: "MMMM DD, YYYY")
    }
  }
`
export default IndexPage

如您所见,PrismJS主题背景颜色和字体颜色是可行的。但是语法高亮不能像标记一样工作。

1 个答案:

答案 0 :(得分:1)

gatsby-remark-whatever插件专门用于Gatsby的Remark Markdown解析器gatsby-transformer-remark

但是,当使用Gatsby + WordPress时,您的内容来自WordPress,而不是Markdown文件。这意味着这些插件不会修改您的WordPress内容,尽管您有可能实现此目的,但这可能不是解决问题的最简单方法。

同样的事情也要进行测试:该示例代码串不会经过Gatsbty的Markdown流程,因此Markdown PrismJS插件不会产生影响。

如果您使用WordPress插件添加了语法突出显示HTML,则需要在PHP中在服务器端使用HTML,则应通过WordPress REST API进行传递。

然后,您可以手动添加所需的CSS和主题自定义(类似于构建常规WordPress主题的前端时的方式。)

或者,您可以像在另一个React项目中一样使用Prism.js。我认为本How to get PrismJS working in React教程对您有最大的帮助。

运行npm install prismjs后,基于您代码的类似示例:

// Import the PrismJS CSS, contained in the node_modules
// You might need to download a custom theme to support 
// some languages like Swift
import "prismjs/themes/prism.css";

import React, { Component } from "react";
import Prism from "prismjs";

class IndexPage extends Component {
  componentDidMount() {
    Prism.highlightAll();
  }

  render() {
    var sampleCode = `.example {
  font-weight: bold;
}`;

    return (
      <div>
        <pre>
          <code className="language-css">{sampleCode}</code>
        </pre>
      </div>
    );
  }
}

export default IndexPage;

如果您不使用其他Markdown页面,则可以决定npm uninstallgatsby-remark-*插件并删除其配置。希望对您有所帮助!