htmlContent.ts 2.7 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

6
import { equals } from 'vs/base/common/arrays';
7
import { UriComponents } from 'vs/base/common/uri';
8

9 10
export interface IMarkdownString {
	value: string;
J
Johannes Rieken 已提交
11
	isTrusted?: boolean;
12
	uris?: { [href: string]: UriComponents };
13 14 15 16 17
}

export class MarkdownString implements IMarkdownString {

	value: string;
J
Johannes Rieken 已提交
18
	isTrusted?: boolean;
19 20 21 22 23

	constructor(value: string = '') {
		this.value = value;
	}

24
	appendText(value: string): MarkdownString {
J
Johannes Rieken 已提交
25 26
		// escape markdown syntax tokens: http://daringfireball.net/projects/markdown/syntax#backslash
		this.value += value.replace(/[\\`*_{}[\]()#+\-.!]/g, '\\$&');
27 28 29
		return this;
	}

30 31 32 33 34
	appendMarkdown(value: string): MarkdownString {
		this.value += value;
		return this;
	}

35
	appendCodeblock(langId: string, code: string): MarkdownString {
36
		this.value += '\n```';
37 38 39
		this.value += langId;
		this.value += '\n';
		this.value += code;
40
		this.value += '\n```\n';
41 42 43 44
		return this;
	}
}

A
Alex Dima 已提交
45
export function isEmptyMarkdownString(oneOrMany: IMarkdownString | IMarkdownString[] | null | undefined): boolean {
46 47 48 49 50 51 52 53 54 55 56 57
	if (isMarkdownString(oneOrMany)) {
		return !oneOrMany.value;
	} else if (Array.isArray(oneOrMany)) {
		return oneOrMany.every(isEmptyMarkdownString);
	} else {
		return true;
	}
}

export function isMarkdownString(thing: any): thing is IMarkdownString {
	if (thing instanceof MarkdownString) {
		return true;
J
Johannes Rieken 已提交
58
	} else if (thing && typeof thing === 'object') {
59
		return typeof (<IMarkdownString>thing).value === 'string'
R
Rob Lourens 已提交
60
			&& (typeof (<IMarkdownString>thing).isTrusted === 'boolean' || (<IMarkdownString>thing).isTrusted === undefined);
61 62 63 64
	}
	return false;
}

65
export function markedStringsEquals(a: IMarkdownString | IMarkdownString[], b: IMarkdownString | IMarkdownString[]): boolean {
66 67
	if (!a && !b) {
		return true;
68
	} else if (!a || !b) {
69
		return false;
70
	} else if (Array.isArray(a) && Array.isArray(b)) {
71
		return equals(a, b, markdownStringEqual);
72
	} else if (isMarkdownString(a) && isMarkdownString(b)) {
73
		return markdownStringEqual(a, b);
74
	} else {
75 76 77 78
		return false;
	}
}

79 80 81 82 83 84
function markdownStringEqual(a: IMarkdownString, b: IMarkdownString): boolean {
	if (a === b) {
		return true;
	} else if (!a || !b) {
		return false;
	} else {
J
Johannes Rieken 已提交
85
		return a.value === b.value && a.isTrusted === b.isTrusted;
86 87 88
	}
}

89 90 91 92 93 94
export function removeMarkdownEscapes(text: string): string {
	if (!text) {
		return text;
	}
	return text.replace(/\\([\\`*_{}[\]()#+\-.!])/g, '$1');
}