Skip to content

Commit 05bcad5

Browse files
authored
Merge pull request #47 from andrewbranch/special-characters-in-language-names
Allow # and + in language names
2 parents 0972ff2 + 1a42998 commit 05bcad5

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed

src/parseCodeFenceHeader.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// @ts-check
2-
const identifierPattern = /[a-z0-9-_]/i;
2+
const identifierPattern = /[a-z0-9-_+#]/i;
33
const triviaPattern = /\s/;
44
const startOfNumberPattern = /[0-9-.]/;
55
const numberPattern = /[0-9-.e]/;
@@ -24,11 +24,19 @@ function parseCodeFenceHeader(input) {
2424
if (!isEnd() && current() !== '{') {
2525
languageName = parseIdentifier();
2626
}
27+
const languageNameEnd = pos;
2728
skipTrivia();
2829
if (!isEnd() && current() === '{') {
2930
options = parseObject();
3031
}
3132

33+
if (!isEnd()) {
34+
if (languageNameEnd === pos) {
35+
return fail(`Invalid character in language name: '${current()}'`);
36+
}
37+
return fail(`Unrecognized input: '${current()}'`);
38+
}
39+
3240
return { languageName, options };
3341

3442
function current() {

test/__snapshots__/gatsby-remark-vscode.test.js.snap

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,56 @@ color: #D4D4D4;
247247
}
248248
`;
249249
250+
exports[`included languages and themes includes special characters in language name 1`] = `
251+
Object {
252+
"children": Array [
253+
Object {
254+
"children": Array [],
255+
"type": "paragraph",
256+
},
257+
Object {
258+
"lang": "c++",
259+
"type": "html",
260+
"value": "<pre class=\\"default-dark vscode-highlight\\" data-language=\\"c++\\"><code class=\\"vscode-highlight-code\\"><span class=\\"vscode-highlight-line\\"><span class=\\"mtk4\\">const</span><span class=\\"mtk1\\"> x = </span><span class=\\"mtk7\\">3</span><span class=\\"mtk1\\">;</span></span>
261+
<span class=\\"vscode-highlight-line\\"><span class=\\"mtk3\\">// Comment</span></span></code></pre>",
262+
},
263+
Object {
264+
"children": Array [],
265+
"type": "paragraph",
266+
},
267+
Object {
268+
"type": "html",
269+
"value": "<style class=\\"vscode-highlight-styles\\">.default-dark {
270+
background-color: #1E1E1E;
271+
color: #D4D4D4;
272+
}
273+
274+
.default-dark .mtk1 { color: #D4D4D4; }
275+
.default-dark .mtk2 { color: #1E1E1E; }
276+
.default-dark .mtk3 { color: #6A9955; }
277+
.default-dark .mtk4 { color: #569CD6; }
278+
.default-dark .mtk5 { color: #D16969; }
279+
.default-dark .mtk6 { color: #D7BA7D; }
280+
.default-dark .mtk7 { color: #B5CEA8; }
281+
.default-dark .mtk8 { color: #CE9178; }
282+
.default-dark .mtk9 { color: #646695; }
283+
.default-dark .mtk10 { color: #4EC9B0; }
284+
.default-dark .mtk11 { color: #DCDCAA; }
285+
.default-dark .mtk12 { color: #9CDCFE; }
286+
.default-dark .mtk13 { color: #000080; }
287+
.default-dark .mtk14 { color: #F44747; }
288+
.default-dark .mtk15 { color: #C586C0; }
289+
.default-dark .mtk16 { color: #6796E6; }
290+
.default-dark .mtk17 { color: #808080; }
291+
.default-dark .mtki { font-style: italic; }
292+
.default-dark .mtkb { font-weight: bold; }
293+
.default-dark .mtku { text-decoration: underline; text-underline-position: under; }</style>",
294+
},
295+
],
296+
"type": "root",
297+
}
298+
`;
299+
250300
exports[`included languages and themes partially works if an embedded grammar is missing 1`] = `
251301
Object {
252302
"children": Array [

test/gatsby-remark-vscode.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ describe('included languages and themes', () => {
117117
it('can use a custom language alias', async () => {
118118
return testSnapshot({ languageAliases: { 'java-scripty': 'js' } }, createMarkdownAST('java-scripty'));
119119
});
120+
121+
it('includes special characters in language name', async () => {
122+
return testSnapshot({}, createMarkdownAST('c++'));
123+
});
120124
});
121125

122126
describe('extension downloading', () => {

test/parseCodeFenceHeader.test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const parse = require('../src/parseCodeFenceHeader');
44
describe('parseCodeFenceHeader', () => {
55
it('parses language name without options', () => {
66
expect(parse('jsx')).toEqual({ languageName: 'jsx', options: {} });
7+
expect(parse('c++')).toEqual({ languageName: 'c++', options: {} });
78
});
89

910
it('parses empty options', () => {
@@ -52,5 +53,7 @@ describe('parseCodeFenceHeader', () => {
5253
expect(() => parse('jsx{ a: boo }')).toThrowError(/unrecognized input 'boo'/i);
5354
expect(() => parse('jsx{ : }')).toThrowError(/expected identifier/i);
5455
expect(() => parse('jsx{ a: "')).toThrowError(/unexpected end of input/i);
56+
expect(() => parse('c%')).toThrowError(/invalid character in language name.+?%/i);
57+
expect(() => parse('c %')).toThrowError(/unrecognized input.+?%/i);
5558
});
5659
});

0 commit comments

Comments
 (0)