Skip to content

Commit 9e01742

Browse files
committed
Replace markdown-named-headers with custom version
Fixes #47537 Use our own version of markdown named headers. This fixes some bugs around handling duplicate headers
1 parent 7d95e3e commit 9e01742

File tree

3 files changed

+31
-17
lines changed

3 files changed

+31
-17
lines changed

extensions/markdown-language-features/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,6 @@
308308
"dependencies": {
309309
"highlight.js": "9.12.0",
310310
"markdown-it": "^8.4.1",
311-
"markdown-it-named-headers": "0.0.4",
312311
"vscode-extension-telemetry": "0.0.22",
313312
"vscode-nls": "^4.0.0"
314313
},

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

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ export class MarkdownEngine {
1717
private md?: MarkdownIt;
1818

1919
private firstLine?: number;
20-
2120
private currentDocument?: vscode.Uri;
21+
private _slugCount = new Map<string, number>();
2222

2323
public constructor(
2424
private readonly extensionPreviewResourceProvider: MarkdownContributions,
@@ -36,7 +36,6 @@ export class MarkdownEngine {
3636
private async getEngine(resource: vscode.Uri): Promise<MarkdownIt> {
3737
if (!this.md) {
3838
const hljs = await import('highlight.js');
39-
const mdnh = await import('markdown-it-named-headers');
4039
this.md = (await import('markdown-it'))({
4140
html: true,
4241
highlight: (str: string, lang?: string) => {
@@ -54,8 +53,6 @@ export class MarkdownEngine {
5453
}
5554
return `<code><div>${this.md!.utils.escapeHtml(str)}</div></code>`;
5655
}
57-
}).use(mdnh, {
58-
slugify: (header: string) => this.slugifier.fromHeading(header).value
5956
});
6057

6158
for (const plugin of this.extensionPreviewResourceProvider.markdownItPlugins) {
@@ -71,6 +68,7 @@ export class MarkdownEngine {
7168

7269
this.addLinkNormalizer(this.md);
7370
this.addLinkValidator(this.md);
71+
this.addNamedHeaders(this.md);
7472
}
7573

7674
const config = vscode.workspace.getConfiguration('markdown', resource);
@@ -101,13 +99,17 @@ export class MarkdownEngine {
10199
}
102100
this.currentDocument = document;
103101
this.firstLine = offset;
102+
this._slugCount = new Map<string, number>();
103+
104104
const engine = await this.getEngine(document);
105105
return engine.render(text);
106106
}
107107

108108
public async parse(document: vscode.Uri, source: string): Promise<Token[]> {
109109
const { text, offset } = this.stripFrontmatter(source);
110110
this.currentDocument = document;
111+
this._slugCount = new Map<string, number>();
112+
111113
const engine = await this.getEngine(document);
112114

113115
return engine.parse(text, {}).map(token => {
@@ -219,4 +221,29 @@ export class MarkdownEngine {
219221
return validateLink(link) || link.indexOf('file:') === 0;
220222
};
221223
}
224+
225+
private addNamedHeaders(md: any): void {
226+
const original = md.renderer.rules.heading_open;
227+
md.renderer.rules.heading_open = (tokens: any, idx: number, options: any, env: any, self: any) => {
228+
const title = tokens[idx + 1].children.reduce((acc: string, t: any) => acc + t.content, '');
229+
let slug = this.slugifier.fromHeading(title);
230+
231+
if (this._slugCount.has(slug.value)) {
232+
const count = this._slugCount.get(slug.value)!;
233+
this._slugCount.set(slug.value, count + 1);
234+
slug = this.slugifier.fromHeading(slug.value + '-' + (count + 1));
235+
} else {
236+
this._slugCount.set(slug.value, 0);
237+
}
238+
239+
tokens[idx].attrs = tokens[idx].attrs || [];
240+
tokens[idx].attrs.push(['id', slug.value]);
241+
242+
if (original) {
243+
return original(tokens, idx, options, env, self);
244+
} else {
245+
return self.renderToken(tokens, idx, options, env, self);
246+
}
247+
};
248+
}
222249
}

extensions/markdown-language-features/yarn.lock

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3895,13 +3895,6 @@ map-visit@^1.0.0:
38953895
dependencies:
38963896
object-visit "^1.0.0"
38973897

3898-
3899-
version "0.0.4"
3900-
resolved "https://registry.yarnpkg.com/markdown-it-named-headers/-/markdown-it-named-headers-0.0.4.tgz#82efc28324240a6b1e77b9aae501771d5f351c1f"
3901-
integrity sha1-gu/CgyQkCmsed7mq5QF3HV81HB8=
3902-
dependencies:
3903-
string "^3.0.1"
3904-
39053898
markdown-it@^8.4.1:
39063899
version "8.4.1"
39073900
resolved "https://registry.yarnpkg.com/markdown-it/-/markdown-it-8.4.1.tgz#206fe59b0e4e1b78a7c73250af9b34a4ad0aaf44"
@@ -5711,11 +5704,6 @@ string-width@^2.0.0, string-width@^2.1.0:
57115704
is-fullwidth-code-point "^2.0.0"
57125705
strip-ansi "^4.0.0"
57135706

5714-
string@^3.0.1:
5715-
version "3.3.1"
5716-
resolved "https://registry.yarnpkg.com/string/-/string-3.3.1.tgz#8d2757ec1c0e6c526796fbb6b14036a4098398b7"
5717-
integrity sha1-jSdX7BwObFJnlvu2sUA2pAmDmLc=
5718-
57195707
string_decoder@^1.0.0, string_decoder@~1.0.3:
57205708
version "1.0.3"
57215709
resolved "https://registry.yarnpkg.com/string_decoder/-/string_decoder-1.0.3.tgz#0fc67d7c141825de94282dd536bec6b9bce860ab"

0 commit comments

Comments
 (0)