Skip to content

Commit cad50f2

Browse files
authored
1 parent 1246fe5 commit cad50f2

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/vs/base/browser/markdownRenderer.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ export function renderMarkdown(markdown: IMarkdownString, options: MarkdownRende
143143
if (options.codeBlockRenderer) {
144144
renderer.code = (code, lang) => {
145145
const id = defaultGenerator.nextId();
146-
const value = options.codeBlockRenderer!(lang ?? '', code);
146+
const value = options.codeBlockRenderer!(postProcessCodeBlockLanguageId(lang), code);
147147
codeBlocks.push(value.then(element => [id, element]));
148148
return `<div class="code" data-code="${id}">${escape(code)}</div>`;
149149
};
@@ -294,6 +294,18 @@ export function renderMarkdown(markdown: IMarkdownString, options: MarkdownRende
294294
};
295295
}
296296

297+
function postProcessCodeBlockLanguageId(lang: string | undefined): string {
298+
if (!lang) {
299+
return '';
300+
}
301+
302+
const parts = lang.split(/[\s+|:|,|\{|\?]/, 1);
303+
if (parts.length) {
304+
return parts[0];
305+
}
306+
return lang;
307+
}
308+
297309
function resolveWithBaseUri(baseUri: URI, href: string): string {
298310
const hasScheme = /^\w[\w\d+.-]*:/.test(href);
299311
if (hasScheme) {

src/vs/base/test/browser/markdownRenderer.test.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ suite('MarkdownRenderer', () => {
6868
});
6969

7070
suite('Code block renderer', () => {
71-
const simpleCodeBlockRenderer = (code: string): Promise<HTMLElement> => {
71+
const simpleCodeBlockRenderer = (lang: string, code: string): Promise<HTMLElement> => {
7272
const element = document.createElement('code');
7373
element.textContent = code;
7474
return Promise.resolve(element);
@@ -115,6 +115,19 @@ suite('MarkdownRenderer', () => {
115115
}, 50);
116116
});
117117
});
118+
119+
test('Code blocks should use leading language id (#157793)', async () => {
120+
const markdown = { value: '```js some other stuff\n1 + 1;\n```' };
121+
const lang = await new Promise<string>(resolve => {
122+
renderMarkdown(markdown, {
123+
codeBlockRenderer: async (lang, value) => {
124+
resolve(lang);
125+
return simpleCodeBlockRenderer(lang, value);
126+
}
127+
});
128+
});
129+
assert.strictEqual(lang, 'js');
130+
});
118131
});
119132

120133
suite('ThemeIcons Support On', () => {

0 commit comments

Comments
 (0)