diff --git a/extensions/markdown/src/markdownEngine.ts b/extensions/markdown/src/markdownEngine.ts index d41ced769e7eddf7273f09953d29100f478035c2..b2e41133330374c4fc0a9dd709757228941091e9 100644 --- a/extensions/markdown/src/markdownEngine.ts +++ b/extensions/markdown/src/markdownEngine.ts @@ -35,7 +35,7 @@ export class MarkdownEngine { } } - private async getEngine(): Promise { + private async getEngine(resource: vscode.Uri): Promise { if (!this.md) { const hljs = await import('highlight.js'); const mdnh = await import('markdown-it-named-headers'); @@ -66,10 +66,10 @@ export class MarkdownEngine { this.addLinkValidator(this.md); } - const config = vscode.workspace.getConfiguration('markdown'); + const config = vscode.workspace.getConfiguration('markdown', resource); this.md.set({ - breaks: config.get('preview.breaks', false), - linkify: config.get('preview.linkify', true) + breaks: config.get('preview.breaks', false), + linkify: config.get('preview.linkify', true) }); return this.md; } @@ -94,14 +94,14 @@ export class MarkdownEngine { } this.currentDocument = document; this.firstLine = offset; - const engine = await this.getEngine(); + const engine = await this.getEngine(document); return engine.render(text); } public async parse(document: vscode.Uri, source: string): Promise { const { text, offset } = this.stripFrontmatter(source); this.currentDocument = document; - const engine = await this.getEngine(); + const engine = await this.getEngine(document); return engine.parse(text, {}).map(token => { if (token.map) { diff --git a/extensions/markdown/src/previewContentProvider.ts b/extensions/markdown/src/previewContentProvider.ts index 64d51eccd1f3ade2d3184f2b821a1e18cc774cca..0a1c17029947b01497edacbd0adc6bdd2f1e8b5d 100644 --- a/extensions/markdown/src/previewContentProvider.ts +++ b/extensions/markdown/src/previewContentProvider.ts @@ -59,7 +59,7 @@ class MarkdownPreviewConfig { private constructor(resource: vscode.Uri) { const editorConfig = vscode.workspace.getConfiguration('editor', resource); const markdownConfig = vscode.workspace.getConfiguration('markdown', resource); - const markdownEditorConfig = vscode.workspace.getConfiguration('[markdown]', resource); + const markdownEditorConfig = vscode.workspace.getConfiguration('[markdown]'); this.scrollBeyondLastLine = editorConfig.get('scrollBeyondLastLine', false); @@ -107,9 +107,41 @@ class MarkdownPreviewConfig { [key: string]: any; } +class PreviewConfigManager { + private previewConfigurationsForWorkspaces = new Map(); + + public loadAndCacheConfiguration( + resource: vscode.Uri + ) { + const config = MarkdownPreviewConfig.getConfigForResource(resource); + this.previewConfigurationsForWorkspaces.set(this.getKey(resource), config); + return config; + } + + public shouldUpdateConfiguration( + resource: vscode.Uri + ): boolean { + const key = this.getKey(resource); + const currentConfig = this.previewConfigurationsForWorkspaces.get(key); + const newConfig = MarkdownPreviewConfig.getConfigForResource(resource); + return (!currentConfig || !currentConfig.isEqualTo(newConfig)); + } + + private getKey( + resource: vscode.Uri + ): string { + const folder = vscode.workspace.getWorkspaceFolder(resource); + if (!folder) { + return ''; + } + return folder.uri.toString(); + } +} + export class MDDocumentContentProvider implements vscode.TextDocumentContentProvider { private _onDidChange = new vscode.EventEmitter(); private _waiting: boolean = false; + private previewConfigurations = new PreviewConfigManager(); private extraStyles: Array = []; private extraScripts: Array = []; @@ -206,7 +238,7 @@ export class MDDocumentContentProvider implements vscode.TextDocumentContentProv } const document = await vscode.workspace.openTextDocument(sourceUri); - const config = MarkdownPreviewConfig.getConfigForResource(sourceUri); + const config = this.previewConfigurations.loadAndCacheConfiguration(sourceUri); const initialData = { previewUri: uri.toString(), @@ -247,7 +279,10 @@ export class MDDocumentContentProvider implements vscode.TextDocumentContentProv // update all generated md documents for (const document of vscode.workspace.textDocuments) { if (document.uri.scheme === 'markdown') { - this.update(document.uri); + const sourceUri = vscode.Uri.parse(document.uri.query); + if (this.previewConfigurations.shouldUpdateConfiguration(sourceUri)) { + this.update(document.uri); + } } } }