提交 dc614f21 编写于 作者: M Matt Bierner 提交者: GitHub

Cleanup markdown extension (#18951)

* Splits symbol provider to own file
* Removes duplicate frontmatter logic
上级 c486e019
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import * as vscode from 'vscode';
import { MarkdownEngine } from './markdownEngine';
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);
});
}
}
\ No newline at end of file
......@@ -10,6 +10,7 @@ import * as path from 'path';
import TelemetryReporter from 'vscode-extension-telemetry';
import { MarkdownEngine } from './markdownEngine';
import DocumentLinkProvider from './documentLinkProvider';
import MDDocumentSymbolProvider from './documentSymbolProvider';
interface IPackageInfo {
name: string;
......@@ -19,8 +20,6 @@ interface IPackageInfo {
var telemetryReporter: TelemetryReporter | null;
const FrontMatterRegex = /^---\s*(.|\s)*?---\s*/;
export function activate(context: vscode.ExtensionContext) {
let packageInfo = getPackageInfo(context);
......@@ -248,20 +247,7 @@ class MDDocumentContentProvider implements vscode.TextDocumentContentProvider {
const scrollBeyondLastLine = vscode.workspace.getConfiguration('editor')['scrollBeyondLastLine'];
const wordWrap = vscode.workspace.getConfiguration('editor')['wordWrap'];
const enablePreviewSync = vscode.workspace.getConfiguration('markdown').get('preview.experimentalSyncronizationEnabled', true);
const previewFrontMatter = vscode.workspace.getConfiguration('markdown')['previewFrontMatter'];
const text = document.getText();
let contents;
let lineOffset = 0;
if (previewFrontMatter === 'hide') {
const frontMatter = text.match(FrontMatterRegex);
if (frontMatter) {
lineOffset = (frontMatter[0].match(/\n/g) || []).length;
}
contents = text.replace(FrontMatterRegex, '');
} else {
contents = text;
}
let initialLine = 0;
const editor = vscode.window.activeTextEditor;
......@@ -269,8 +255,7 @@ class MDDocumentContentProvider implements vscode.TextDocumentContentProvider {
initialLine = editor.selection.start.line;
}
const body = this.engine.render(sourceUri, lineOffset, contents);
const body = this.engine.render(sourceUri, previewFrontMatter === 'hide', document.getText());
return `<!DOCTYPE html>
<html>
......@@ -311,39 +296,3 @@ class MDDocumentContentProvider implements vscode.TextDocumentContentProvider {
}
}
}
class MDDocumentSymbolProvider implements vscode.DocumentSymbolProvider {
constructor(private engine: MarkdownEngine) { }
provideDocumentSymbols(document: vscode.TextDocument): vscode.ProviderResult<vscode.SymbolInformation[]> {
let offset = 0;
let text = document.getText();
const frontMatterMatch = FrontMatterRegex.exec(text);
if (frontMatterMatch) {
const frontMatter = frontMatterMatch[0];
offset = frontMatter.split(/\r\n|\n|\r/g).length - 1;
text = text.substr(frontMatter.length);
}
const tokens = this.engine.parse(text);
const headings = tokens.filter(token => token.type === 'heading_open');
return headings.map(heading => {
const lineNumber = heading.map[0];
const line = document.lineAt(lineNumber + offset);
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);
});
}
}
\ No newline at end of file
......@@ -21,6 +21,8 @@ interface MarkdownIt {
utils: any;
}
const FrontMatterRegex = /^---\s*(.|\s)*?---\s*/;
export class MarkdownEngine {
private md: MarkdownIt;
......@@ -55,14 +57,39 @@ export class MarkdownEngine {
return this.md;
}
public render(document: vscode.Uri, firstLine: number, text: string): string {
private stripFrontmatter(text: string): { text: string, offset: number } {
let offset = 0;
const frontMatterMatch = FrontMatterRegex.exec(text);
if (frontMatterMatch) {
const frontMatter = frontMatterMatch[0];
offset = frontMatter.split(/\r\n|\n|\r/g).length - 1;
text = text.substr(frontMatter.length);
}
return { text, offset };
}
public render(document: vscode.Uri, stripFrontmatter: boolean, text: string): string {
let offset = 0;
if (stripFrontmatter) {
const markdownContent = this.stripFrontmatter(text);
offset = markdownContent.offset;
text = markdownContent.text;
}
this.currentDocument = document;
this.firstLine = firstLine;
this.firstLine = offset;
return this.engine.render(text);
}
public parse(text: string): IToken[] {
return this.engine.parse(text);
public parse(source: string): IToken[] {
const {text, offset} = this.stripFrontmatter(source);
return this.engine.parse(text).map(token => {
if (token.map) {
token.map[0] += offset;
}
return token;
});
}
private addLineNumberRenderer(md: any, ruleName: string): void {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册