-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix
gatsby-remark-prismjs
highlight bug (#1921)
* prism: add scripts to download language dependencies from github * Fix loading prism language If language has dependencies, should load dependencies first. e.g. cpp depends on c, should load c first.
- Loading branch information
1 parent
759e827
commit ae18f24
Showing
8 changed files
with
723 additions
and
11 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
packages/gatsby-remark-prismjs/scripts/get-prism-language-dependencies.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,63 @@ | ||
const https = require(`https`) | ||
const execSync = require(`child_process`).execSync | ||
const fs = require(`fs`) | ||
const path = require(`path`) | ||
|
||
const fileSavePath = path.resolve(__dirname, `../src/prism-language-dependencies.js`) | ||
|
||
function getVersion() { | ||
const prismInfo = JSON.parse(execSync(`npm ls prismjs --json`)) | ||
return prismInfo.dependencies.prismjs.version | ||
} | ||
|
||
function processData(data, url) { | ||
// `components.js`: | ||
// var components = { | ||
// "core": { ... }, | ||
// "languages": { ... }, | ||
// ... | ||
// } | ||
eval(data) | ||
if (typeof components === `undefined`) { | ||
throw new Error(`The content structure of \`components.js\` seems changed.`) | ||
} | ||
|
||
// eslint-disable-next-line no-undef | ||
const languages = components.languages | ||
const content = `// From ${JSON.stringify(url)} | ||
module.exports = ${JSON.stringify(languages, null, 2)} | ||
` | ||
|
||
fs.writeFileSync(fileSavePath, content, `utf8`) | ||
} | ||
|
||
function requestData() { | ||
const version = getVersion() | ||
const url = `https://raw.githubusercontent.com/PrismJS/prism/v${version}/components.js` | ||
|
||
https | ||
.get(url, res => { | ||
if (res.statusCode !== 200) { | ||
throw new Error(`Request Failed.\nRequest URL: ${url}\nStatus Code: ${res.statusCode}`) | ||
} | ||
|
||
res.setEncoding(`utf8`) | ||
let rawData = `` | ||
res.on(`data`, chunk => { | ||
rawData += chunk | ||
}) | ||
|
||
res.on(`end`, () => { | ||
try { | ||
processData(rawData, url) | ||
} catch (e) { | ||
console.error(e.message) | ||
} | ||
}) | ||
}) | ||
.on(`error`, e => { | ||
console.error(e.message) | ||
}) | ||
} | ||
|
||
requestData() |
11 changes: 10 additions & 1 deletion
11
packages/gatsby-remark-prismjs/src/__tests__/__snapshots__/highlight-code.js.snap
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
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
44 changes: 44 additions & 0 deletions
44
packages/gatsby-remark-prismjs/src/__tests__/load-prism-language.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,44 @@ | ||
const loadPrismLanguage = require(`../load-prism-language`) | ||
|
||
describe(`load prism language`, () => { | ||
afterEach(() => { | ||
jest.resetModules() | ||
}) | ||
|
||
it(`throw if language not support`, () => { | ||
expect(() => loadPrismLanguage(`imnotalanguage`)).toThrow( | ||
`Prism doesn't support language 'imnotalanguage'.` | ||
) | ||
}) | ||
|
||
it(`load supported language`, () => { | ||
const language = `c` | ||
const Prism = require(`prismjs`) | ||
|
||
const languagesBeforeLoaded = Object.keys(Prism.languages) | ||
expect(Prism.languages).not.toHaveProperty(language) | ||
|
||
loadPrismLanguage(language) | ||
|
||
const languagesAfterLoaded = Object.keys(Prism.languages) | ||
expect(Prism.languages).toHaveProperty(language) | ||
expect(languagesAfterLoaded.length).toBe(languagesBeforeLoaded.length + 1) | ||
}) | ||
|
||
it(`also load the required language`, () => { | ||
const language = `cpp` | ||
const requiredLanguage = `c` | ||
const Prism = require(`prismjs`) | ||
|
||
const languagesBeforeLoaded = Object.keys(Prism.languages) | ||
expect(Prism.languages).not.toHaveProperty(language) | ||
expect(Prism.languages).not.toHaveProperty(requiredLanguage) | ||
|
||
loadPrismLanguage(language) | ||
|
||
const languagesAfterLoaded = Object.keys(Prism.languages) | ||
expect(Prism.languages).toHaveProperty(language) | ||
expect(Prism.languages).toHaveProperty(requiredLanguage) | ||
expect(languagesAfterLoaded.length).toBe(languagesBeforeLoaded.length + 2) | ||
}) | ||
}) |
2 changes: 1 addition & 1 deletion
2
packages/gatsby-remark-prismjs/src/__tests__/parse-line-number-range.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
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
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,31 @@ | ||
const Prism = require(`prismjs`) | ||
|
||
const languageDependencies = require(`./prism-language-dependencies`) | ||
|
||
module.exports = function loadPrismLanguage(language) { | ||
if (Prism.languages[language]) { | ||
// Don't load already loaded language | ||
return | ||
} | ||
|
||
const languageData = languageDependencies[language] | ||
if (!languageData) { | ||
throw new Error(`Prism doesn't support language '${language}'.`) | ||
} | ||
|
||
if (languageData.option === `default`) { | ||
// Default language has already been loaded by Prism | ||
return | ||
} | ||
|
||
if (languageData.require) { | ||
// Load the required language first | ||
if (Array.isArray(languageData.require)) { | ||
languageData.require.forEach(loadPrismLanguage) | ||
} else { | ||
loadPrismLanguage(languageData.require) | ||
} | ||
} | ||
|
||
require(`prismjs/components/prism-${language}.js`) | ||
} |
Oops, something went wrong.