Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prism plugin bugfix alt #1491

Merged
merged 5 commits into from Jul 13, 2017
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion packages/gatsby-remark-prismjs/README.md
Expand Up @@ -16,7 +16,16 @@ plugins: [
resolve: `gatsby-transformer-remark`,
options: {
plugins: [
`gatsby-remark-prismjs`,
{
resolve: `gatsby-remark-prismjs`,
options: {
// Class prefix for <pre> tags containing syntax highlighting;
// Defaults to 'language-'.
// If you use Prism directly within your site,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps rewrite this sentence slightly so makes clear this option is only needed when loading prismjs into the browser and this is not very common.

// you may use this to prevent Prism from re-processing syntax.
classPrefix: 'language-',
},
},
]
}
}
Expand Down
3 changes: 2 additions & 1 deletion packages/gatsby-remark-prismjs/package.json
Expand Up @@ -9,7 +9,8 @@
"unist-util-visit": "^1.1.1"
},
"devDependencies": {
"babel-cli": "^6.24.1"
"babel-cli": "^6.24.1",
"remark": "^7.0.1"
},
"keywords": [
"gatsby",
Expand Down
@@ -0,0 +1,89 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`remark prism plugin generates a <pre> tag with a custom class prefix if configured 1`] = `
Object {
"children": Array [
Object {
"lang": "js",
"position": Position {
"end": Object {
"column": 4,
"line": 3,
"offset": 17,
},
"indent": Array [
1,
1,
],
"start": Object {
"column": 1,
"line": 1,
"offset": 0,
},
},
"type": "html",
"value": "<div class=\\"gatsby-highlight\\">
<pre class=\\"custom-js\\"><code><span class=\\"token comment\\" spellcheck=\\"true\\">// Fake</span>
</code></pre>
</div>",
},
],
"position": Object {
"end": Object {
"column": 4,
"line": 3,
"offset": 17,
},
"start": Object {
"column": 1,
"line": 1,
"offset": 0,
},
},
"type": "root",
}
`;

exports[`remark prism plugin generates a <pre> tag with class="language-*" prefix by default 1`] = `
Object {
"children": Array [
Object {
"lang": "js",
"position": Position {
"end": Object {
"column": 4,
"line": 3,
"offset": 17,
},
"indent": Array [
1,
1,
],
"start": Object {
"column": 1,
"line": 1,
"offset": 0,
},
},
"type": "html",
"value": "<div class=\\"gatsby-highlight\\">
<pre class=\\"language-js\\"><code><span class=\\"token comment\\" spellcheck=\\"true\\">// Fake</span>
</code></pre>
</div>",
},
],
"position": Object {
"end": Object {
"column": 4,
"line": 3,
"offset": 17,
},
"start": Object {
"column": 1,
"line": 1,
"offset": 0,
},
},
"type": "root",
}
`;
18 changes: 18 additions & 0 deletions packages/gatsby-remark-prismjs/src/__tests__/index.js
@@ -0,0 +1,18 @@
const remark = require(`remark`)
const plugin = require(`../index`)

describe(`remark prism plugin`, () => {
it(`generates a <pre> tag with class="language-*" prefix by default`, () => {
const code = '```js\n// Fake\n```'
const markdownAST = remark.parse(code)
plugin({ markdownAST })
expect(markdownAST).toMatchSnapshot()
})

it(`generates a <pre> tag with a custom class prefix if configured`, () => {
const code = '```js\n// Fake\n```'
const markdownAST = remark.parse(code)
plugin({ markdownAST }, { classPrefix: 'custom-' })
expect(markdownAST).toMatchSnapshot()
})
})
15 changes: 11 additions & 4 deletions packages/gatsby-remark-prismjs/src/index.js
Expand Up @@ -3,7 +3,7 @@ const visit = require(`unist-util-visit`)
const parseLineNumberRange = require(`./parse-line-number-range`)
const highlightCode = require(`./highlight-code`)

module.exports = ({ markdownAST }) => {
module.exports = ({ markdownAST }, { classPrefix = 'language-' } = {}) => {
visit(markdownAST, `code`, node => {
let language = node.lang
let { splitLanguage, highlightLines } = parseLineNumberRange(language)
Expand All @@ -15,17 +15,24 @@ module.exports = ({ markdownAST }) => {
// outcome without any additional CSS.
//
// @see https://github.com/PrismJS/prism/blob/1d5047df37aacc900f8270b1c6215028f6988eb1/themes/prism.css#L49-L54
let preCssClassLanguage = `none`
let languageName = `none`
if (language) {
language = language.toLowerCase()
preCssClassLanguage = language
languageName = language
}

// Allow users to specify a custom class prefix to avoid breaking
// line highlights if Prism is required by any other code.
// This supports custom user styling without causing Prism to
// re-process our already-highlighted markup.
// @see https://github.com/gatsbyjs/gatsby/issues/1486
const className = `${classPrefix}${languageName}`

// Replace the node with the markup we need to make
// 100% width highlighted code lines work
node.type = `html`
node.value = `<div class="gatsby-highlight">
<pre class="language-${preCssClassLanguage}"><code>${highlightCode(
<pre class="${className}"><code>${highlightCode(
language,
node.value,
highlightLines
Expand Down