Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/base/test/common/markdownString.test.ts
3296 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 { IMarkdownString, MarkdownString } from '../../common/htmlContent.js';
8
import { ensureNoDisposablesAreLeakedInTestSuite } from './utils.js';
9
import { URI } from '../../common/uri.js';
10
11
suite('MarkdownString', () => {
12
13
ensureNoDisposablesAreLeakedInTestSuite();
14
15
test('Escape leading whitespace', function () {
16
const mds = new MarkdownString();
17
mds.appendText('Hello\n Not a code block');
18
assert.strictEqual(mds.value, 'Hello\n\n    Not a code block');
19
});
20
21
test('MarkdownString.appendText doesn\'t escape quote #109040', function () {
22
const mds = new MarkdownString();
23
mds.appendText('> Text\n>More');
24
assert.strictEqual(mds.value, '\\> Text\n\n\\>More');
25
});
26
27
test('appendText', () => {
28
29
const mds = new MarkdownString();
30
mds.appendText('# foo\n*bar*');
31
32
assert.strictEqual(mds.value, '\\# foo\n\n\\*bar\\*');
33
});
34
35
test('appendLink', function () {
36
37
function assertLink(target: string, label: string, title: string | undefined, expected: string) {
38
const mds = new MarkdownString();
39
mds.appendLink(target, label, title);
40
assert.strictEqual(mds.value, expected);
41
}
42
43
assertLink(
44
'https://example.com\\()![](file:///Users/jrieken/Code/_samples/devfest/foo/img.png)', 'hello', undefined,
45
'[hello](https://example.com\\(\\)![](file:///Users/jrieken/Code/_samples/devfest/foo/img.png\\))'
46
);
47
assertLink(
48
'https://example.com', 'hello', 'title',
49
'[hello](https://example.com "title")'
50
);
51
assertLink(
52
'foo)', 'hello]', undefined,
53
'[hello\\]](foo\\))'
54
);
55
assertLink(
56
'foo\\)', 'hello]', undefined,
57
'[hello\\]](foo\\))'
58
);
59
assertLink(
60
'fo)o', 'hell]o', undefined,
61
'[hell\\]o](fo\\)o)'
62
);
63
assertLink(
64
'foo)', 'hello]', 'title"',
65
'[hello\\]](foo\\) "title\\"")'
66
);
67
});
68
69
test('lift', () => {
70
const dto: IMarkdownString = {
71
value: 'hello',
72
baseUri: URI.file('/foo/bar'),
73
supportThemeIcons: true,
74
isTrusted: true,
75
supportHtml: true,
76
uris: {
77
[URI.file('/foo/bar2').toString()]: URI.file('/foo/bar2'),
78
[URI.file('/foo/bar3').toString()]: URI.file('/foo/bar3')
79
}
80
};
81
const mds = MarkdownString.lift(dto);
82
assert.strictEqual(mds.value, dto.value);
83
assert.strictEqual(mds.baseUri?.toString(), dto.baseUri?.toString());
84
assert.strictEqual(mds.supportThemeIcons, dto.supportThemeIcons);
85
assert.strictEqual(mds.isTrusted, dto.isTrusted);
86
assert.strictEqual(mds.supportHtml, dto.supportHtml);
87
assert.deepStrictEqual(mds.uris, dto.uris);
88
});
89
90
test('lift returns new instance', () => {
91
const instance = new MarkdownString('hello');
92
const mds2 = MarkdownString.lift(instance).appendText('world');
93
assert.strictEqual(mds2.value, 'helloworld');
94
assert.strictEqual(instance.value, 'hello');
95
});
96
97
suite('appendCodeBlock', () => {
98
function assertCodeBlock(lang: string, code: string, result: string) {
99
const mds = new MarkdownString();
100
mds.appendCodeblock(lang, code);
101
assert.strictEqual(mds.value, result);
102
}
103
104
test('common cases', () => {
105
// no backticks
106
assertCodeBlock('ts', 'const a = 1;', `\n${[
107
'```ts',
108
'const a = 1;',
109
'```'
110
].join('\n')}\n`);
111
// backticks
112
assertCodeBlock('ts', 'const a = `1`;', `\n${[
113
'```ts',
114
'const a = `1`;',
115
'```'
116
].join('\n')}\n`);
117
});
118
119
// @see https://github.com/microsoft/vscode/issues/193746
120
test('escape fence', () => {
121
// fence in the first line
122
assertCodeBlock('md', '```\n```', `\n${[
123
'````md',
124
'```\n```',
125
'````'
126
].join('\n')}\n`);
127
// fence in the middle of code
128
assertCodeBlock('md', '\n\n```\n```', `\n${[
129
'````md',
130
'\n\n```\n```',
131
'````'
132
].join('\n')}\n`);
133
// longer fence at the end of code
134
assertCodeBlock('md', '```\n```\n````\n````', `\n${[
135
'`````md',
136
'```\n```\n````\n````',
137
'`````'
138
].join('\n')}\n`);
139
});
140
});
141
142
suite('ThemeIcons', () => {
143
144
suite('Support On', () => {
145
146
test('appendText', () => {
147
const mds = new MarkdownString(undefined, { supportThemeIcons: true });
148
mds.appendText('$(zap) $(not a theme icon) $(add)');
149
150
assert.strictEqual(mds.value, '\\\\$\\(zap\\) $\\(not a theme icon\\) \\\\$\\(add\\)');
151
});
152
153
test('appendMarkdown', () => {
154
const mds = new MarkdownString(undefined, { supportThemeIcons: true });
155
mds.appendMarkdown('$(zap) $(not a theme icon) $(add)');
156
157
assert.strictEqual(mds.value, '$(zap) $(not a theme icon) $(add)');
158
});
159
160
test('appendMarkdown with escaped icon', () => {
161
const mds = new MarkdownString(undefined, { supportThemeIcons: true });
162
mds.appendMarkdown('\\$(zap) $(not a theme icon) $(add)');
163
164
assert.strictEqual(mds.value, '\\$(zap) $(not a theme icon) $(add)');
165
});
166
167
});
168
169
suite('Support Off', () => {
170
171
test('appendText', () => {
172
const mds = new MarkdownString(undefined, { supportThemeIcons: false });
173
mds.appendText('$(zap) $(not a theme icon) $(add)');
174
175
assert.strictEqual(mds.value, '$\\(zap\\) $\\(not a theme icon\\) $\\(add\\)');
176
});
177
178
test('appendMarkdown', () => {
179
const mds = new MarkdownString(undefined, { supportThemeIcons: false });
180
mds.appendMarkdown('$(zap) $(not a theme icon) $(add)');
181
182
assert.strictEqual(mds.value, '$(zap) $(not a theme icon) $(add)');
183
});
184
185
test('appendMarkdown with escaped icon', () => {
186
const mds = new MarkdownString(undefined, { supportThemeIcons: true });
187
mds.appendMarkdown('\\$(zap) $(not a theme icon) $(add)');
188
189
assert.strictEqual(mds.value, '\\$(zap) $(not a theme icon) $(add)');
190
});
191
192
});
193
});
194
});
195
196