-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
Fix gatsby-remark-prismjs
highlight bug
#1921
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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() |
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`) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd like to isolate the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Eh, dunno. Not enough of a Jest expert to know how to do this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ooo… but now I am :-) https://facebook.github.io/jest/docs/en/configuration.html#resetmodules-boolean There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @KyleAMathews Works great! and it's ready to review now. |
||
|
||
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) | ||
}) | ||
}) |
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`) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@KyleAMathews Besides, I think these tests are some as:
gatsby/packages/gatsby-remark-prismjs/src/__tests__/parse-line-number-range.js
Lines 4 to 43 in ca12b4d
Why duplicate here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If there's duplicates, feel free to PR them away!