mirror of
https://github.com/microsoft/vscode.git
synced 2026-05-02 14:31:31 +01:00
Support In-Document links inside of the markdown editor (#19411)
* Support In Document links inside of the markdown editor Fixes #17288 * Cleaning up code to reduce duplication
This commit is contained in:
@@ -8,28 +8,16 @@
|
||||
import * as vscode from 'vscode';
|
||||
|
||||
import { MarkdownEngine } from './markdownEngine';
|
||||
import { TableOfContentProvider } from './tableOfContentsProvider';
|
||||
|
||||
export default class MDDocumentSymbolProvider implements vscode.DocumentSymbolProvider {
|
||||
|
||||
constructor(private engine: MarkdownEngine) { }
|
||||
|
||||
provideDocumentSymbols(document: vscode.TextDocument): vscode.ProviderResult<vscode.SymbolInformation[]> {
|
||||
const tokens = this.engine.parse(document.getText());
|
||||
const headings = tokens.filter(token => token.type === 'heading_open');
|
||||
|
||||
return headings.map(heading => {
|
||||
const lineNumber = heading.map[0];
|
||||
const line = document.lineAt(lineNumber);
|
||||
const location = new vscode.Location(document.uri, line.range);
|
||||
|
||||
// # Header => 'Header'
|
||||
// ## Header ## => 'Header'
|
||||
// ## Header #### => 'Header'
|
||||
// Header ## => 'Header ##'
|
||||
// =========
|
||||
const text = line.text.replace(/^\s*(#)+\s*(.*?)\s*\1*$/, '$2');
|
||||
|
||||
return new vscode.SymbolInformation(text, vscode.SymbolKind.Module, '', location);
|
||||
const toc = new TableOfContentProvider(this.engine, document);
|
||||
return toc.getToc().map(entry => {
|
||||
return new vscode.SymbolInformation(entry.text, vscode.SymbolKind.Module, '', entry.location);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user