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:
Matt Bierner
2017-01-27 14:36:28 -08:00
committed by GitHub
parent 3b2ac054f9
commit f7697a7f37
4 changed files with 114 additions and 30 deletions

View File

@@ -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);
});
}
}