UNPKG

4.76 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2022 Ericsson and others.
4//
5// This program and the accompanying materials are made available under the
6// terms of the Eclipse Public License v. 2.0 which is available at
7// http://www.eclipse.org/legal/epl-2.0.
8//
9// This Source Code may also be made available under the following Secondary
10// Licenses when the conditions for such availability set forth in the Eclipse
11// Public License v. 2.0 are satisfied: GNU General Public License, version 2
12// with the GNU Classpath Exception which is available at
13// https://www.gnu.org/software/classpath/license.html.
14//
15// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
16// *****************************************************************************
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.escapeMarkdownSyntaxTokens = exports.MarkdownStringImpl = exports.MarkdownString = exports.MarkdownStringTextNewlineStyle = void 0;
19const strings_1 = require("../strings");
20const icon_utilities_1 = require("./icon-utilities");
21const types_1 = require("../types");
22var MarkdownStringTextNewlineStyle;
23(function (MarkdownStringTextNewlineStyle) {
24 MarkdownStringTextNewlineStyle[MarkdownStringTextNewlineStyle["Paragraph"] = 0] = "Paragraph";
25 MarkdownStringTextNewlineStyle[MarkdownStringTextNewlineStyle["Break"] = 1] = "Break";
26})(MarkdownStringTextNewlineStyle = exports.MarkdownStringTextNewlineStyle || (exports.MarkdownStringTextNewlineStyle = {}));
27var MarkdownString;
28(function (MarkdownString) {
29 /**
30 * @returns whether the candidate satisfies the interface of a markdown string
31 */
32 function is(candidate) {
33 return (0, types_1.isObject)(candidate) && (0, types_1.isString)(candidate.value);
34 }
35 MarkdownString.is = is;
36})(MarkdownString = exports.MarkdownString || (exports.MarkdownString = {}));
37// Copied from https://github.com/microsoft/vscode/blob/7d9b1c37f8e5ae3772782ba3b09d827eb3fdd833/src/vs/base/common/htmlContent.ts
38class MarkdownStringImpl {
39 constructor(value = '', isTrustedOrOptions = false) {
40 var _a, _b, _c;
41 this.value = value;
42 if (typeof this.value !== 'string') {
43 throw new Error('Illegal value for MarkdownString. Expected string, got ' + typeof this.value);
44 }
45 if (typeof isTrustedOrOptions === 'boolean') {
46 this.isTrusted = isTrustedOrOptions;
47 this.supportThemeIcons = false;
48 this.supportHtml = false;
49 }
50 else {
51 this.isTrusted = (_a = isTrustedOrOptions.isTrusted) !== null && _a !== void 0 ? _a : undefined;
52 this.supportThemeIcons = (_b = isTrustedOrOptions.supportThemeIcons) !== null && _b !== void 0 ? _b : false;
53 this.supportHtml = (_c = isTrustedOrOptions.supportHtml) !== null && _c !== void 0 ? _c : false;
54 }
55 }
56 appendText(value, newlineStyle = MarkdownStringTextNewlineStyle.Paragraph) {
57 this.value += escapeMarkdownSyntaxTokens(this.supportThemeIcons ? (0, icon_utilities_1.escapeIcons)(value) : value)
58 .replace(/([ \t]+)/g, (_match, g1) => ' '.repeat(g1.length))
59 .replace(/\>/gm, '\\>')
60 .replace(/\n/g, newlineStyle === MarkdownStringTextNewlineStyle.Break ? '\\\n' : '\n\n');
61 return this;
62 }
63 appendMarkdown(value) {
64 this.value += value;
65 return this;
66 }
67 appendCodeblock(langId, code) {
68 this.value += '\n```';
69 this.value += langId;
70 this.value += '\n';
71 this.value += code;
72 this.value += '\n```\n';
73 return this;
74 }
75 appendLink(target, label, title) {
76 this.value += '[';
77 this.value += this._escape(label, ']');
78 this.value += '](';
79 this.value += this._escape(String(target), ')');
80 if (title) {
81 this.value += ` "${this._escape(this._escape(title, '"'), ')')}"`;
82 }
83 this.value += ')';
84 return this;
85 }
86 _escape(value, ch) {
87 const r = new RegExp((0, strings_1.escapeRegExpCharacters)(ch), 'g');
88 return value.replace(r, (match, offset) => {
89 if (value.charAt(offset - 1) !== '\\') {
90 return `\\${match}`;
91 }
92 else {
93 return match;
94 }
95 });
96 }
97}
98exports.MarkdownStringImpl = MarkdownStringImpl;
99function escapeMarkdownSyntaxTokens(text) {
100 // escape markdown syntax tokens: http://daringfireball.net/projects/markdown/syntax#backslash
101 return text.replace(/[\\`*_{}[\]()#+\-!]/g, '\\$&');
102}
103exports.escapeMarkdownSyntaxTokens = escapeMarkdownSyntaxTokens;
104//# sourceMappingURL=markdown-string.js.map
\No newline at end of file