Skip to content

Commit 21ce7da

Browse files
authored
Extract default marked renderers to constant functions (microsoft#159630)
1 parent 01ea9c1 commit 21ce7da

File tree

1 file changed

+50
-43
lines changed

1 file changed

+50
-43
lines changed

src/vs/base/browser/markdownRenderer.ts

Lines changed: 50 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,53 @@ export interface MarkdownRenderOptions extends FormattedTextRenderOptions {
3232
readonly asyncRenderCallback?: () => void;
3333
}
3434

35+
const defaultMarkedRenderers = Object.freeze({
36+
image: (href: string | null, title: string | null, text: string): string => {
37+
let dimensions: string[] = [];
38+
let attributes: string[] = [];
39+
if (href) {
40+
({ href, dimensions } = parseHrefAndDimensions(href));
41+
attributes.push(`src="${escapeDoubleQuotes(href)}"`);
42+
}
43+
if (text) {
44+
attributes.push(`alt="${escapeDoubleQuotes(text)}"`);
45+
}
46+
if (title) {
47+
attributes.push(`title="${escapeDoubleQuotes(title)}"`);
48+
}
49+
if (dimensions.length) {
50+
attributes = attributes.concat(dimensions);
51+
}
52+
return '<img ' + attributes.join(' ') + '>';
53+
},
54+
55+
paragraph: (text: string): string => {
56+
return `<p>${text}</p>`;
57+
},
58+
59+
link: (href: string | null, title: string | null, text: string): string => {
60+
if (typeof href !== 'string') {
61+
return '';
62+
}
63+
64+
// Remove markdown escapes. Workaround for https://github.com/chjj/marked/issues/829
65+
if (href === text) { // raw link case
66+
text = removeMarkdownEscapes(text);
67+
}
68+
69+
title = typeof title === 'string' ? escapeDoubleQuotes(removeMarkdownEscapes(title)) : '';
70+
href = removeMarkdownEscapes(href);
71+
72+
// HTML Encode href
73+
href = href.replace(/&/g, '&amp;')
74+
.replace(/</g, '&lt;')
75+
.replace(/>/g, '&gt;')
76+
.replace(/"/g, '&quot;')
77+
.replace(/'/g, '&#39;');
78+
return `<a href="${href}" title="${title || href}">${text}</a>`;
79+
},
80+
});
81+
3582
/**
3683
* Low-level way create a html element from a markdown string.
3784
*
@@ -93,49 +140,9 @@ export function renderMarkdown(markdown: IMarkdownString, options: MarkdownRende
93140
};
94141

95142
const renderer = new marked.Renderer();
96-
97-
renderer.image = (href: string, title: string, text: string) => {
98-
let dimensions: string[] = [];
99-
let attributes: string[] = [];
100-
if (href) {
101-
({ href, dimensions } = parseHrefAndDimensions(href));
102-
attributes.push(`src="${escapeDoubleQuotes(href)}"`);
103-
}
104-
if (text) {
105-
attributes.push(`alt="${escapeDoubleQuotes(text)}"`);
106-
}
107-
if (title) {
108-
attributes.push(`title="${escapeDoubleQuotes(title)}"`);
109-
}
110-
if (dimensions.length) {
111-
attributes = attributes.concat(dimensions);
112-
}
113-
return '<img ' + attributes.join(' ') + '>';
114-
};
115-
renderer.link = (href, title, text): string => {
116-
if (typeof href !== 'string') {
117-
return '';
118-
}
119-
120-
// Remove markdown escapes. Workaround for https://github.com/chjj/marked/issues/829
121-
if (href === text) { // raw link case
122-
text = removeMarkdownEscapes(text);
123-
}
124-
125-
title = typeof title === 'string' ? escapeDoubleQuotes(removeMarkdownEscapes(title)) : '';
126-
href = removeMarkdownEscapes(href);
127-
128-
// HTML Encode href
129-
href = href.replace(/&/g, '&amp;')
130-
.replace(/</g, '&lt;')
131-
.replace(/>/g, '&gt;')
132-
.replace(/"/g, '&quot;')
133-
.replace(/'/g, '&#39;');
134-
return `<a href="${href}" title="${title || href}">${text}</a>`;
135-
};
136-
renderer.paragraph = (text): string => {
137-
return `<p>${text}</p>`;
138-
};
143+
renderer.image = defaultMarkedRenderers.image;
144+
renderer.link = defaultMarkedRenderers.link;
145+
renderer.paragraph = defaultMarkedRenderers.paragraph;
139146

140147
// Will collect [id, renderedElement] tuples
141148
const codeBlocks: Promise<[string, HTMLElement]>[] = [];

0 commit comments

Comments
 (0)