Skip to content

Commit ba43231

Browse files
committed
Fix nested code fences
1 parent ff4e599 commit ba43231

File tree

5 files changed

+71
-6
lines changed

5 files changed

+71
-6
lines changed

package-lock.json

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"lodash.uniq": "^4.5.0",
5252
"oniguruma": "^7.0.2",
5353
"plist": "^3.0.1",
54+
"unist-util-visit": "^1.4.0",
5455
"vscode-textmate": "^4.1.0"
5556
},
5657
"jest": {

src/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// @ts-check
22
const fs = require('fs');
33
const path = require('path');
4+
const visit = require('unist-util-visit');
45
const escapeHTML = require('lodash.escape');
56
const lineHighlighting = require('./lineHighlighting');
67
const createGetRegistry = require('./createGetRegistry');
@@ -142,9 +143,12 @@ function createPlugin() {
142143
) {
143144
/** @type {Record<string, string>} */
144145
const stylesheets = {};
146+
const nodes = [];
147+
visit(markdownAST, 'code', node => {
148+
nodes.push(node);
149+
});
145150

146-
for (const node of markdownAST.children) {
147-
if (node.type !== 'code') continue;
151+
for (const node of nodes) {
148152
/** @type {string} */
149153
const text = node.value || (node.children && node.children[0] && node.children[0].value);
150154
if (!text) continue;

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ exports[`can replace a color value 1`] = `
44
Object {
55
"children": Array [
66
Object {
7+
"children": Array [],
78
"type": "paragraph",
89
},
910
Object {
@@ -13,6 +14,7 @@ Object {
1314
<span class=\\"vscode-highlight-line\\"><span class=\\"mtk3\\">// Comment</span></span></code></pre>",
1415
},
1516
Object {
17+
"children": Array [],
1618
"type": "paragraph",
1719
},
1820
Object {
@@ -44,13 +46,15 @@ color: #D4D4D4;
4446
.default-dark .mtku { text-decoration: underline; text-underline-position: under; }</style>",
4547
},
4648
],
49+
"type": "root",
4750
}
4851
`;
4952
5053
exports[`extension downloading can download an extension to resolve a grammar 1`] = `
5154
Object {
5255
"children": Array [
5356
Object {
57+
"children": Array [],
5458
"type": "paragraph",
5559
},
5660
Object {
@@ -60,6 +64,7 @@ Object {
6064
<span class=\\"vscode-highlight-line\\"><span class=\\"mtk1\\">// Comment</span></span></code></pre>",
6165
},
6266
Object {
67+
"children": Array [],
6368
"type": "paragraph",
6469
},
6570
Object {
@@ -91,13 +96,15 @@ color: #D4D4D4;
9196
.default-dark .mtku { text-decoration: underline; text-underline-position: under; }</style>",
9297
},
9398
],
99+
"type": "root",
94100
}
95101
`;
96102
97103
exports[`extension downloading can download an extension to resolve a theme 1`] = `
98104
Object {
99105
"children": Array [
100106
Object {
107+
"children": Array [],
101108
"type": "paragraph",
102109
},
103110
Object {
@@ -107,6 +114,7 @@ Object {
107114
<span class=\\"vscode-highlight-line\\"><span class=\\"mtk3\\">// Comment</span></span></code></pre>",
108115
},
109116
Object {
117+
"children": Array [],
110118
"type": "paragraph",
111119
},
112120
Object {
@@ -135,13 +143,15 @@ Object {
135143
.custom-theme .mtku { text-decoration: underline; text-underline-position: under; }</style>",
136144
},
137145
],
146+
"type": "root",
138147
}
139148
`;
140149
141150
exports[`included languages and themes can use a custom language alias 1`] = `
142151
Object {
143152
"children": Array [
144153
Object {
154+
"children": Array [],
145155
"type": "paragraph",
146156
},
147157
Object {
@@ -151,6 +161,7 @@ Object {
151161
<span class=\\"vscode-highlight-line\\"><span class=\\"mtk3\\">// Comment</span></span></code></pre>",
152162
},
153163
Object {
164+
"children": Array [],
154165
"type": "paragraph",
155166
},
156167
Object {
@@ -182,13 +193,15 @@ color: #D4D4D4;
182193
.default-dark .mtku { text-decoration: underline; text-underline-position: under; }</style>",
183194
},
184195
],
196+
"type": "root",
185197
}
186198
`;
187199
188200
exports[`included languages and themes can use a standard language alias 1`] = `
189201
Object {
190202
"children": Array [
191203
Object {
204+
"children": Array [],
192205
"type": "paragraph",
193206
},
194207
Object {
@@ -198,6 +211,7 @@ Object {
198211
<span class=\\"vscode-highlight-line\\"><span class=\\"mtk3\\">// Comment</span></span></code></pre>",
199212
},
200213
Object {
214+
"children": Array [],
201215
"type": "paragraph",
202216
},
203217
Object {
@@ -229,13 +243,15 @@ color: #D4D4D4;
229243
.default-dark .mtku { text-decoration: underline; text-underline-position: under; }</style>",
230244
},
231245
],
246+
"type": "root",
232247
}
233248
`;
234249
235250
exports[`included languages and themes partially works if an embedded grammar is missing 1`] = `
236251
Object {
237252
"children": Array [
238253
Object {
254+
"children": Array [],
239255
"type": "paragraph",
240256
},
241257
Object {
@@ -245,6 +261,7 @@ Object {
245261
<span class=\\"vscode-highlight-line\\">// Comment</span></code></pre>",
246262
},
247263
Object {
264+
"children": Array [],
248265
"type": "paragraph",
249266
},
250267
Object {
@@ -276,13 +293,15 @@ color: #D4D4D4;
276293
.default-dark .mtku { text-decoration: underline; text-underline-position: under; }</style>",
277294
},
278295
],
296+
"type": "root",
279297
}
280298
`;
281299
282300
exports[`included languages and themes works with default options 1`] = `
283301
Object {
284302
"children": Array [
285303
Object {
304+
"children": Array [],
286305
"type": "paragraph",
287306
},
288307
Object {
@@ -292,6 +311,7 @@ Object {
292311
<span class=\\"vscode-highlight-line\\"><span class=\\"mtk3\\">// Comment</span></span></code></pre>",
293312
},
294313
Object {
314+
"children": Array [],
295315
"type": "paragraph",
296316
},
297317
Object {
@@ -323,13 +343,15 @@ color: #D4D4D4;
323343
.default-dark .mtku { text-decoration: underline; text-underline-position: under; }</style>",
324344
},
325345
],
346+
"type": "root",
326347
}
327348
`;
328349
329350
exports[`included languages and themes works without highlighting if a code fence has no language 1`] = `
330351
Object {
331352
"children": Array [
332353
Object {
354+
"children": Array [],
333355
"type": "paragraph",
334356
},
335357
Object {
@@ -339,6 +361,7 @@ Object {
339361
<span class=\\"vscode-highlight-line\\">// Comment</span></code></pre>",
340362
},
341363
Object {
364+
"children": Array [],
342365
"type": "paragraph",
343366
},
344367
Object {
@@ -349,13 +372,15 @@ color: #D4D4D4;
349372
}</style>",
350373
},
351374
],
375+
"type": "root",
352376
}
353377
`;
354378
355379
exports[`included languages and themes works without highlighting if language is not recognized 1`] = `
356380
Object {
357381
"children": Array [
358382
Object {
383+
"children": Array [],
359384
"type": "paragraph",
360385
},
361386
Object {
@@ -365,6 +390,7 @@ Object {
365390
<span class=\\"vscode-highlight-line\\">// Comment</span></code></pre>",
366391
},
367392
Object {
393+
"children": Array [],
368394
"type": "paragraph",
369395
},
370396
Object {
@@ -375,13 +401,15 @@ color: #D4D4D4;
375401
}</style>",
376402
},
377403
],
404+
"type": "root",
378405
}
379406
`;
380407
381408
exports[`prefers-color-scheme supports prefers-color-scheme via an object for \`colorTheme\` 1`] = `
382409
Object {
383410
"children": Array [
384411
Object {
412+
"children": Array [],
385413
"type": "paragraph",
386414
},
387415
Object {
@@ -391,6 +419,7 @@ Object {
391419
<span class=\\"vscode-highlight-line\\"><span class=\\"mtk3 mtki\\">// Comment</span></span></code></pre>",
392420
},
393421
Object {
422+
"children": Array [],
394423
"type": "paragraph",
395424
},
396425
Object {
@@ -490,13 +519,15 @@ background-color: #F5F5F5;
490519
}</style>",
491520
},
492521
],
522+
"type": "root",
493523
}
494524
`;
495525
496526
exports[`prefers-color-scheme supports prefers-color-scheme with dynamically selected themes 1`] = `
497527
Object {
498528
"children": Array [
499529
Object {
530+
"children": Array [],
500531
"type": "paragraph",
501532
},
502533
Object {
@@ -506,9 +537,11 @@ Object {
506537
<span class=\\"vscode-highlight-line\\"><span class=\\"mtk3 mtki\\">// Comment</span></span></code></pre>",
507538
},
508539
Object {
540+
"children": Array [],
509541
"type": "paragraph",
510542
},
511543
Object {
544+
"children": Array [],
512545
"type": "paragraph",
513546
},
514547
Object {
@@ -518,6 +551,7 @@ Object {
518551
<span class=\\"vscode-highlight-line\\"><span class=\\"mtk3 mtki\\">// Comment</span></span></code></pre>",
519552
},
520553
Object {
554+
"children": Array [],
521555
"type": "paragraph",
522556
},
523557
Object {
@@ -629,13 +663,15 @@ color: #f8f8f2;
629663
}</style>",
630664
},
631665
],
666+
"type": "root",
632667
}
633668
`;
634669
635670
exports[`sets highlighted line class names 1`] = `
636671
Object {
637672
"children": Array [
638673
Object {
674+
"children": Array [],
639675
"type": "paragraph",
640676
},
641677
Object {
@@ -648,6 +684,7 @@ Object {
648684
<span class=\\"vscode-highlight-line\\"><span class=\\"mtk3\\">// 5</span></span></code></pre>",
649685
},
650686
Object {
687+
"children": Array [],
651688
"type": "paragraph",
652689
},
653690
Object {
@@ -679,5 +716,6 @@ color: #D4D4D4;
679716
.default-dark .mtku { text-decoration: underline; text-underline-position: under; }</style>",
680717
},
681718
],
719+
"type": "root",
682720
}
683721
`;

test/gatsby-remark-vscode.test.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@ function createCache() {
3434

3535
function createMarkdownAST(lang = 'js', value = 'const x = 3;\n// Comment') {
3636
return {
37+
type: 'root',
3738
children: [
38-
{ type: 'paragraph' },
39+
{ type: 'paragraph', children: [] },
3940
{ type: 'code', lang, value },
40-
{ type: 'paragraph' },
41+
{ type: 'paragraph', children: [] },
4142
],
4243
};
4344
}
@@ -79,7 +80,7 @@ describe('included languages and themes', () => {
7980

8081
it('only adds theme CSS once', async () => {
8182
const plugin = createPlugin();
82-
const markdownAST = { children: [...createMarkdownAST().children, ...createMarkdownAST().children] };
83+
const markdownAST = { type: 'root', children: [...createMarkdownAST().children, ...createMarkdownAST().children] };
8384
const cache = createCache();
8485
await plugin({ markdownAST, markdownNode, cache }, defaultOptions);
8586
expect(markdownAST.children.filter(node => node.type === 'html')).toHaveLength(3);
@@ -199,7 +200,7 @@ describe('prefers-color-scheme', () => {
199200
});
200201

201202
it('supports prefers-color-scheme with dynamically selected themes', async () => {
202-
const markdownAST = { children: [...createMarkdownAST().children, ...createMarkdownAST().children] };
203+
const markdownAST = { type: 'root', children: [...createMarkdownAST().children, ...createMarkdownAST().children] };
203204
let i = 0;
204205
const darkThemes = ['Dark+ (default dark)', 'Monokai'];
205206
return testSnapshot({

0 commit comments

Comments
 (0)