Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/util/node/test/markdown.spec.ts
13401 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import assert from 'assert';
7
import { suite, test } from 'vitest';
8
import { createFilepathRegexp, extractCodeBlocks, mdCodeBlockLangToLanguageId } from '../../common/markdown';
9
10
suite('markdown', () => {
11
suite('extractCodeBlocks', () => {
12
test('should extract single code block', () => {
13
{
14
const result = extractCodeBlocks([
15
'```',
16
'single',
17
'```',
18
].join('\n'));
19
assert.strictEqual(result.length, 1);
20
assert.deepStrictEqual(result[0].code, 'single');
21
}
22
{
23
const result = extractCodeBlocks([
24
'```ts',
25
'single',
26
'```',
27
].join('\n'));
28
assert.strictEqual(result.length, 1);
29
assert.deepStrictEqual(result[0].code, 'single');
30
assert.deepStrictEqual(result[0].language, 'ts');
31
}
32
});
33
34
test('should extract multiple code blocks', () => {
35
const result = extractCodeBlocks([
36
'```',
37
'one',
38
'```',
39
'',
40
'code',
41
'',
42
'```php',
43
'two',
44
'```',
45
].join('\n'));
46
assert.strictEqual(result.length, 2);
47
assert.deepStrictEqual(result[0].code, 'one');
48
assert.deepStrictEqual(result[0].language, '');
49
50
assert.deepStrictEqual(result[1].code, 'two');
51
assert.deepStrictEqual(result[1].language, 'php');
52
});
53
54
test('should detect nested code blocks', () => {
55
const result = extractCodeBlocks([
56
'```',
57
'one',
58
'```',
59
'',
60
'- code',
61
' ',
62
' ```php',
63
' two',
64
' ```',
65
].join('\n'));
66
assert.strictEqual(result.length, 2);
67
assert.deepStrictEqual(result[0].code, 'one');
68
assert.deepStrictEqual(result[0].language, '');
69
70
assert.deepStrictEqual(result[1].code, 'two');
71
assert.deepStrictEqual(result[1].language, 'php');
72
});
73
});
74
75
suite('createFilepathRegexp', () => {
76
test('should match filepath comment with //', () => {
77
const regexp = createFilepathRegexp('typescript');
78
const result = regexp.exec('// filepath: /path/to/file');
79
assert.ok(result);
80
assert.strictEqual(result[1], '/path/to/file');
81
});
82
83
test('should match filepath comment with // with newline', () => {
84
const regexp = createFilepathRegexp('typescript');
85
const result = regexp.exec('// filepath: /path/to/file\n');
86
assert.ok(result);
87
assert.strictEqual(result[1], '/path/to/file');
88
});
89
90
test('should match filepath comment with // with newline \r\n', () => {
91
const regexp = createFilepathRegexp('typescript');
92
const result = regexp.exec('// filepath: /path/to/file\r\n');
93
assert.ok(result);
94
assert.strictEqual(result[1], '/path/to/file');
95
});
96
97
test('should match filepath comment with #', () => {
98
const regexp = createFilepathRegexp('python');
99
const result = regexp.exec('# filepath: /path/to/file');
100
assert.ok(result);
101
assert.strictEqual(result[1], '/path/to/file');
102
});
103
104
test('should match filepath comment with <!--', () => {
105
const regexp = createFilepathRegexp('html');
106
const result = regexp.exec('<!-- filepath: /path/to/file -->');
107
assert.ok(result);
108
assert.strictEqual(result[1], '/path/to/file');
109
});
110
111
test('should match filepath comment with <!-- but no -->', () => {
112
const regexp = createFilepathRegexp('html');
113
const result = regexp.exec('<!-- filepath: /path/to/file');
114
assert.ok(result);
115
assert.strictEqual(result[1], '/path/to/file');
116
});
117
118
test('should match filepath comment with <!-- and spaces in path', () => {
119
const regexp = createFilepathRegexp('html');
120
const result = regexp.exec('<!-- filepath: /path/to/file with spaces -->');
121
assert.ok(result);
122
assert.strictEqual(result[1], '/path/to/file with spaces');
123
});
124
125
test('should match filepath comment with <!-- and spaces in path no spaces at end', () => {
126
const regexp = createFilepathRegexp('html');
127
const result = regexp.exec('<!-- filepath: /path/to/file with spaces-->');
128
assert.ok(result);
129
assert.strictEqual(result[1], '/path/to/file with spaces');
130
});
131
132
test('should match filepath comment with <!-- and spaces in path no spaces at end with newline', () => {
133
const regexp = createFilepathRegexp('html');
134
const result = regexp.exec('<!-- filepath: /path/to/file with spaces-->\n');
135
assert.ok(result);
136
assert.strictEqual(result[1], '/path/to/file with spaces');
137
});
138
139
test('should match alternative BAT line comments', () => {
140
const regexp = createFilepathRegexp('bat');
141
const result = regexp.exec(':: filepath: /path/to/file');
142
assert.ok(result);
143
assert.strictEqual(result[1], '/path/to/file');
144
});
145
146
test('should match BAT line comments', () => {
147
const regexp = createFilepathRegexp('bat');
148
const result = regexp.exec('REM filepath: /path/to/file');
149
assert.ok(result);
150
assert.strictEqual(result[1], '/path/to/file');
151
});
152
153
test('should always match #', () => {
154
const regexp = createFilepathRegexp('html');
155
const result = regexp.exec('# filepath: /path/to/file');
156
assert.ok(result);
157
assert.strictEqual(result[1], '/path/to/file');
158
});
159
160
test('should always match //', () => {
161
const regexp = createFilepathRegexp('html');
162
const result = regexp.exec('// filepath: /path/to/file');
163
assert.ok(result);
164
assert.strictEqual(result[1], '/path/to/file');
165
});
166
167
test('should accept extra whitespaces', () => {
168
const regexp = createFilepathRegexp('html');
169
const result = regexp.exec(' // filepath:/path/to/file ');
170
assert.ok(result);
171
assert.strictEqual(result[1], '/path/to/file');
172
});
173
174
test('should accept extra whitespaces in path', () => {
175
const regexp = createFilepathRegexp('html');
176
const result = regexp.exec(' // filepath:/path/to/file with spaces.py ');
177
assert.ok(result);
178
assert.strictEqual(result[1], '/path/to/file with spaces.py');
179
});
180
181
test('should accept extra whitespaces in path and newline', () => {
182
const regexp = createFilepathRegexp('html');
183
const result = regexp.exec(' // filepath:/path/to/file with spaces.py \n');
184
assert.ok(result);
185
assert.strictEqual(result[1], '/path/to/file with spaces.py');
186
});
187
188
test('should accept empty language', () => {
189
const regexp = createFilepathRegexp();
190
const result = regexp.exec(' // filepath: /path/to/file ');
191
assert.ok(result);
192
assert.strictEqual(result[1], '/path/to/file');
193
});
194
});
195
196
suite('mdCodeBlockLangToLanguageId', () => {
197
test('ts is typescript', () => {
198
const result = mdCodeBlockLangToLanguageId('ts');
199
assert.strictEqual(result, 'typescript');
200
});
201
202
test('tsreact is typescriptreact', () => {
203
const result = mdCodeBlockLangToLanguageId('tsx');
204
assert.strictEqual(result, 'typescriptreact');
205
});
206
207
test('python is python', () => {
208
const result = mdCodeBlockLangToLanguageId('python');
209
assert.strictEqual(result, 'python');
210
});
211
212
});
213
});
214
215
216