mirror of
https://github.com/microsoft/vscode.git
synced 2026-04-22 17:48:56 +01:00
Support markdown link navigation when duplicate slugs exist
Fixes #59711 For a md document: ```md # a # a - [a](#a) - [next a](#a-1) ``` You can now click on the second link in the editor to navigate to the second `a` header. It is identified by being suffixed with `-1`.
This commit is contained in:
@@ -51,11 +51,23 @@ export class TableOfContentsProvider {
|
||||
const toc: TocEntry[] = [];
|
||||
const tokens = await this.engine.parse(document.uri, document.getText());
|
||||
|
||||
const slugCount = new Map<string, number>();
|
||||
|
||||
for (const heading of tokens.filter(token => token.type === 'heading_open')) {
|
||||
const lineNumber = heading.map[0];
|
||||
const line = document.lineAt(lineNumber);
|
||||
|
||||
let slug = githubSlugifier.fromHeading(line.text);
|
||||
if (slugCount.has(slug.value)) {
|
||||
const count = slugCount.get(slug.value)!;
|
||||
slugCount.set(slug.value, count + 1);
|
||||
slug = githubSlugifier.fromHeading(slug.value + '-' + (count + 1));
|
||||
} else {
|
||||
slugCount.set(slug.value, 0);
|
||||
}
|
||||
|
||||
toc.push({
|
||||
slug: githubSlugifier.fromHeading(line.text),
|
||||
slug,
|
||||
text: TableOfContentsProvider.getHeaderText(line.text),
|
||||
level: TableOfContentsProvider.getHeaderLevel(heading.markup),
|
||||
line: lineNumber,
|
||||
|
||||
Reference in New Issue
Block a user