From b6555680b04867b39360a44bb6f22928faaf66c8 Mon Sep 17 00:00:00 2001 From: Yiyi Wang Date: Wed, 6 Sep 2023 16:47:36 +0800 Subject: [PATCH] Fixed several bugs (#1745) * fix: Fixed the single preview * feat: Updated crossnote to 0.8.7 * feat: Added markdownFileExtensions setting --- package.json | 18 ++++++++++++-- src/config.ts | 4 ++++ src/preview-provider.ts | 52 ++++++++++++++++++++++++----------------- src/utils.ts | 21 +++++++++++++---- yarn.lock | 8 +++---- 5 files changed, 72 insertions(+), 31 deletions(-) diff --git a/package.json b/package.json index 25920fd..415606e 100644 --- a/package.json +++ b/package.json @@ -533,6 +533,19 @@ "description": "The URL of the Kroki server to use. ", "default": "https://kroki.io", "type": "string" + }, + "markdown-preview-enhanced.markdownFileExtensions": { + "description": "Markdown file extensions. This is used to determine whether to show the preview button in the markdown file context menu.", + "default": [ + ".md", + ".markdown", + ".mdown", + ".mkdn", + ".mkd", + ".rmd", + ".qmd" + ], + "type": "array" } } }, @@ -583,7 +596,8 @@ } }, "activationEvents": [ - "onLanguage:markdown" + "onLanguage:markdown", + "onLanguage:quarto" ], "husky": { "hooks": { @@ -598,7 +612,7 @@ }, "dependencies": { "@types/vfile": "^3.0.2", - "crossnote": "^0.8.6" + "crossnote": "^0.8.8" }, "devDependencies": { "@types/cheerio": "0.22.11", diff --git a/src/config.ts b/src/config.ts index 79a1bc6..cbbba9b 100644 --- a/src/config.ts +++ b/src/config.ts @@ -23,6 +23,7 @@ export class MarkdownPreviewEnhancedConfig implements NotebookConfig { return new MarkdownPreviewEnhancedConfig(); } + public readonly markdownFileExtensions: string[]; public readonly configPath: string; public readonly usePandocParser: boolean; public readonly breakOnSingleNewLine: boolean; @@ -81,6 +82,9 @@ export class MarkdownPreviewEnhancedConfig implements NotebookConfig { ); const defaultConfig = getDefaultNotebookConfig(); + this.markdownFileExtensions = + config.get('markdownFileExtensions') ?? + defaultConfig.markdownFileExtensions; this.configPath = config.get('configPath') ?? ''; this.usePandocParser = isVSCodeWebExtension() ? false // pandoc is not supported in web extension diff --git a/src/preview-provider.ts b/src/preview-provider.ts index c23fa93..5b84424 100644 --- a/src/preview-provider.ts +++ b/src/preview-provider.ts @@ -109,8 +109,8 @@ export class PreviewProvider { vscode.TextEditor > = new Map(); - private singlePreviewPanel: vscode.WebviewPanel | null; - private singlePreviewPanelSourceUriTarget: Uri | null; + private static singlePreviewPanel: vscode.WebviewPanel | null; + private static singlePreviewPanelSourceUriTarget: Uri | null; /** * The key is markdown file fsPath @@ -207,7 +207,9 @@ export class PreviewProvider { // refresh iframes if (useSinglePreview()) { - this.refreshPreviewPanel(this.singlePreviewPanelSourceUriTarget); + this.refreshPreviewPanel( + PreviewProvider.singlePreviewPanelSourceUriTarget, + ); } else { for (const key in this.previewMaps) { if (this.previewMaps.hasOwnProperty(key)) { @@ -223,7 +225,7 @@ export class PreviewProvider { */ public getPreview(sourceUri: Uri): vscode.WebviewPanel | null { if (useSinglePreview()) { - return this.singlePreviewPanel; + return PreviewProvider.singlePreviewPanel; } else { return this.previewMaps[sourceUri.fsPath]; } @@ -235,7 +237,7 @@ export class PreviewProvider { */ public isPreviewOn(sourceUri: Uri) { if (useSinglePreview()) { - return !!this.singlePreviewPanel; + return !!PreviewProvider.singlePreviewPanel; } else { return !!this.getPreview(sourceUri); } @@ -243,8 +245,8 @@ export class PreviewProvider { public destroyPreview(sourceUri: Uri) { if (useSinglePreview()) { - this.singlePreviewPanel = null; - this.singlePreviewPanelSourceUriTarget = null; + PreviewProvider.singlePreviewPanel = null; + PreviewProvider.singlePreviewPanelSourceUriTarget = null; this.preview2EditorMap = new Map(); this.previewMaps = {}; } else { @@ -272,17 +274,22 @@ export class PreviewProvider { ) { const isUsingSinglePreview = useSinglePreview(); let previewPanel: vscode.WebviewPanel; - if (isUsingSinglePreview && this.singlePreviewPanel) { - const oldResourceRoot = this.singlePreviewPanelSourceUriTarget - ? getWorkspaceFolderUri(this.singlePreviewPanelSourceUriTarget) + if (isUsingSinglePreview && PreviewProvider.singlePreviewPanel) { + const oldResourceRoot = PreviewProvider.singlePreviewPanelSourceUriTarget + ? getWorkspaceFolderUri( + PreviewProvider.singlePreviewPanelSourceUriTarget, + ) : undefined; const newResourceRoot = getWorkspaceFolderUri(sourceUri); if (oldResourceRoot?.fsPath !== newResourceRoot.fsPath) { - this.singlePreviewPanel.dispose(); + const singlePreview = PreviewProvider.singlePreviewPanel; + PreviewProvider.singlePreviewPanel = null; + PreviewProvider.singlePreviewPanelSourceUriTarget = null; + singlePreview.dispose(); return this.initPreview(sourceUri, editor, viewOptions); } else { - previewPanel = this.singlePreviewPanel; - this.singlePreviewPanelSourceUriTarget = sourceUri; + previewPanel = PreviewProvider.singlePreviewPanel; + PreviewProvider.singlePreviewPanelSourceUriTarget = sourceUri; } } else if (this.previewMaps[sourceUri.fsPath]) { previewPanel = this.previewMaps[sourceUri.fsPath]; @@ -335,8 +342,8 @@ export class PreviewProvider { ); if (isUsingSinglePreview) { - this.singlePreviewPanel = previewPanel; - this.singlePreviewPanelSourceUriTarget = sourceUri; + PreviewProvider.singlePreviewPanel = previewPanel; + PreviewProvider.singlePreviewPanelSourceUriTarget = sourceUri; } } @@ -382,8 +389,8 @@ export class PreviewProvider { */ public closeAllPreviews(singlePreview: boolean) { if (singlePreview) { - if (this.singlePreviewPanel) { - this.singlePreviewPanel.dispose(); + if (PreviewProvider.singlePreviewPanel) { + PreviewProvider.singlePreviewPanel.dispose(); } } else { const previewPanels: vscode.WebviewPanel[] = []; @@ -402,8 +409,8 @@ export class PreviewProvider { this.previewMaps = {}; this.preview2EditorMap = new Map(); // this.engineMaps = {}; - this.singlePreviewPanel = null; - this.singlePreviewPanelSourceUriTarget = null; + PreviewProvider.singlePreviewPanel = null; + PreviewProvider.singlePreviewPanelSourceUriTarget = null; } public previewPostMessage(sourceUri: Uri, message: any) { @@ -414,10 +421,13 @@ export class PreviewProvider { } public previewHasTheSameSingleSourceUri(sourceUri: Uri) { - if (!this.singlePreviewPanelSourceUriTarget) { + if (!PreviewProvider.singlePreviewPanelSourceUriTarget) { return false; } else { - return this.singlePreviewPanelSourceUriTarget.fsPath === sourceUri.fsPath; + return ( + PreviewProvider.singlePreviewPanelSourceUriTarget.fsPath === + sourceUri.fsPath + ); } } diff --git a/src/utils.ts b/src/utils.ts index fe22ff0..e5782b1 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -64,10 +64,23 @@ function getGlobalConfigPath(): string { export const globalConfigPath = getGlobalConfigPath(); export function isMarkdownFile(document: vscode.TextDocument) { - return ( - document.languageId === 'markdown' && - document.uri.scheme !== 'markdown-preview-enhanced' - ); // prevent processing of own documents + let flag = + (document.languageId === 'markdown' || document.languageId === 'quarto') && + document.uri.scheme !== 'markdown-preview-enhanced'; // prevent processing of own documents + + if (!flag) { + // Check file extension + const config = vscode.workspace.getConfiguration( + 'markdown-preview-enhanced', + ); + const markdownFileExtensions = + config.get('markdownFileExtensions') ?? []; + const fileName = document.fileName; + const ext = path.extname(fileName).toLowerCase(); + flag = markdownFileExtensions.includes(ext); + } + + return flag; } /** diff --git a/yarn.lock b/yarn.lock index f401a66..ffff4f4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1809,10 +1809,10 @@ cross-spawn@^7.0.0, cross-spawn@^7.0.2, cross-spawn@^7.0.3: shebang-command "^2.0.0" which "^2.0.1" -crossnote@^0.8.6: - version "0.8.6" - resolved "https://registry.yarnpkg.com/crossnote/-/crossnote-0.8.6.tgz#b2bd05c7cc223044c5faddf9c5a6b81a1f2c3e72" - integrity sha512-Vl8bFmuqAq3kkfeJT46MMA3uc7Qb9tlG+9dg//sEMq2mWdF3tZVcLTma9ZaFClCRL7rN+o3/pCBSRuVr0VlMrg== +crossnote@^0.8.8: + version "0.8.8" + resolved "https://registry.yarnpkg.com/crossnote/-/crossnote-0.8.8.tgz#2f69c180177321644d43b789103d0e2ac57de4fd" + integrity sha512-w4z4xGTY7PFPBzPh0DLXyETY7IV9ewM18LnZmO7GIInHNvoZ9xEvrRL2qXewqgb7g6Zip9W31TNyPj4QMmpN6A== dependencies: "@viz-js/viz" "^3.1.0" async-mutex "^0.4.0"