提交 1c6f2f9a 编写于 作者: Y Yu Zhang 提交者: Matt Bierner

🐛 include setext heading levels (Markdown) (#32987)

* 🐛 include setext heading levels

* 📝 typo

* 💚 use `===`
上级 949b9f1a
......@@ -19,7 +19,7 @@ export default class MDDocumentSymbolProvider implements vscode.DocumentSymbolPr
public async provideDocumentSymbols(document: vscode.TextDocument): Promise<vscode.SymbolInformation[]> {
const toc = await new TableOfContentsProvider(this.engine, document).getToc();
return toc.map(entry => {
return new vscode.SymbolInformation(entry.text, vscode.SymbolKind.Namespace, '', entry.location);
return new vscode.SymbolInformation('#'.repeat(entry.level) + ' ' + entry.text, vscode.SymbolKind.Namespace, '', entry.location);
});
}
}
\ No newline at end of file
......@@ -12,6 +12,7 @@ import { MarkdownEngine } from './markdownEngine';
export interface TocEntry {
slug: string;
text: string;
level: number;
line: number;
location: vscode.Location;
}
......@@ -53,10 +54,12 @@ export class TableOfContentsProvider {
const lineNumber = heading.map[0];
const line = document.lineAt(lineNumber);
const href = TableOfContentsProvider.slugify(line.text);
const level = TableOfContentsProvider.getHeaderLevel(heading.markup);
if (href) {
toc.push({
slug: href,
text: TableOfContentsProvider.getHeaderText(line.text),
level: level,
line: lineNumber,
location: new vscode.Location(document.uri, line.range)
});
......@@ -65,14 +68,24 @@ export class TableOfContentsProvider {
return toc;
}
private static getHeaderLevel(markup: string): number {
if (markup === '=') {
return 1;
} else if (markup === '-') {
return 2;
} else { // '#', '##', ...
return markup.length;
}
}
private static getHeaderText(header: string): string {
return header.replace(/^\s*(#+)\s*(.*?)\s*\1*$/, (_, level, word) => `${level} ${word.trim()}`);
return header.replace(/^\s*#+\s*(.*?)\s*\1*$/, (_, word) => `${word.trim()}`);
}
public static slugify(header: string): string {
return encodeURI(header.trim()
.toLowerCase()
.replace(/[\]\[\!\"\#\$\%\&\'\(\)\*\+\,\.\/\:\;\<\=\>\?\@\\\^\_\{\|\}\~]/g, '')
.replace(/[\]\[\!\"\#\$\%\&\'\(\)\*\+\,\.\/\:\;\<\=\>\?\@\\\^\_\{\|\}\~\`]/g, '')
.replace(/\s+/g, '-')
.replace(/^\-+/, '')
.replace(/\-+$/, ''));
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册