Path: blob/main/src/vs/base/test/common/markdownString.test.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import assert from 'assert';6import { IMarkdownString, MarkdownString } from '../../common/htmlContent.js';7import { ensureNoDisposablesAreLeakedInTestSuite } from './utils.js';8import { URI } from '../../common/uri.js';910suite('MarkdownString', () => {1112ensureNoDisposablesAreLeakedInTestSuite();1314test('Escape leading whitespace', function () {15const mds = new MarkdownString();16mds.appendText('Hello\n Not a code block');17assert.strictEqual(mds.value, 'Hello\n\n Not a code block');18});1920test('MarkdownString.appendText doesn\'t escape quote #109040', function () {21const mds = new MarkdownString();22mds.appendText('> Text\n>More');23assert.strictEqual(mds.value, '\\> Text\n\n\\>More');24});2526test('appendText', () => {2728const mds = new MarkdownString();29mds.appendText('# foo\n*bar*');3031assert.strictEqual(mds.value, '\\# foo\n\n\\*bar\\*');32});3334test('appendLink', function () {3536function assertLink(target: string, label: string, title: string | undefined, expected: string) {37const mds = new MarkdownString();38mds.appendLink(target, label, title);39assert.strictEqual(mds.value, expected);40}4142assertLink(43'https://example.com\\()', 'hello', undefined,44'[hello](https://example.com\\(\\))'45);46assertLink(47'https://example.com', 'hello', 'title',48'[hello](https://example.com "title")'49);50assertLink(51'foo)', 'hello]', undefined,52'[hello\\]](foo\\))'53);54assertLink(55'foo\\)', 'hello]', undefined,56'[hello\\]](foo\\))'57);58assertLink(59'fo)o', 'hell]o', undefined,60'[hell\\]o](fo\\)o)'61);62assertLink(63'foo)', 'hello]', 'title"',64'[hello\\]](foo\\) "title\\"")'65);66});6768test('lift', () => {69const dto: IMarkdownString = {70value: 'hello',71baseUri: URI.file('/foo/bar'),72supportThemeIcons: true,73isTrusted: true,74supportHtml: true,75uris: {76[URI.file('/foo/bar2').toString()]: URI.file('/foo/bar2'),77[URI.file('/foo/bar3').toString()]: URI.file('/foo/bar3')78}79};80const mds = MarkdownString.lift(dto);81assert.strictEqual(mds.value, dto.value);82assert.strictEqual(mds.baseUri?.toString(), dto.baseUri?.toString());83assert.strictEqual(mds.supportThemeIcons, dto.supportThemeIcons);84assert.strictEqual(mds.isTrusted, dto.isTrusted);85assert.strictEqual(mds.supportHtml, dto.supportHtml);86assert.deepStrictEqual(mds.uris, dto.uris);87});8889test('lift returns new instance', () => {90const instance = new MarkdownString('hello');91const mds2 = MarkdownString.lift(instance).appendText('world');92assert.strictEqual(mds2.value, 'helloworld');93assert.strictEqual(instance.value, 'hello');94});9596suite('appendCodeBlock', () => {97function assertCodeBlock(lang: string, code: string, result: string) {98const mds = new MarkdownString();99mds.appendCodeblock(lang, code);100assert.strictEqual(mds.value, result);101}102103test('common cases', () => {104// no backticks105assertCodeBlock('ts', 'const a = 1;', `\n${[106'```ts',107'const a = 1;',108'```'109].join('\n')}\n`);110// backticks111assertCodeBlock('ts', 'const a = `1`;', `\n${[112'```ts',113'const a = `1`;',114'```'115].join('\n')}\n`);116});117118// @see https://github.com/microsoft/vscode/issues/193746119test('escape fence', () => {120// fence in the first line121assertCodeBlock('md', '```\n```', `\n${[122'````md',123'```\n```',124'````'125].join('\n')}\n`);126// fence in the middle of code127assertCodeBlock('md', '\n\n```\n```', `\n${[128'````md',129'\n\n```\n```',130'````'131].join('\n')}\n`);132// longer fence at the end of code133assertCodeBlock('md', '```\n```\n````\n````', `\n${[134'`````md',135'```\n```\n````\n````',136'`````'137].join('\n')}\n`);138});139});140141suite('ThemeIcons', () => {142143suite('Support On', () => {144145test('appendText', () => {146const mds = new MarkdownString(undefined, { supportThemeIcons: true });147mds.appendText('$(zap) $(not a theme icon) $(add)');148149assert.strictEqual(mds.value, '\\\\$\\(zap\\) $\\(not a theme icon\\) \\\\$\\(add\\)');150});151152test('appendMarkdown', () => {153const mds = new MarkdownString(undefined, { supportThemeIcons: true });154mds.appendMarkdown('$(zap) $(not a theme icon) $(add)');155156assert.strictEqual(mds.value, '$(zap) $(not a theme icon) $(add)');157});158159test('appendMarkdown with escaped icon', () => {160const mds = new MarkdownString(undefined, { supportThemeIcons: true });161mds.appendMarkdown('\\$(zap) $(not a theme icon) $(add)');162163assert.strictEqual(mds.value, '\\$(zap) $(not a theme icon) $(add)');164});165166});167168suite('Support Off', () => {169170test('appendText', () => {171const mds = new MarkdownString(undefined, { supportThemeIcons: false });172mds.appendText('$(zap) $(not a theme icon) $(add)');173174assert.strictEqual(mds.value, '$\\(zap\\) $\\(not a theme icon\\) $\\(add\\)');175});176177test('appendMarkdown', () => {178const mds = new MarkdownString(undefined, { supportThemeIcons: false });179mds.appendMarkdown('$(zap) $(not a theme icon) $(add)');180181assert.strictEqual(mds.value, '$(zap) $(not a theme icon) $(add)');182});183184test('appendMarkdown with escaped icon', () => {185const mds = new MarkdownString(undefined, { supportThemeIcons: true });186mds.appendMarkdown('\\$(zap) $(not a theme icon) $(add)');187188assert.strictEqual(mds.value, '\\$(zap) $(not a theme icon) $(add)');189});190191});192});193});194195196