1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | exports.escapeMarkdownSyntaxTokens = exports.MarkdownStringImpl = exports.MarkdownString = exports.MarkdownStringTextNewlineStyle = void 0;
|
19 | const strings_1 = require("../strings");
|
20 | const icon_utilities_1 = require("./icon-utilities");
|
21 | const types_1 = require("../types");
|
22 | var MarkdownStringTextNewlineStyle;
|
23 | (function (MarkdownStringTextNewlineStyle) {
|
24 | MarkdownStringTextNewlineStyle[MarkdownStringTextNewlineStyle["Paragraph"] = 0] = "Paragraph";
|
25 | MarkdownStringTextNewlineStyle[MarkdownStringTextNewlineStyle["Break"] = 1] = "Break";
|
26 | })(MarkdownStringTextNewlineStyle = exports.MarkdownStringTextNewlineStyle || (exports.MarkdownStringTextNewlineStyle = {}));
|
27 | var MarkdownString;
|
28 | (function (MarkdownString) {
|
29 | |
30 |
|
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 |
|
38 | class 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 | }
|
98 | exports.MarkdownStringImpl = MarkdownStringImpl;
|
99 | function escapeMarkdownSyntaxTokens(text) {
|
100 |
|
101 | return text.replace(/[\\`*_{}[\]()#+\-!]/g, '\\$&');
|
102 | }
|
103 | exports.escapeMarkdownSyntaxTokens = escapeMarkdownSyntaxTokens;
|
104 |
|
\ | No newline at end of file |