mirror of
https://github.com/microsoft/vscode.git
synced 2025-12-27 21:52:59 +00: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,
|
||||
|
||||
@@ -106,4 +106,25 @@ suite('markdown.TableOfContentsProvider', () => {
|
||||
assert.strictEqual((await provider.lookup('Заголовок-header-3'))!.line, 4);
|
||||
assert.strictEqual((await provider.lookup('Заголовок'))!.line, 5);
|
||||
});
|
||||
|
||||
test('Lookup should support suffixes for repeated headers', async () => {
|
||||
const doc = new InMemoryDocument(testFileName, `# a\n# a\n## a`);
|
||||
const provider = new TableOfContentsProvider(createNewMarkdownEngine(), doc);
|
||||
|
||||
{
|
||||
const entry = await provider.lookup('a');
|
||||
assert.ok(entry);
|
||||
assert.strictEqual(entry!.line, 0);
|
||||
}
|
||||
{
|
||||
const entry = await provider.lookup('a-1');
|
||||
assert.ok(entry);
|
||||
assert.strictEqual(entry!.line, 1);
|
||||
}
|
||||
{
|
||||
const entry = await provider.lookup('a-2');
|
||||
assert.ok(entry);
|
||||
assert.strictEqual(entry!.line, 2);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user