Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/base/common/htmlContent.ts
3291 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 { illegalArgument } from './errors.js';
7
import { escapeIcons } from './iconLabels.js';
8
import { Schemas } from './network.js';
9
import { isEqual } from './resources.js';
10
import { escapeRegExpCharacters } from './strings.js';
11
import { URI, UriComponents } from './uri.js';
12
13
export interface MarkdownStringTrustedOptions {
14
readonly enabledCommands: readonly string[];
15
}
16
17
export interface IMarkdownString {
18
readonly value: string;
19
readonly isTrusted?: boolean | MarkdownStringTrustedOptions;
20
readonly supportThemeIcons?: boolean;
21
readonly supportHtml?: boolean;
22
readonly baseUri?: UriComponents;
23
uris?: { [href: string]: UriComponents };
24
}
25
26
export const enum MarkdownStringTextNewlineStyle {
27
Paragraph = 0,
28
Break = 1,
29
}
30
31
export class MarkdownString implements IMarkdownString {
32
33
public value: string;
34
public isTrusted?: boolean | MarkdownStringTrustedOptions;
35
public supportThemeIcons?: boolean;
36
public supportHtml?: boolean;
37
public baseUri?: URI;
38
public uris?: { [href: string]: UriComponents } | undefined;
39
40
public static lift(dto: IMarkdownString): MarkdownString {
41
const markdownString = new MarkdownString(dto.value, dto);
42
markdownString.uris = dto.uris;
43
markdownString.baseUri = dto.baseUri ? URI.revive(dto.baseUri) : undefined;
44
return markdownString;
45
}
46
47
constructor(
48
value: string = '',
49
isTrustedOrOptions: boolean | { isTrusted?: boolean | MarkdownStringTrustedOptions; supportThemeIcons?: boolean; supportHtml?: boolean } = false,
50
) {
51
this.value = value;
52
if (typeof this.value !== 'string') {
53
throw illegalArgument('value');
54
}
55
56
if (typeof isTrustedOrOptions === 'boolean') {
57
this.isTrusted = isTrustedOrOptions;
58
this.supportThemeIcons = false;
59
this.supportHtml = false;
60
}
61
else {
62
this.isTrusted = isTrustedOrOptions.isTrusted ?? undefined;
63
this.supportThemeIcons = isTrustedOrOptions.supportThemeIcons ?? false;
64
this.supportHtml = isTrustedOrOptions.supportHtml ?? false;
65
}
66
}
67
68
appendText(value: string, newlineStyle: MarkdownStringTextNewlineStyle = MarkdownStringTextNewlineStyle.Paragraph): MarkdownString {
69
this.value += escapeMarkdownSyntaxTokens(this.supportThemeIcons ? escapeIcons(value) : value) // CodeQL [SM02383] The Markdown is fully sanitized after being rendered.
70
.replace(/([ \t]+)/g, (_match, g1) => ' '.repeat(g1.length)) // CodeQL [SM02383] The Markdown is fully sanitized after being rendered.
71
.replace(/\>/gm, '\\>') // CodeQL [SM02383] The Markdown is fully sanitized after being rendered.
72
.replace(/\n/g, newlineStyle === MarkdownStringTextNewlineStyle.Break ? '\\\n' : '\n\n'); // CodeQL [SM02383] The Markdown is fully sanitized after being rendered.
73
74
return this;
75
}
76
77
appendMarkdown(value: string): MarkdownString {
78
this.value += value;
79
return this;
80
}
81
82
appendCodeblock(langId: string, code: string): MarkdownString {
83
this.value += `\n${appendEscapedMarkdownCodeBlockFence(code, langId)}\n`;
84
return this;
85
}
86
87
appendLink(target: URI | string, label: string, title?: string): MarkdownString {
88
this.value += '[';
89
this.value += this._escape(label, ']');
90
this.value += '](';
91
this.value += this._escape(String(target), ')');
92
if (title) {
93
this.value += ` "${this._escape(this._escape(title, '"'), ')')}"`;
94
}
95
this.value += ')';
96
return this;
97
}
98
99
private _escape(value: string, ch: string): string {
100
const r = new RegExp(escapeRegExpCharacters(ch), 'g');
101
return value.replace(r, (match, offset) => {
102
if (value.charAt(offset - 1) !== '\\') {
103
return `\\${match}`;
104
} else {
105
return match;
106
}
107
});
108
}
109
}
110
111
export function isEmptyMarkdownString(oneOrMany: IMarkdownString | IMarkdownString[] | null | undefined): boolean {
112
if (isMarkdownString(oneOrMany)) {
113
return !oneOrMany.value;
114
} else if (Array.isArray(oneOrMany)) {
115
return oneOrMany.every(isEmptyMarkdownString);
116
} else {
117
return true;
118
}
119
}
120
121
export function isMarkdownString(thing: unknown): thing is IMarkdownString {
122
if (thing instanceof MarkdownString) {
123
return true;
124
} else if (thing && typeof thing === 'object') {
125
return typeof (<IMarkdownString>thing).value === 'string'
126
&& (typeof (<IMarkdownString>thing).isTrusted === 'boolean' || typeof (<IMarkdownString>thing).isTrusted === 'object' || (<IMarkdownString>thing).isTrusted === undefined)
127
&& (typeof (<IMarkdownString>thing).supportThemeIcons === 'boolean' || (<IMarkdownString>thing).supportThemeIcons === undefined);
128
}
129
return false;
130
}
131
132
export function markdownStringEqual(a: IMarkdownString, b: IMarkdownString): boolean {
133
if (a === b) {
134
return true;
135
} else if (!a || !b) {
136
return false;
137
} else {
138
return a.value === b.value
139
&& a.isTrusted === b.isTrusted
140
&& a.supportThemeIcons === b.supportThemeIcons
141
&& a.supportHtml === b.supportHtml
142
&& (a.baseUri === b.baseUri || !!a.baseUri && !!b.baseUri && isEqual(URI.from(a.baseUri), URI.from(b.baseUri)));
143
}
144
}
145
146
export function escapeMarkdownSyntaxTokens(text: string): string {
147
// escape markdown syntax tokens: http://daringfireball.net/projects/markdown/syntax#backslash
148
return text.replace(/[\\`*_{}[\]()#+\-!~]/g, '\\$&'); // CodeQL [SM02383] Backslash is escaped in the character class
149
}
150
151
/**
152
* @see https://github.com/microsoft/vscode/issues/193746
153
*/
154
export function appendEscapedMarkdownCodeBlockFence(code: string, langId: string) {
155
const longestFenceLength =
156
code.match(/^`+/gm)?.reduce((a, b) => (a.length > b.length ? a : b)).length ??
157
0;
158
const desiredFenceLength =
159
longestFenceLength >= 3 ? longestFenceLength + 1 : 3;
160
161
// the markdown result
162
return [
163
`${'`'.repeat(desiredFenceLength)}${langId}`,
164
code,
165
`${'`'.repeat(desiredFenceLength)}`,
166
].join('\n');
167
}
168
169
export function escapeDoubleQuotes(input: string) {
170
return input.replace(/"/g, '&quot;');
171
}
172
173
export function removeMarkdownEscapes(text: string): string {
174
if (!text) {
175
return text;
176
}
177
return text.replace(/\\([\\`*_{}[\]()#+\-.!~])/g, '$1');
178
}
179
180
export function parseHrefAndDimensions(href: string): { href: string; dimensions: string[] } {
181
const dimensions: string[] = [];
182
const splitted = href.split('|').map(s => s.trim());
183
href = splitted[0];
184
const parameters = splitted[1];
185
if (parameters) {
186
const heightFromParams = /height=(\d+)/.exec(parameters);
187
const widthFromParams = /width=(\d+)/.exec(parameters);
188
const height = heightFromParams ? heightFromParams[1] : '';
189
const width = widthFromParams ? widthFromParams[1] : '';
190
const widthIsFinite = isFinite(parseInt(width));
191
const heightIsFinite = isFinite(parseInt(height));
192
if (widthIsFinite) {
193
dimensions.push(`width="${width}"`);
194
}
195
if (heightIsFinite) {
196
dimensions.push(`height="${height}"`);
197
}
198
}
199
return { href, dimensions };
200
}
201
202
export function markdownCommandLink(command: { title: string; id: string; arguments?: unknown[] }, escapeTokens = true): string {
203
const uri = URI.from({
204
scheme: Schemas.command,
205
path: command.id,
206
query: command.arguments?.length ? encodeURIComponent(JSON.stringify(command.arguments)) : undefined,
207
}).toString();
208
209
return `[${escapeTokens ? escapeMarkdownSyntaxTokens(command.title) : command.title}](${uri})`;
210
}
211
212