Skip to content

Commit 422f91d

Browse files
fix(gatsby-remark-prismjs): Update warnings to handle missing language (#13159)
* fix: Update warnings to handle missing language * Reverted change to class and added argument to control warnings * Keep option name consistent
1 parent d00ead5 commit 422f91d

File tree

4 files changed

+73
-18
lines changed

4 files changed

+73
-18
lines changed

packages/gatsby-remark-prismjs/src/__tests__/highlight-code.js

+18-3
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,30 @@ export default Counter
5656
})
5757

5858
describe(`with language-text`, () => {
59-
it(`escapes &, <, " elements #4597 and warns`, () => {
59+
it(`escapes &, <, " elements and warns`, () => {
60+
spyOn(console, `warn`)
61+
62+
const highlightCode = require(`../highlight-code`)
63+
const language = `text`
64+
const code = `<button />`
65+
expect(highlightCode(language, code, [], true)).toMatch(
66+
`&lt;button /&gt;`
67+
)
68+
expect(console.warn).toHaveBeenCalledWith(
69+
`code block language not specified in markdown.`,
70+
`applying generic code block`
71+
)
72+
})
73+
74+
it(`can warn about languages missing from inline code`, () => {
6075
spyOn(console, `warn`)
6176

6277
const highlightCode = require(`../highlight-code`)
6378
const language = `text`
6479
const code = `<button />`
6580
expect(highlightCode(language, code)).toMatch(`&lt;button /&gt;`)
6681
expect(console.warn).toHaveBeenCalledWith(
67-
`unable to find prism language 'text' for highlighting.`,
82+
`code block or inline code language not specified in markdown.`,
6883
`applying generic code block`
6984
)
7085
})
@@ -84,7 +99,7 @@ export default Counter
8499
expect(console.warn).toHaveBeenCalledTimes(2)
85100
expect(console.warn).toHaveBeenNthCalledWith(
86101
1,
87-
`unable to find prism language 'text' for highlighting.`,
102+
`code block or inline code language not specified in markdown.`,
88103
`applying generic code block`
89104
)
90105
expect(console.warn).toHaveBeenNthCalledWith(

packages/gatsby-remark-prismjs/src/__tests__/index.js

+30-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
const remark = require(`remark`)
2-
const plugin = require(`../index`)
2+
let plugin
33

44
describe(`remark prism plugin`, () => {
5+
beforeEach(() => {
6+
jest.resetModules()
7+
plugin = require(`../index`)
8+
})
9+
510
describe(`generates a <pre> tag`, () => {
611
it(`with class="language-*" prefix by default`, () => {
712
const code = `\`\`\`js\n// Fake\n\`\`\``
@@ -101,4 +106,28 @@ describe(`remark prism plugin`, () => {
101106
expect(markdownAST).toMatchSnapshot()
102107
})
103108
})
109+
110+
describe(`warnings`, () => {
111+
it(`warns if the language is not specified for a code block`, () => {
112+
spyOn(console, `warn`)
113+
const code = `\`\`\`\n// Fake\n\`\`\``
114+
const markdownAST = remark.parse(code)
115+
plugin({ markdownAST }, { noInlineHighlight: true })
116+
expect(console.warn).toHaveBeenCalledWith(
117+
`code block language not specified in markdown.`,
118+
`applying generic code block`
119+
)
120+
})
121+
122+
it(`gives a different warning if inline code can be highlighted`, () => {
123+
spyOn(console, `warn`)
124+
const code = `\`foo bar\``
125+
const markdownAST = remark.parse(code)
126+
plugin({ markdownAST })
127+
expect(console.warn).toHaveBeenCalledWith(
128+
`code block or inline code language not specified in markdown.`,
129+
`applying generic code block`
130+
)
131+
})
132+
})
104133
})

packages/gatsby-remark-prismjs/src/highlight-code.js

+24-13
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,37 @@ const loadPrismLanguage = require(`./load-prism-language`)
55
const handleDirectives = require(`./directives`)
66
const unsupportedLanguages = new Set()
77

8-
module.exports = (language, code, lineNumbersHighlight = []) => {
8+
module.exports = (
9+
language,
10+
code,
11+
lineNumbersHighlight = [],
12+
noInlineHighlight = false
13+
) => {
914
// (Try to) load languages on demand.
1015
if (!Prism.languages[language]) {
1116
try {
1217
loadPrismLanguage(language)
1318
} catch (e) {
1419
// Language wasn't loaded so let's bail.
15-
if (language === `none`) {
16-
return code // Don't escape if set to none.
17-
} else {
18-
const lang = language.toLowerCase()
19-
if (!unsupportedLanguages.has(lang)) {
20-
console.warn(
21-
`unable to find prism language '${lang}' for highlighting.`,
22-
`applying generic code block`
23-
)
24-
unsupportedLanguages.add(lang)
25-
}
26-
return _.escape(code)
20+
let message = null
21+
switch (language) {
22+
case `none`:
23+
return code // Don't escape if set to none.
24+
case `text`:
25+
message = noInlineHighlight
26+
? `code block language not specified in markdown.`
27+
: `code block or inline code language not specified in markdown.`
28+
break
29+
default:
30+
message = `unable to find prism language '${language}' for highlighting.`
2731
}
32+
33+
const lang = language.toLowerCase()
34+
if (!unsupportedLanguages.has(lang)) {
35+
console.warn(message, `applying generic code block`)
36+
unsupportedLanguages.add(lang)
37+
}
38+
return _.escape(code)
2839
}
2940
}
3041

packages/gatsby-remark-prismjs/src/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ module.exports = (
7070
+ `<div class="${highlightClassName}" data-language="${languageName}">`
7171
+ `<pre${numLinesStyle} class="${className}${numLinesClass}">`
7272
+ `<code class="${className}">`
73-
+ `${highlightCode(languageName, node.value, highlightLines)}`
73+
+ `${highlightCode(languageName, node.value, highlightLines, noInlineHighlight)}`
7474
+ `</code>`
7575
+ `${numLinesNumber}`
7676
+ `</pre>`

0 commit comments

Comments
 (0)