From 1488aafa096630a65f6ff4602547aa59f9e0cb7f Mon Sep 17 00:00:00 2001 From: Christof Marti Date: Fri, 8 Sep 2017 09:45:17 -0700 Subject: [PATCH] Load Markdown and XML parsers on demand (fixes #33141) --- .../extension-editing/src/extensionLinter.ts | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/extensions/extension-editing/src/extensionLinter.ts b/extensions/extension-editing/src/extensionLinter.ts index d06332e8221..66e82c78191 100644 --- a/extensions/extension-editing/src/extensionLinter.ts +++ b/extensions/extension-editing/src/extensionLinter.ts @@ -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(); private readmeQ = new Set(); 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(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) {