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

'use strict';

8
import { equals } from 'vs/base/common/arrays';
9
import { marked } from 'vs/base/common/marked/marked';
10

11 12 13 14 15
/**
 * MarkedString can be used to render human readable text. It is either a markdown string
 * or a code-block that provides a language and a code snippet. Note that
 * markdown strings will be sanitized - that means html will be escaped.
 */
16
export type MarkedString = string;
17

J
Johannes Rieken 已提交
18
export function markedStringsEquals(a: MarkedString | MarkedString[], b: MarkedString | MarkedString[]): boolean {
19 20
	if (!a && !b) {
		return true;
21
	} else if (!a || !b) {
22
		return false;
23 24 25 26 27
	} else if (typeof a === 'string' && typeof b === 'string') {
		return a === b;
	} else if (Array.isArray(a) && Array.isArray(b)) {
		return equals(a, b);
	} else {
28 29 30 31
		return false;
	}
}

J
Johannes Rieken 已提交
32
export function textToMarkedString(text: string): MarkedString {
33
	return text.replace(/[\\`*_{}[\]()#+\-.!]/g, '\\$&'); // escape markdown syntax tokens: http://daringfireball.net/projects/markdown/syntax#backslash
34 35
}

36 37 38 39 40 41
export function removeMarkdownEscapes(text: string): string {
	if (!text) {
		return text;
	}
	return text.replace(/\\([\\`*_{}[\]()#+\-.!])/g, '$1');
}
42 43 44 45 46 47 48 49 50 51 52 53 54

export function containsCommandLink(value: MarkedString): boolean {
	let uses = false;
	const renderer = new marked.Renderer();
	renderer.link = (href, title, text): string => {
		if (href.match(/^command:/i)) {
			uses = true;
		}
		return 'link';
	};
	marked(value, { renderer });
	return uses;
}