-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.ts
160 lines (143 loc) · 4.23 KB
/
index.ts
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import * as assert from 'assert';
import * as fs from 'fs';
import * as path from 'path';
import * as remark from 'remark';
import * as midas from 'remark-midas';
import * as treeSitter from 'remark-tree-sitter';
import * as highlight from 'remark-highlight.js';
import * as html from 'remark-html';
import * as codeExtra from 'remark-code-extra';
import { Options } from 'remark-code-extra/options';
import { MDASTCode } from 'remark-code-extra/types';
import { Element } from 'remark-code-extra/types/hast';
import {promisify} from 'util';
const readFile = promisify(fs.readFile);
const writeFile = promisify(fs.writeFile);
const FILES_DIR = path.join(path.dirname(__dirname), 'files');
function test(name: string, input: string, output: string, options: Options, use?: 'midas' | 'tree-sitter' | 'highlight.js') {
it(name, async () => {
const markdownPath = path.join(FILES_DIR, 'input', input + '.md');
const htmlPath = path.join(FILES_DIR, 'output', output + '.expected.html');
let processor = remark();
if (use === 'midas') processor = processor.use(midas);
if (use === 'tree-sitter')
processor = processor.use(treeSitter, {
grammarPackages: ['@atom-languages/language-typescript']
});
if (use === 'highlight.js') processor = processor.use(highlight);
processor = processor.use(codeExtra, options).use(html);
const markdownSource = await readFile(markdownPath, 'utf8');
const htmlResult = await promisify(processor.process)(markdownSource);
try {
const expected = await readFile(htmlPath, 'utf8');
assert.equal(htmlResult.contents, expected);
} catch (e) {
if (process.env.TEST_FIX === 'true') {
await writeFile(htmlPath, htmlResult.contents);
throw new Error(`Result Unexpected, written new contents to ${htmlPath}`);
} else {
throw e;
}
}
});
}
const element: Element = {
type: 'element',
tagName: 'span',
properties: {
className: ['some-thing']
},
children: []
};
describe('main tests', () => {
test('Skip all', 'basic', '001', {
transform: () => { /* undefined */ }
});
test('Skip all async', 'basic', '001', {
transform: async () => null
});
test('Skip specific language', 'basic', '002', {
transform: (node: MDASTCode) => node.lang === 'skipped' ? null : {}
});
test('Skip specific language async', 'basic', '002', {
transform: (node: MDASTCode) => node.lang === 'skipped' ? null : {}
});
test('No transformation', 'basic', '003', {transform: {
// No concrete transformation
}});
test('Add header', 'basic', '004', {transform: {
before: [element]
}
});
test('Add header func', 'basic', '004', {
transform: () => ({
before: [element]
})
});
test('Add header async', 'basic', '004', {
transform: async () => ({
before: [element]
})
});
test('Add multiple before', 'basic', '005', {
transform: {
before: [element, element]
}
});
test('Add footer', 'basic', '006', {
transform: {
after: [element]
}
});
test('Add footer func', 'basic', '006', {
transform: () => ({
after: [element]
})
});
test('Add footer async', 'basic', '006', {
transform: async () => ({
after: [element]
})
});
test('Add multiple after', 'basic', '007', {
transform: {
after: [element, element]
}
});
test('Add class to pre', 'basic', '008', {
transform: {
transform: node => {
(node.data as any).hChildren[0].properties = {className: ['foo']};
}
}
});
test('Add class to pre async', 'basic', '008', {
transform: {
transform: node => new Promise(resolve => {
(node.data as any).hChildren[0].properties = { className: ['foo'] };
setTimeout(resolve, 0);
})
}
});
test(
'Add midas with header async', 'css', '009', {
transform: async () => ({
before: [element]
})
},
'midas');
test(
'Add tree-sitter with footer async', 'typescript', '010', {
transform: async () => ({
after: [element]
})
},
'tree-sitter');
test(
'Add highlight.js with footer async', 'typescript', '011', {
transform: async () => ({
after: [element]
})
},
'highlight.js');
});