Skip to content

Commit

Permalink
Fixed several bugs (#1745)
Browse files Browse the repository at this point in the history
* fix: Fixed the single preview

* feat: Updated crossnote to 0.8.7

* feat: Added markdownFileExtensions setting
  • Loading branch information
shd101wyy authored Sep 6, 2023
1 parent 9acb810 commit b655568
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 31 deletions.
18 changes: 16 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
},
Expand Down Expand Up @@ -583,7 +596,8 @@
}
},
"activationEvents": [
"onLanguage:markdown"
"onLanguage:markdown",
"onLanguage:quarto"
],
"husky": {
"hooks": {
Expand All @@ -598,7 +612,7 @@
},
"dependencies": {
"@types/vfile": "^3.0.2",
"crossnote": "^0.8.6"
"crossnote": "^0.8.8"
},
"devDependencies": {
"@types/cheerio": "0.22.11",
Expand Down
4 changes: 4 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -81,6 +82,9 @@ export class MarkdownPreviewEnhancedConfig implements NotebookConfig {
);
const defaultConfig = getDefaultNotebookConfig();

this.markdownFileExtensions =
config.get<string[]>('markdownFileExtensions') ??
defaultConfig.markdownFileExtensions;
this.configPath = config.get<string>('configPath') ?? '';
this.usePandocParser = isVSCodeWebExtension()
? false // pandoc is not supported in web extension
Expand Down
52 changes: 31 additions & 21 deletions src/preview-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)) {
Expand All @@ -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];
}
Expand All @@ -235,16 +237,16 @@ export class PreviewProvider {
*/
public isPreviewOn(sourceUri: Uri) {
if (useSinglePreview()) {
return !!this.singlePreviewPanel;
return !!PreviewProvider.singlePreviewPanel;
} else {
return !!this.getPreview(sourceUri);
}
}

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 {
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -335,8 +342,8 @@ export class PreviewProvider {
);

if (isUsingSinglePreview) {
this.singlePreviewPanel = previewPanel;
this.singlePreviewPanelSourceUriTarget = sourceUri;
PreviewProvider.singlePreviewPanel = previewPanel;
PreviewProvider.singlePreviewPanelSourceUriTarget = sourceUri;
}
}

Expand Down Expand Up @@ -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[] = [];
Expand All @@ -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) {
Expand All @@ -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
);
}
}

Expand Down
21 changes: 17 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string[]>('markdownFileExtensions') ?? [];
const fileName = document.fileName;
const ext = path.extname(fileName).toLowerCase();
flag = markdownFileExtensions.includes(ext);
}

return flag;
}

/**
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit b655568

Please sign in to comment.