提交 1488aafa 编写于 作者: C Christof Marti

Load Markdown and XML parsers on demand (fixes #33141)

上级 e15c1372
...@@ -8,8 +8,7 @@ import * as path from 'path'; ...@@ -8,8 +8,7 @@ import * as path from 'path';
import { parseTree, findNodeAtLocation, Node as JsonNode } from 'jsonc-parser'; import { parseTree, findNodeAtLocation, Node as JsonNode } from 'jsonc-parser';
import * as nls from 'vscode-nls'; import * as nls from 'vscode-nls';
import * as MarkdownIt from 'markdown-it'; import * as MarkdownItType from 'markdown-it';
import * as parse5 from 'parse5';
import { languages, workspace, Disposable, ExtensionContext, TextDocument, Uri, Diagnostic, Range, DiagnosticSeverity, Position } from 'vscode'; import { languages, workspace, Disposable, ExtensionContext, TextDocument, Uri, Diagnostic, Range, DiagnosticSeverity, Position } from 'vscode';
...@@ -33,7 +32,7 @@ enum Context { ...@@ -33,7 +32,7 @@ enum Context {
} }
interface TokenAndPosition { interface TokenAndPosition {
token: MarkdownIt.Token; token: MarkdownItType.Token;
begin: number; begin: number;
end: number; end: number;
} }
...@@ -53,7 +52,7 @@ export class ExtensionLinter { ...@@ -53,7 +52,7 @@ export class ExtensionLinter {
private packageJsonQ = new Set<TextDocument>(); private packageJsonQ = new Set<TextDocument>();
private readmeQ = new Set<TextDocument>(); private readmeQ = new Set<TextDocument>();
private timer: NodeJS.Timer; private timer: NodeJS.Timer;
private markdownIt = new MarkdownIt(); private markdownIt: MarkdownItType.MarkdownIt;
constructor(private context: ExtensionContext) { constructor(private context: ExtensionContext) {
this.disposables.push( this.disposables.push(
...@@ -148,8 +147,11 @@ export class ExtensionLinter { ...@@ -148,8 +147,11 @@ export class ExtensionLinter {
} }
const text = document.getText(); const text = document.getText();
if (!this.markdownIt) {
this.markdownIt = new (await import('markdown-it'));
}
const tokens = this.markdownIt.parse(text, {}); const tokens = this.markdownIt.parse(text, {});
const tokensAndPositions = (function toTokensAndPositions(this: ExtensionLinter, tokens: MarkdownIt.Token[], begin = 0, end = text.length): TokenAndPosition[] { const tokensAndPositions = (function toTokensAndPositions(this: ExtensionLinter, tokens: MarkdownItType.Token[], begin = 0, end = text.length): TokenAndPosition[] {
const tokensAndPositions = tokens.map<TokenAndPosition>(token => { const tokensAndPositions = tokens.map<TokenAndPosition>(token => {
if (token.map) { if (token.map) {
const tokenBegin = document.offsetAt(new Position(token.map[0], 0)); const tokenBegin = document.offsetAt(new Position(token.map[0], 0));
...@@ -192,8 +194,9 @@ export class ExtensionLinter { ...@@ -192,8 +194,9 @@ export class ExtensionLinter {
}); });
let svgStart: Diagnostic; let svgStart: Diagnostic;
tokensAndPositions.filter(tnp => tnp.token.type === 'text' && tnp.token.content) for (const tnp of tokensAndPositions) {
.map(tnp => { if (tnp.token.type === 'text' && tnp.token.content) {
const parse5 = await import('parse5');
const parser = new parse5.SAXParser({ locationInfo: true }); const parser = new parse5.SAXParser({ locationInfo: true });
parser.on('startTag', (name, attrs, selfClosing, location) => { parser.on('startTag', (name, attrs, selfClosing, location) => {
if (name === 'img') { if (name === 'img') {
...@@ -220,13 +223,14 @@ export class ExtensionLinter { ...@@ -220,13 +223,14 @@ export class ExtensionLinter {
}); });
parser.write(tnp.token.content); parser.write(tnp.token.content);
parser.end(); parser.end();
}); }
}
this.diagnosticsCollection.set(document.uri, diagnostics); this.diagnosticsCollection.set(document.uri, diagnostics);
}; };
} }
private locateToken(text: string, begin: number, end: number, token: MarkdownIt.Token, content: string) { private locateToken(text: string, begin: number, end: number, token: MarkdownItType.Token, content: string) {
if (content) { if (content) {
const tokenBegin = text.indexOf(content, begin); const tokenBegin = text.indexOf(content, begin);
if (tokenBegin !== -1) { if (tokenBegin !== -1) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册