Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gatsby-remark-prismjs): add show invisibles plugin (#21439)
* 20642 Prism show invisibles plugin * feat(gatsby-remark-prismjs): add show invisibles tests * feat(gatsby-remark-prismjs): fixes eslint no-redeclare * feat(gatsby-remark-prismjs): fixes eslint no-redeclare * feat(gatsby-remark-prismjs): fix eslint no-redeclare * Revert "Merge branch 'topics/20642-remark-prismjs-plugin' of https://github.com/rbelow/gatsby into topics/20642-remark-prismjs-plugin" This reverts commit eaf1691, reversing changes made to a6b52bb.
- Loading branch information
Showing
4 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
/*.js | ||
/plugins |
11 changes: 11 additions & 0 deletions
11
packages/gatsby-remark-prismjs/src/__tests__/plugins/prism-show-invisibles.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const loadPrismShowInvisibles = require(`../../plugins/prism-show-invisibles`) | ||
|
||
describe(`prism-show-invisibles`, () => { | ||
test(`should export a function`, () => { | ||
expect(typeof loadPrismShowInvisibles).toBe(`function`) | ||
}) | ||
|
||
test(`should not return`, () => { | ||
expect(loadPrismShowInvisibles()).toBeUndefined() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
packages/gatsby-remark-prismjs/src/plugins/prism-show-invisibles.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
const Prism = require(`prismjs`) | ||
|
||
const env = { | ||
grammar: undefined, | ||
} | ||
|
||
function loadEnv(language) { | ||
env.grammar = Prism.languages[language] | ||
if (!env.grammar) return | ||
} | ||
|
||
module.exports = (language = `javascript`) => { | ||
loadEnv(language) | ||
|
||
// Gatsby Prism Show Invisibles Plugin | ||
// | ||
// This file serves as an adapter module | ||
// for the original Prism Show Invisibles | ||
// implementation. The ported plugin code | ||
// is from npm's prismjs@1.17.1. | ||
// | ||
// @see https://github.com/PrismJS/prism/blob/v1.17.1/plugins/show-invisibles/prism-show-invisibles.js | ||
// | ||
// prism-show-invisibles.js:start | ||
if ( | ||
(typeof self !== `undefined` && !self.Prism) || | ||
(typeof global !== `undefined` && !global.Prism) | ||
) { | ||
return | ||
} | ||
|
||
var invisibles = { | ||
tab: /\t/, | ||
crlf: /\r\n/, | ||
lf: /\n/, | ||
cr: /\r/, | ||
space: / /, | ||
} | ||
|
||
/** | ||
* Handles the recursive calling of `addInvisibles` for one token. | ||
* | ||
* @param {Object|Array} tokens The grammar or array which contains the token. | ||
* @param {string|number} name The name or index of the token in `tokens`. | ||
*/ | ||
function handleToken(tokens, name) { | ||
var value = tokens[name] | ||
|
||
var type = Prism.util.type(value) | ||
switch (type) { | ||
case `RegExp`: | ||
var inside = {} | ||
tokens[name] = { | ||
pattern: value, | ||
inside: inside, | ||
} | ||
addInvisibles(inside) | ||
break | ||
|
||
case `Array`: | ||
for (var i = 0, l = value.length; i < l; i++) { | ||
handleToken(value, i) | ||
} | ||
break | ||
|
||
default: | ||
// 'Object' | ||
var inside = value.inside || (value.inside = {}) // eslint-disable-line no-redeclare | ||
addInvisibles(inside) | ||
break | ||
} | ||
} | ||
|
||
/** | ||
* Recursively adds patterns to match invisible characters to the given grammar (if not added already). | ||
* | ||
* @param {Object} grammar | ||
*/ | ||
function addInvisibles(grammar) { | ||
if (!grammar || grammar[`tab`]) { | ||
return | ||
} | ||
|
||
// assign invisibles here to "mark" the grammar in case of self references | ||
for (var name in invisibles) { | ||
if (invisibles.hasOwnProperty(name)) { | ||
grammar[name] = invisibles[name] | ||
} | ||
} | ||
|
||
/* eslint-disable no-redeclare */ | ||
for (var name in grammar) { | ||
/* eslint-enable no-redeclare */ | ||
if (grammar.hasOwnProperty(name) && !invisibles[name]) { | ||
if (name === `rest`) { | ||
addInvisibles(grammar[`rest`]) | ||
} else { | ||
handleToken(grammar, name) | ||
} | ||
} | ||
} | ||
} | ||
|
||
Prism.hooks.add(`before-highlight`, function(env) { | ||
addInvisibles(env.grammar) | ||
}) | ||
// prism-show-invisibles.js:end | ||
|
||
addInvisibles(env.grammar) | ||
} |