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