Skip to content

Commit 48f1ece

Browse files
authored
Re-use plaintext markdown renderer (microsoft#159627)
This renderer is stateless so we can reuse it across calls to `renderMarkdownAsPlaintext`
1 parent 21337ae commit 48f1ece

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed

src/vs/base/browser/markdownRenderer.ts

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { Event } from 'vs/base/common/event';
1414
import { IMarkdownString, escapeDoubleQuotes, parseHrefAndDimensions, removeMarkdownEscapes } from 'vs/base/common/htmlContent';
1515
import { markdownEscapeEscapedIcons } from 'vs/base/common/iconLabels';
1616
import { defaultGenerator } from 'vs/base/common/idGenerator';
17+
import { Lazy } from 'vs/base/common/lazy';
1718
import { DisposableStore } from 'vs/base/common/lifecycle';
1819
import { marked } from 'vs/base/common/marked/marked';
1920
import { parse } from 'vs/base/common/marshalling';
@@ -399,6 +400,27 @@ export function renderStringAsPlaintext(string: IMarkdownString | string) {
399400
* Strips all markdown from `markdown`. For example `# Header` would be output as `Header`.
400401
*/
401402
export function renderMarkdownAsPlaintext(markdown: IMarkdownString) {
403+
// values that are too long will freeze the UI
404+
let value = markdown.value ?? '';
405+
if (value.length > 100_000) {
406+
value = `${value.substr(0, 100_000)}…`;
407+
}
408+
409+
const html = marked.parse(value, { renderer: plainTextRenderer.getValue() }).replace(/&(#\d+|[a-zA-Z]+);/g, m => unescapeInfo.get(m) ?? m);
410+
411+
return sanitizeRenderedMarkdown({ isTrusted: false }, html).toString();
412+
}
413+
414+
const unescapeInfo = new Map<string, string>([
415+
['&quot;', '"'],
416+
['&nbsp;', ' '],
417+
['&amp;', '&'],
418+
['&#39;', '\''],
419+
['&lt;', '<'],
420+
['&gt;', '>'],
421+
]);
422+
423+
const plainTextRenderer = new Lazy<marked.Renderer>(() => {
402424
const renderer = new marked.Renderer();
403425

404426
renderer.code = (code: string): string => {
@@ -461,22 +483,5 @@ export function renderMarkdownAsPlaintext(markdown: IMarkdownString) {
461483
renderer.link = (_href: string, _title: string, text: string): string => {
462484
return text;
463485
};
464-
// values that are too long will freeze the UI
465-
let value = markdown.value ?? '';
466-
if (value.length > 100_000) {
467-
value = `${value.substr(0, 100_000)}…`;
468-
}
469-
470-
const unescapeInfo = new Map<string, string>([
471-
['&quot;', '"'],
472-
['&nbsp;', ' '],
473-
['&amp;', '&'],
474-
['&#39;', '\''],
475-
['&lt;', '<'],
476-
['&gt;', '>'],
477-
]);
478-
479-
const html = marked.parse(value, { renderer }).replace(/&(#\d+|[a-zA-Z]+);/g, m => unescapeInfo.get(m) ?? m);
480-
481-
return sanitizeRenderedMarkdown({ isTrusted: false }, html).toString();
482-
}
486+
return renderer;
487+
});

0 commit comments

Comments
 (0)