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