diff --git a/src/modules/convert/api.ts b/src/modules/convert/api.ts index 698f200..dc698ab 100644 --- a/src/modules/convert/api.ts +++ b/src/modules/convert/api.ts @@ -907,13 +907,14 @@ async function processN2MRehypeImageNodes( try { // Don't overwrite if (skipSavingImages || (await fileExists(newAbsPath))) { - newFile = newAbsPath.replace(/\\/g, "/"); + newFile = newAbsPath; } else { newFile = (await Zotero.File.copyToUnique(oldFile, newAbsPath)).path; - newFile = newFile.replace(/\\/g, "/"); } - newFile = Zotero.File.normalizeToUnix( - absolutePath ? newFile : `attachments/${newFile.split(/\//).pop()}`, + newFile = formatPath( + absolutePath + ? newFile + : `attachments/${PathUtils.split(newFile).pop()}`, ); } catch (e) { ztoolkit.log(e); @@ -1093,9 +1094,7 @@ async function processM2NRehypeImageNodes( for (const node of nodes) { if (isImport) { // We encode the src in md2remark and decode it here. - let src = Zotero.File.normalizeToUnix( - decodeURIComponent(node.properties.src), - ); + let src = formatPath(decodeURIComponent(node.properties.src)); const srcType = (src as string).startsWith("data:") ? "b64" : (src as string).startsWith("http") diff --git a/src/modules/createNote.ts b/src/modules/createNote.ts index c2cb73a..7f02dba 100644 --- a/src/modules/createNote.ts +++ b/src/modules/createNote.ts @@ -1,5 +1,6 @@ import { getString } from "../utils/locale"; import { config } from "../../package.json"; +import { formatPath } from "../utils/str"; export { createWorkspaceNote, createNoteFromTemplate, createNoteFromMD }; @@ -104,10 +105,10 @@ async function createNoteFromMD() { ignoreVersion: true, }); if (noteItem && syncNotes) { - const pathSplit = Zotero.File.normalizeToUnix(filepath).split("/"); + const pathSplit = PathUtils.split(formatPath(filepath)); addon.api.sync.updateSyncStatus(noteItem.id, { itemID: noteItem.id, - path: Zotero.File.normalizeToUnix(pathSplit.slice(0, -1).join("/")), + path: formatPath(pathSplit.slice(0, -1).join("/")), filename: pathSplit.pop() || "", lastsync: new Date().getTime(), md5: "", diff --git a/src/modules/sync/api.ts b/src/modules/sync/api.ts index d0055b8..4292910 100644 --- a/src/modules/sync/api.ts +++ b/src/modules/sync/api.ts @@ -2,7 +2,7 @@ import YAML = require("yamljs"); import { clearPref, getPref, setPref } from "../../utils/prefs"; import { getNoteLinkParams } from "../../utils/link"; import { config } from "../../../package.json"; -import { fileExists } from "../../utils/str"; +import { fileExists, formatPath } from "../../utils/str"; export { getRelatedNoteIds, @@ -117,9 +117,11 @@ function getSyncStatus(noteId?: number): SyncStatus { lastsync: new Date().getTime(), itemID: -1, }); - return JSON.parse( + const status = JSON.parse( (getPref(`syncDetail-${noteId}`) as string) || defaultStatus, ); + status.path = formatPath(status.path); + return status; } function getMDStatusFromContent(contentRaw: string): MDStatus { @@ -164,18 +166,16 @@ async function getMDStatus( const syncStatus = getSyncStatus(source.id); filepath = PathUtils.join(syncStatus.path, syncStatus.filename); } - filepath = Zotero.File.normalizeToUnix(filepath); + filepath = formatPath(filepath); if (await fileExists(filepath)) { const contentRaw = (await Zotero.File.getContentsAsync( filepath, "utf-8", )) as string; ret = getMDStatusFromContent(contentRaw); - const pathSplit = filepath.split("/"); - ret.filedir = Zotero.File.normalizeToUnix( - pathSplit.slice(0, -1).join("/"), - ); - ret.filename = filepath.split("/").pop() || ""; + const pathSplit = PathUtils.split(filepath); + ret.filedir = formatPath(pathSplit.slice(0, -1).join("/")); + ret.filename = pathSplit.pop() || ""; const stat = await IOUtils.stat(filepath); ret.lastmodify = new Date(stat.lastModified); } diff --git a/src/modules/sync/diffWindow.ts b/src/modules/sync/diffWindow.ts index 840afb4..8940490 100644 --- a/src/modules/sync/diffWindow.ts +++ b/src/modules/sync/diffWindow.ts @@ -1,6 +1,6 @@ import { diffChars } from "diff"; import { config } from "../../../package.json"; -import { fileExists, getItemDataURL } from "../../utils/str"; +import { fileExists, formatPath, getItemDataURL } from "../../utils/str"; import { isWindowAlive } from "../../utils/window"; import { waitUtilAsync } from "../../utils/wait"; @@ -8,7 +8,7 @@ export async function showSyncDiff(noteId: number, mdPath: string) { const noteItem = Zotero.Items.get(noteId); const syncStatus = addon.api.sync.getSyncStatus(noteId); const noteStatus = addon.api.sync.getNoteStatus(noteId)!; - mdPath = Zotero.File.normalizeToUnix(mdPath); + mdPath = formatPath(mdPath); if (!noteItem || !noteItem.isNote() || !(await fileExists(mdPath))) { return; } diff --git a/src/utils/note.ts b/src/utils/note.ts index 87fb525..002df40 100644 --- a/src/utils/note.ts +++ b/src/utils/note.ts @@ -1,7 +1,7 @@ import TreeModel = require("tree-model"); -import katex = require("katex"); +import katex from "katex"; import { getEditorInstance, getPositionAtLine, insert } from "./editor"; -import { getItemDataURL } from "./str"; +import { formatPath, getItemDataURL } from "./str"; import { showHint } from "./hint"; import { config } from "../../package.json"; @@ -474,7 +474,7 @@ async function importImageToNote( } blob = res.response; } else if (type === "file") { - src = Zotero.File.normalizeToUnix(src); + src = formatPath(src); const noteAttachmentKeys = Zotero.Items.get(note.getAttachments()).map( (_i) => _i.key, ); diff --git a/src/utils/str.ts b/src/utils/str.ts index 9612b32..ce60278 100644 --- a/src/utils/str.ts +++ b/src/utils/str.ts @@ -24,8 +24,7 @@ export function fill( export function formatPath(path: string, suffix: string = "") { path = Zotero.File.normalizeToUnix(path); if (Zotero.isWin) { - path = path.replace(/\\/g, "/"); - path = PathUtils.join(...path.split(/\//)); + path = path.replace(/\//g, "\\"); } if (Zotero.isMac && path.charAt(0) !== "/") { path = "/" + path;