src/vs/base/common/htmlContent.ts
255 LOC · 199 covered · 56 uncovered · 47 ranges · 3818 concepts · 18 introducers · 1859 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
/*---------------------------------------------------------------------------------------------
htmlContent.ts ×20
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { illegalArgument } from './errors.js';
import { escapeIcons } from './iconLabels.js';
import { Schemas } from './network.js';
import { isEqual } from './resources.js';
import { escapeRegExpCharacters } from './strings.js';
import { URI, UriComponents } from './uri.js';
export interface MarkdownStringTrustedOptions {
readonly enabledCommands: readonly string[];
}
export interface IMarkdownString {
readonly value: string;
readonly isTrusted?: boolean | MarkdownStringTrustedOptions;
readonly supportThemeIcons?: boolean;
readonly supportHtml?: boolean;
/** @internal */
readonly supportAlertSyntax?: boolean;
readonly baseUri?: UriComponents;
uris?: { [href: string]: UriComponents };
}
export const enum MarkdownStringTextNewlineStyle {
Paragraph = 0,
Break = 1,
}
export class MarkdownString implements IMarkdownString {
public value: string;
public isTrusted?: boolean | MarkdownStringTrustedOptions;
public supportThemeIcons?: boolean;
public supportHtml?: boolean;
public supportAlertSyntax?: boolean;
public baseUri?: URI;
public uris?: { [href: string]: UriComponents } | undefined;
public static lift(dto: IMarkdownString): MarkdownString {
markdownString.uris = dto.uris;
markdownString.baseUri = dto.baseUri ? URI.revive(dto.baseUri) : undefined;
return markdownString;
}
constructor(
isTrustedOrOptions: boolean | { isTrusted?: boolean | MarkdownStringTrustedOptions; supportThemeIcons?: boolean; supportHtml?: boolean; supportAlertSyntax?: boolean } = false,
) {
this.value = value;
if (typeof this.value !== 'string') {
throw illegalArgument('value');
}
if (typeof isTrustedOrOptions === 'boolean') {
this.supportThemeIcons = false;
this.supportHtml = false;
this.supportAlertSyntax = false;
}
this.isTrusted = isTrustedOrOptions.isTrusted ?? undefined;
this.supportThemeIcons = isTrustedOrOptions.supportThemeIcons ?? false;
this.supportHtml = isTrustedOrOptions.supportHtml ?? false;
this.supportAlertSyntax = isTrustedOrOptions.supportAlertSyntax ?? false;
}
appendText(value: string, newlineStyle: MarkdownStringTextNewlineStyle = MarkdownStringTextNewlineStyle.Paragraph): MarkdownString {
this.value += escapeMarkdownSyntaxTokens(this.supportThemeIcons ? escapeIcons(value) : value) // CodeQL [SM02383] The Markdown is fully sanitized after being rendered.
htmlContent.ts ×1
.replace(/([ \t]+)/g, (_match, g1) => ' '.repeat(g1.length)) // CodeQL [SM02383] The Markdown is fully sanitized after being rendered.
.replace(/\>/gm, '\\>') // CodeQL [SM02383] The Markdown is fully sanitized after being rendered.
.replace(/\n/g, newlineStyle === MarkdownStringTextNewlineStyle.Break ? '\\\n' : '\n\n'); // CodeQL [SM02383] The Markdown is fully sanitized after being rendered.
return this;
}
appendMarkdown(value: string): MarkdownString {
return this;
}
appendCodeblock(langId: string, code: string): MarkdownString {
return this;
}
appendLink(target: URI | string, label: string, title?: string): MarkdownString {
this.value += this._escape(label, ']');
this.value += '](';
this.value += this._escape(String(target), ')');
if (title) {
}
return this;
}
private _escape(value: string, ch: string): string {
return value.replace(r, (match, offset) => {
return `\\${match}`;
} else {
return match;
}
}
export function isEmptyMarkdownString(oneOrMany: IMarkdownString | IMarkdownString[] | null | undefined): boolean {
if (isMarkdownString(oneOrMany)) {
return !oneOrMany.value;
} else if (Array.isArray(oneOrMany)) {
return oneOrMany.every(isEmptyMarkdownString);
} else {
return true;
}
}
export function isMarkdownString(thing: unknown): thing is IMarkdownString {
return true;
return typeof (<IMarkdownString>thing).value === 'string'
&& (typeof (<IMarkdownString>thing).isTrusted === 'boolean' || typeof (<IMarkdownString>thing).isTrusted === 'object' || (<IMarkdownString>thing).isTrusted === undefined)
htmlContent.ts ×1
&& (typeof (<IMarkdownString>thing).supportThemeIcons === 'boolean' || (<IMarkdownString>thing).supportThemeIcons === undefined)
&& (typeof (<IMarkdownString>thing).supportAlertSyntax === 'boolean' || (<IMarkdownString>thing).supportAlertSyntax === undefined);
}
export function markdownStringEqual(a: IMarkdownString, b: IMarkdownString): boolean {
if (a === b) {
return true;
} else if (!a || !b) {
return false;
} else {
return a.value === b.value
&& a.isTrusted === b.isTrusted
&& a.supportThemeIcons === b.supportThemeIcons
&& a.supportHtml === b.supportHtml
&& a.supportAlertSyntax === b.supportAlertSyntax
&& (a.baseUri === b.baseUri || !!a.baseUri && !!b.baseUri && isEqual(URI.from(a.baseUri), URI.from(b.baseUri)));
}
}
export function escapeMarkdownSyntaxTokens(text: string): string {
// escape markdown syntax tokens: http://daringfireball.net/projects/markdown/syntax#backslash
htmlContent.ts ×1
return text
.replace(/[\\`*_{}[\]()#+!~]/g, '\\$&') // CodeQL [SM02383] Backslash is escaped in the character class
.replace(/^([ \t]*)-/gm, '$1\\-'); // CodeQL [SM02383] Backslash is escaped in the character class
}
/**
* Escapes only the characters that would break out of markdown link text
* (`[label](url)`) syntax: `\` and `]`. Use this when the escaped string is
* displayed as the visible label of a link, since renderers that extract the
* link text without re-parsing markdown (e.g. the chat inline anchor / skill
* pill) would otherwise show full `escapeMarkdownSyntaxTokens` backslashes
* (`\-`, `\.`, ...) verbatim.
*/
export function escapeMarkdownLinkLabel(text: string): string {
}
/**
* @see https://github.com/microsoft/vscode/issues/193746
*/
export function appendEscapedMarkdownCodeBlockFence(code: string, langId: string) {
code.match(/^`+/gm)?.reduce((a, b) => (a.length > b.length ? a : b)).length ??
longestFenceLength >= 3 ? longestFenceLength + 1 : 3;
// the markdown result
return [
`${'`'.repeat(desiredFenceLength)}${langId}`,
code,
`${'`'.repeat(desiredFenceLength)}`,
].join('\n');
}
/**
* Wraps arbitrary text in a markdown inline code span using a backtick fence
* long enough to safely contain any backtick sequences present in the text.
*
* Backticks inside an inline code span cannot be backslash-escaped per the
* CommonMark spec — the only safe way is to choose a delimiter run longer
* than any run of backticks in the content (and pad with spaces if the
* content begins or ends with a backtick).
*/
export function appendEscapedMarkdownInlineCode(text: string): string {
const longestBacktickRun = Math.max(0, ...(text.match(/`+/g) ?? []).map(m => m.length));
htmlContent.ts ×1
const fence = '`'.repeat(longestBacktickRun + 1);
const needsSpace = text.startsWith('`') || text.endsWith('`');
const content = needsSpace ? ` ${text} ` : text;
return `${fence}${content}${fence}`;
}
export function escapeDoubleQuotes(input: string) {
return input.replace(/"/g, '"');
}
export function removeMarkdownEscapes(text: string): string {
if (!text) {
return text;
}
return text.replace(/\\([\\`*_{}[\]()#+\-.!~])/g, '$1');
}
export function parseHrefAndDimensions(href: string): { href: string; dimensions: string[] } {
const dimensions: string[] = [];
const splitted = href.split('|').map(s => s.trim());
href = splitted[0];
const parameters = splitted[1];
if (parameters) {
const heightFromParams = /height=(\d+)/.exec(parameters);
const widthFromParams = /width=(\d+)/.exec(parameters);
const height = heightFromParams ? heightFromParams[1] : '';
const width = widthFromParams ? widthFromParams[1] : '';
const widthIsFinite = isFinite(parseInt(width));
const heightIsFinite = isFinite(parseInt(height));
if (widthIsFinite) {
dimensions.push(`width="${width}"`);
}
if (heightIsFinite) {
dimensions.push(`height="${height}"`);
}
}
return { href, dimensions };
}
export function createMarkdownLink(text: string, href: string, title?: string, escapeTokens = true): string {
return `[${escapeTokens ? escapeMarkdownSyntaxTokens(text) : text}](${href}${title ? ` "${escapeMarkdownSyntaxTokens(title)}"` : ''})`;
}
export function createMarkdownCommandLink(command: { text: string; id: string; arguments?: unknown[]; tooltip: string }, escapeTokens = true): string {
const uri = createCommandUri(command.id, ...(command.arguments || [])).toString();
return createMarkdownLink(command.text, uri, command.tooltip, escapeTokens);
}
export function createCommandUri(commandId: string, ...commandArgs: unknown[]): URI {
scheme: Schemas.command,
path: commandId,
query: commandArgs.length ? encodeURIComponent(JSON.stringify(commandArgs)) : undefined,
});
}