提交 231c6e77 编写于 作者: E Eric Amodio

Updates #84695 - api review

`appendMarkdown` is now the only way to use ThemeIcons
上级 390fe6df
......@@ -5,7 +5,7 @@
import { equals } from 'vs/base/common/arrays';
import { UriComponents } from 'vs/base/common/uri';
import { escapeCodicons, markdownUnescapeCodicons } from 'vs/base/common/codicons';
import { escapeCodicons } from 'vs/base/common/codicons';
export interface IMarkdownString {
readonly value: string;
......@@ -39,10 +39,9 @@ export class MarkdownString implements IMarkdownString {
appendText(value: string): MarkdownString {
// escape markdown syntax tokens: http://daringfireball.net/projects/markdown/syntax#backslash
value = value
this._value += (this._supportThemeIcons ? escapeCodicons(value) : value)
.replace(/[\\`*_{}[\]()#+\-.!]/g, '\\$&')
.replace('\n', '\n\n');
this._value += this.supportThemeIcons ? markdownUnescapeCodicons(value) : value;
return this;
}
......@@ -61,10 +60,6 @@ export class MarkdownString implements IMarkdownString {
this._value += '\n```\n';
return this;
}
static escapeThemeIcons(value: string): string {
return escapeCodicons(value);
}
}
export function isEmptyMarkdownString(oneOrMany: IMarkdownString | IMarkdownString[] | null | undefined): boolean {
......
......@@ -54,34 +54,26 @@ suite('MarkdownRenderer', () => {
test('render appendText', () => {
const mds = new MarkdownString(undefined, { supportThemeIcons: true });
mds.appendText('$(zap) $(dont match me)');
mds.appendText('$(zap) $(not a theme icon) $(add)');
let result: HTMLElement = renderMarkdown(mds);
assert.strictEqual(result.innerHTML, `<p><span class="codicon codicon-zap"></span> $(dont match me)</p>`);
});
test('render appendText escaped', () => {
const mds = new MarkdownString(undefined, { supportThemeIcons: true });
mds.appendText(MarkdownString.escapeThemeIcons('$(zap) $(dont match me)'));
let result: HTMLElement = renderMarkdown(mds);
assert.strictEqual(result.innerHTML, `<p>$(zap) $(dont match me)</p>`);
assert.strictEqual(result.innerHTML, `<p>$(zap) $(not a theme icon) $(add)</p>`);
});
test('render appendMarkdown', () => {
const mds = new MarkdownString(undefined, { supportThemeIcons: true });
mds.appendMarkdown('$(zap) $(dont match me)');
mds.appendMarkdown('$(zap) $(not a theme icon) $(add)');
let result: HTMLElement = renderMarkdown(mds);
assert.strictEqual(result.innerHTML, `<p><span class="codicon codicon-zap"></span> $(dont match me)</p>`);
assert.strictEqual(result.innerHTML, `<p><span class="codicon codicon-zap"></span> $(not a theme icon) <span class="codicon codicon-add"></span></p>`);
});
test('render appendMarkdown escaped', () => {
test('render appendMarkdown with escaped icon', () => {
const mds = new MarkdownString(undefined, { supportThemeIcons: true });
mds.appendMarkdown(MarkdownString.escapeThemeIcons('$(zap) $(dont match me)'));
mds.appendMarkdown('\\$(zap) $(not a theme icon) $(add)');
let result: HTMLElement = renderMarkdown(mds);
assert.strictEqual(result.innerHTML, `<p>$(zap) $(dont match me)</p>`);
assert.strictEqual(result.innerHTML, `<p>$(zap) $(not a theme icon) <span class="codicon codicon-add"></span></p>`);
});
});
......@@ -90,18 +82,18 @@ suite('MarkdownRenderer', () => {
test('render appendText', () => {
const mds = new MarkdownString(undefined, { supportThemeIcons: false });
mds.appendText('$(zap) $(dont match me)');
mds.appendText('$(zap) $(not a theme icon) $(add)');
let result: HTMLElement = renderMarkdown(mds);
assert.strictEqual(result.innerHTML, `<p>$(zap) $(dont match me)</p>`);
assert.strictEqual(result.innerHTML, `<p>$(zap) $(not a theme icon) $(add)</p>`);
});
test('render appendMarkdown', () => {
test('render appendMarkdown with escaped icon', () => {
const mds = new MarkdownString(undefined, { supportThemeIcons: false });
mds.appendMarkdown('$(zap) $(dont match me)');
mds.appendMarkdown('\\$(zap) $(not a theme icon) $(add)');
let result: HTMLElement = renderMarkdown(mds);
assert.strictEqual(result.innerHTML, `<p>$(zap) $(dont match me)</p>`);
assert.strictEqual(result.innerHTML, `<p>$(zap) $(not a theme icon) $(add)</p>`);
});
});
......
......@@ -8,10 +8,9 @@ import { MarkdownString } from 'vs/base/common/htmlContent';
suite('MarkdownString', () => {
test('escape', () => {
test('appendText', () => {
const mds = new MarkdownString();
mds.appendText('# foo\n*bar*');
assert.equal(mds.value, '\\# foo\n\n\\*bar\\*');
......@@ -19,59 +18,54 @@ suite('MarkdownString', () => {
suite('ThemeIcons', () => {
test('escapeThemeIcons', () => {
assert.equal(
MarkdownString.escapeThemeIcons('$(zap) $(not an icon) foo$(bar)'),
'\\$(zap) $(not an icon) foo\\$(bar)'
);
});
suite('Support On', () => {
test('appendText', () => {
const mds = new MarkdownString(undefined, { supportThemeIcons: true });
mds.appendText('$(zap)');
assert.equal(mds.value, '$(zap)');
});
test('appendText escaped', () => {
const mds = new MarkdownString(undefined, { supportThemeIcons: true });
mds.appendText(MarkdownString.escapeThemeIcons('$(zap)'));
mds.appendText('$(zap) $(not a theme icon) $(add)');
assert.equal(mds.value, '\\\\$\\(zap\\)');
assert.equal(mds.value, '\\\\$\\(zap\\) $\\(not a theme icon\\) \\\\$\\(add\\)');
});
test('appendMarkdown', () => {
const mds = new MarkdownString(undefined, { supportThemeIcons: true });
mds.appendMarkdown('$(zap)');
mds.appendMarkdown('$(zap) $(not a theme icon) $(add)');
assert.equal(mds.value, '$(zap)');
assert.equal(mds.value, '$(zap) $(not a theme icon) $(add)');
});
test('appendMarkdown escaped', () => {
test('appendMarkdown with escaped icon', () => {
const mds = new MarkdownString(undefined, { supportThemeIcons: true });
mds.appendMarkdown(MarkdownString.escapeThemeIcons('$(zap)'));
mds.appendMarkdown('\\$(zap) $(not a theme icon) $(add)');
assert.equal(mds.value, '\\$(zap)');
assert.equal(mds.value, '\\$(zap) $(not a theme icon) $(add)');
});
});
suite('Support Off', () => {
test('appendText', () => {
const mds = new MarkdownString(undefined, { supportThemeIcons: false });
mds.appendText('$(zap)');
mds.appendText('$(zap) $(not a theme icon) $(add)');
assert.equal(mds.value, '$\\(zap\\)');
assert.equal(mds.value, '$\\(zap\\) $\\(not a theme icon\\) $\\(add\\)');
});
test('appendMarkdown', () => {
const mds = new MarkdownString(undefined, { supportThemeIcons: false });
mds.appendMarkdown('$(zap)');
mds.appendMarkdown('$(zap) $(not a theme icon) $(add)');
assert.equal(mds.value, '$(zap) $(not a theme icon) $(add)');
});
assert.equal(mds.value, '$(zap)');
test('appendMarkdown with escaped icon', () => {
const mds = new MarkdownString(undefined, { supportThemeIcons: true });
mds.appendMarkdown('\\$(zap) $(not a theme icon) $(add)');
assert.equal(mds.value, '\\$(zap) $(not a theme icon) $(add)');
});
});
});
......
......@@ -2341,12 +2341,6 @@ declare module 'vscode' {
*/
export class MarkdownString {
/**
* Escapes any [ThemeIcons](#ThemeIcon), e.g. `$(zap)`, in the string.
* @param value A string.
*/
static escapeThemeIcons(value: string): string;
/**
* The markdown string.
*/
......@@ -2362,9 +2356,9 @@ declare module 'vscode' {
* Creates a new markdown string with the given value.
*
* @param value Optional, initial value.
* @param options Optional, options to specify whether [ThemeIcons](#ThemeIcon) are supported within the [`MarkdownString`](#MarkdownString).
* @param supportThemeIcons Optional, Specifies whether [ThemeIcons](#ThemeIcon) are supported within the [`MarkdownString`](#MarkdownString).
*/
constructor(value?: string, options?: { supportThemeIcons?: boolean });
constructor(value?: string, supportThemeIcons?: boolean);
/**
* Appends and escapes the given string to this markdown string.
......@@ -2373,7 +2367,7 @@ declare module 'vscode' {
appendText(value: string): MarkdownString;
/**
* Appends the given string 'as is' to this markdown string.
* Appends the given string 'as is' to this markdown string. When [`supportThemeIcons`](#MarkdownString.supportThemeIcons) is `true`, [ThemeIcons](#ThemeIcon) in the `value` will be iconified.
* @param value Markdown string.
*/
appendMarkdown(value: string): MarkdownString;
......
......@@ -14,7 +14,7 @@ import { generateUuid } from 'vs/base/common/uuid';
import * as vscode from 'vscode';
import { FileSystemProviderErrorCode, markAsFileSystemProviderError } from 'vs/platform/files/common/files';
import { RemoteAuthorityResolverErrorCode } from 'vs/platform/remote/common/remoteAuthorityResolver';
import { markdownUnescapeCodicons, escapeCodicons } from 'vs/base/common/codicons';
import { escapeCodicons } from 'vs/base/common/codicons';
function es5ClassCompat(target: Function): any {
///@ts-ignore
......@@ -1234,17 +1234,16 @@ export class MarkdownString {
isTrusted?: boolean;
readonly supportThemeIcons?: boolean;
constructor(value?: string, { supportThemeIcons }: { supportThemeIcons?: boolean } = {}) {
constructor(value?: string, supportThemeIcons: boolean = false) {
this.value = value ?? '';
this.supportThemeIcons = supportThemeIcons ?? false;
this.supportThemeIcons = supportThemeIcons;
}
appendText(value: string): MarkdownString {
// escape markdown syntax tokens: http://daringfireball.net/projects/markdown/syntax#backslash
value = value
this.value += (this.supportThemeIcons ? escapeCodicons(value) : value)
.replace(/[\\`*_{}[\]()#+\-.!]/g, '\\$&')
.replace('\n', '\n\n');
this.value += this.supportThemeIcons ? markdownUnescapeCodicons(value) : value;
return this;
}
......@@ -1263,10 +1262,6 @@ export class MarkdownString {
this.value += '\n```\n';
return this;
}
static escapeThemeIcons(value: string): string {
return escapeCodicons(value);
}
}
@es5ClassCompat
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册