mirror of
https://github.com/Microsoft/vscode
synced 2024-09-13 21:55:38 +00:00
Load Markdown and XML parsers on demand (fixes #33141)
This commit is contained in:
parent
e15c137204
commit
1488aafa09
|
@ -8,8 +8,7 @@ import * as path from 'path';
|
|||
|
||||
import { parseTree, findNodeAtLocation, Node as JsonNode } from 'jsonc-parser';
|
||||
import * as nls from 'vscode-nls';
|
||||
import * as MarkdownIt from 'markdown-it';
|
||||
import * as parse5 from 'parse5';
|
||||
import * as MarkdownItType from 'markdown-it';
|
||||
|
||||
import { languages, workspace, Disposable, ExtensionContext, TextDocument, Uri, Diagnostic, Range, DiagnosticSeverity, Position } from 'vscode';
|
||||
|
||||
|
@ -33,7 +32,7 @@ enum Context {
|
|||
}
|
||||
|
||||
interface TokenAndPosition {
|
||||
token: MarkdownIt.Token;
|
||||
token: MarkdownItType.Token;
|
||||
begin: number;
|
||||
end: number;
|
||||
}
|
||||
|
@ -53,7 +52,7 @@ export class ExtensionLinter {
|
|||
private packageJsonQ = new Set<TextDocument>();
|
||||
private readmeQ = new Set<TextDocument>();
|
||||
private timer: NodeJS.Timer;
|
||||
private markdownIt = new MarkdownIt();
|
||||
private markdownIt: MarkdownItType.MarkdownIt;
|
||||
|
||||
constructor(private context: ExtensionContext) {
|
||||
this.disposables.push(
|
||||
|
@ -148,8 +147,11 @@ export class ExtensionLinter {
|
|||
}
|
||||
|
||||
const text = document.getText();
|
||||
if (!this.markdownIt) {
|
||||
this.markdownIt = new (await import('markdown-it'));
|
||||
}
|
||||
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 => {
|
||||
if (token.map) {
|
||||
const tokenBegin = document.offsetAt(new Position(token.map[0], 0));
|
||||
|
@ -192,8 +194,9 @@ export class ExtensionLinter {
|
|||
});
|
||||
|
||||
let svgStart: Diagnostic;
|
||||
tokensAndPositions.filter(tnp => tnp.token.type === 'text' && tnp.token.content)
|
||||
.map(tnp => {
|
||||
for (const tnp of tokensAndPositions) {
|
||||
if (tnp.token.type === 'text' && tnp.token.content) {
|
||||
const parse5 = await import('parse5');
|
||||
const parser = new parse5.SAXParser({ locationInfo: true });
|
||||
parser.on('startTag', (name, attrs, selfClosing, location) => {
|
||||
if (name === 'img') {
|
||||
|
@ -220,13 +223,14 @@ export class ExtensionLinter {
|
|||
});
|
||||
parser.write(tnp.token.content);
|
||||
parser.end();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
const tokenBegin = text.indexOf(content, begin);
|
||||
if (tokenBegin !== -1) {
|
||||
|
|
Loading…
Reference in a new issue