fix: path errors on windows

This commit is contained in:
windingwind 2023-08-14 16:24:29 +08:00
parent f40134cdb3
commit 1deb2c178b
6 changed files with 23 additions and 24 deletions

View File

@ -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")

View File

@ -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: "",

View File

@ -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);
}

View File

@ -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;
}

View File

@ -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,
);

View File

@ -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;