forked from nodejs/doc-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode-tabs.test.mjs
More file actions
77 lines (58 loc) · 1.52 KB
/
code-tabs.test.mjs
File metadata and controls
77 lines (58 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
'use strict';
import { strictEqual } from 'node:assert';
import { describe, it } from 'node:test';
import remarkParse from 'remark-parse';
import remarkRehype from 'remark-rehype';
import { unified } from 'unified';
import { visit } from 'unist-util-visit';
import codeTabs from '../code-tabs.mjs';
function process(markdown) {
const processor = unified().use(remarkParse).use(remarkRehype).use(codeTabs);
return processor.run(processor.parse(markdown));
}
function collectCodeMeta(tree) {
const meta = [];
visit(tree, 'element', node => {
if (node.tagName === 'code') {
meta.push(node.data?.meta ?? null);
}
});
return meta;
}
describe('codeTabs', () => {
it('assigns display names to consecutive blocks with the same language', async () => {
const tree = await process(`
\`\`\`js
console.log('one');
\`\`\`
\`\`\`js
console.log('two');
\`\`\`
`);
const meta = collectCodeMeta(tree);
strictEqual(meta[0], 'displayName="(1)"');
strictEqual(meta[1], 'displayName="(2)"');
});
it('does not modify blocks when languages are different', async () => {
const tree = await process(`
\`\`\`js
console.log('hello');
\`\`\`
\`\`\`python
print('hello')
\`\`\`
`);
const meta = collectCodeMeta(tree);
strictEqual(meta[0], null);
strictEqual(meta[1], null);
});
it('does not modify a single code block', async () => {
const tree = await process(`
\`\`\`js
console.log('hello');
\`\`\`
`);
const meta = collectCodeMeta(tree);
strictEqual(meta[0], null);
});
});