8000 Align rendered header id generation with markdown language service (#… · githubnext/vscode@0cba3e6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0cba3e6

Browse files
authored
Align rendered header id generation with markdown language service (microsoft#221742)
Fixes microsoft#220064
1 parent fbcb074 commit 0cba3e6

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

extensions/markdown-language-features/src/markdownEngine.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ export class MarkdownItEngine implements IMdParser {
313313
private _addNamedHeaders(md: MarkdownIt): void {
314314
const original = md.renderer.rules.heading_open;
315315
md.renderer.rules.heading_open = (tokens: Token[], idx: number, options, env, self) => {
316-
const title = tokens[idx + 1].children!.reduce<string>((acc, t) => acc + t.content, '');
316+
const title = this._tokenToPlainText(tokens[idx + 1]);
317317
let slug = this.slugifier.fromHeading(title);
318318

319319
if (this._slugCount.has(slug.value)) {
@@ -334,6 +334,21 @@ export class MarkdownItEngine implements IMdParser {
334334
};
335335
}
336336

337+
private _tokenToPlainText(token: Token): string {
338+
if (token.children) {
339+
return token.children.map(x => this._tokenToPlainText(x)).join('');
340+
}
341+
342+
switch (token.type) {
343+
case 'text':
344+
case 'emoji':
345+
case 'code_inline':
346+
return token.content;
347+
default:
348+
return '';
349+
}
350+
}
351+
337352
private _addLinkRenderer(md: MarkdownIt): void {
338353
const original = md.renderer.rules.link_open;
339354

@@ -441,4 +456,4 @@ function normalizeHighlightLang(lang: string | undefined) {
441456
default:
442457
return lang;
443458
}
444-
}
459+
}

0 commit comments

Comments
 (0)
0