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