From b292d97272ae09da878e5f1a5a9d328fd37137da Mon Sep 17 00:00:00 2001 From: windingwind <33902321+windingwind@users.noreply.github.com> Date: Mon, 11 Sep 2023 16:12:12 +0800 Subject: [PATCH] fix: #686 --- src/modules/convert/api.ts | 9 +++++++-- src/modules/export/markdown.ts | 4 ++-- src/modules/sync/api.ts | 5 ++++- src/modules/sync/hooks.ts | 2 +- src/modules/sync/infoWindow.ts | 6 ++---- src/modules/sync/managerWindow.ts | 5 +++-- src/utils/str.ts | 11 ++++++++++- 7 files changed, 29 insertions(+), 13 deletions(-) diff --git a/src/modules/convert/api.ts b/src/modules/convert/api.ts index 3fcfb5c..c9f2c79 100644 --- a/src/modules/convert/api.ts +++ b/src/modules/convert/api.ts @@ -21,7 +21,12 @@ import YAML = require("yamljs"); import { Root as HRoot, RootContent } from "hast"; import { Root as MRoot } from "mdast"; import { Nodes } from "hast-util-to-text/lib"; -import { fileExists, formatPath, randomString } from "../../utils/str"; +import { + fileExists, + formatPath, + jointPath, + randomString, +} from "../../utils/str"; import { parseCitationHTML } from "../../utils/citation"; import { copyEmbeddedImagesInHTML, @@ -75,7 +80,7 @@ async function note2md( await processN2MRehypeImageNodes( getN2MRehypeImageNodes(rehype), noteItem.libraryID, - formatPath(PathUtils.join(dir, "attachments")), + jointPath(dir, "attachments"), options.skipSavingImages, false, NodeMode.direct, diff --git a/src/modules/export/markdown.ts b/src/modules/export/markdown.ts index e690b53..bfd4aac 100644 --- a/src/modules/export/markdown.ts +++ b/src/modules/export/markdown.ts @@ -1,5 +1,5 @@ import { showHintWithLink } from "../../utils/hint"; -import { formatPath } from "../../utils/str"; +import { formatPath, jointPath } from "../../utils/str"; export async function saveMD( filename: string, @@ -30,7 +30,7 @@ export async function saveMD( export async function syncMDBatch(saveDir: string, noteIds: number[]) { const noteItems = Zotero.Items.get(noteIds); await Zotero.File.createDirectoryIfMissingAsync(saveDir); - const attachmentsDir = formatPath(PathUtils.join(saveDir, "attachments")); + const attachmentsDir = jointPath(saveDir, "attachments"); const hasImage = noteItems.some((noteItem) => noteItem.getNote().includes(" parseInt(id)); + const keys = rawKeys.split(",").map((id) => String(id)); setPref("syncNoteIds", JSON.stringify(keys)); } addon.data.sync.data = new ztoolkit.LargePref( @@ -30,6 +30,9 @@ function initSyncList() { `${config.prefsPrefix}.syncDetail-`, "parser", ); + // Due to the bug in v1.1.4-22, the sync data may be corrupted + const keys = addon.data.sync.data?.getKeys().map((key) => String(key)); + setPref("syncNoteIds", JSON.stringify(keys)); } function getSyncNoteIds(): number[] { diff --git a/src/modules/sync/hooks.ts b/src/modules/sync/hooks.ts index 88bf7b2..2648727 100644 --- a/src/modules/sync/hooks.ts +++ b/src/modules/sync/hooks.ts @@ -184,7 +184,7 @@ async function callSyncing( progress: 100, }); } catch (e) { - ztoolkit.log(e); + ztoolkit.log("[BetterNotes Syncing Error]", e); showHint(`Sync Error: ${String(e)}`); } finally { progress?.startCloseTimer(5000); diff --git a/src/modules/sync/infoWindow.ts b/src/modules/sync/infoWindow.ts index 882defa..644832f 100644 --- a/src/modules/sync/infoWindow.ts +++ b/src/modules/sync/infoWindow.ts @@ -1,6 +1,6 @@ import { showHint } from "../../utils/hint"; import { getString } from "../../utils/locale"; -import { formatPath, slice } from "../../utils/str"; +import { formatPath, jointPath, slice } from "../../utils/str"; export async function showSyncInfo(noteId: number) { const status = addon.api.sync.getSyncStatus(noteId); @@ -54,9 +54,7 @@ export async function showSyncInfo(noteId: number) { .addButton(getString("syncInfo.reveal"), "reveal", { noClose: true, callback: (ev) => { - Zotero.File.reveal( - formatPath(PathUtils.join(status.path, status.filename)), - ); + Zotero.File.reveal(jointPath(status.path, status.filename)); }, }) .addButton(getString("syncInfo.manager"), "manager", { diff --git a/src/modules/sync/managerWindow.ts b/src/modules/sync/managerWindow.ts index 62c1646..ecfd71d 100644 --- a/src/modules/sync/managerWindow.ts +++ b/src/modules/sync/managerWindow.ts @@ -1,6 +1,7 @@ import { config } from "../../../package.json"; import { getLinkedNotesRecursively, getNoteLink } from "../../utils/link"; import { getString } from "../../utils/locale"; +import { jointPath } from "../../utils/str"; import { isWindowAlive } from "../../utils/window"; export async function showSyncManager() { @@ -49,7 +50,7 @@ export async function showSyncManager() { ), showHeader: true, multiSelect: true, - staticColumns: true, + staticColumns: false, disableFontSizeScaling: true, }) .setProp("getRowCount", () => addon.data.sync.manager.data.length) @@ -128,7 +129,7 @@ function updateData() { noteId: noteId, noteName: Zotero.Items.get(noteId).getNoteTitle(), lastSync: new Date(syncStatus.lastsync).toLocaleString(), - filePath: PathUtils.join(syncStatus.path, syncStatus.filename), + filePath: jointPath(syncStatus.path, syncStatus.filename), }; }); } diff --git a/src/utils/str.ts b/src/utils/str.ts index 4da2cbf..caf0cf5 100644 --- a/src/utils/str.ts +++ b/src/utils/str.ts @@ -83,7 +83,16 @@ export async function fileExists(path: string): Promise { // IOUtils.exists() will throw error if path is not valid return await IOUtils.exists(formatPath(path)); } catch (e) { - ztoolkit.log(e); + ztoolkit.log("[fileExists]", e); return false; } } + +export function jointPath(...paths: string[]) { + try { + return formatPath(PathUtils.join(...paths)); + } catch (e) { + ztoolkit.log("[jointPath]", e); + return ""; + } +}