mirror of
https://github.com/vrtmrz/obsidian-livesync.git
synced 2026-10-24 10:03:20 +00:00
Preserve colon-containing Vault paths with Commonlib 0.1.28
This commit is contained in:
1 parent
2c2b9c90e4
commit
ae9145daa5
8 files changed
+169
-21
No files matched your search
@@ -0,0 +1,64 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
normalizePath: vi.fn((path: string) => `normalised(${path})`),
|
||||
path2idBase: vi.fn(async (path: string) => path),
|
||||
id2pathBase: vi.fn((path: string) => path),
|
||||
expandFilePathPrefix: vi.fn((path: string): [string, string] => {
|
||||
if (path.startsWith("i:")) return ["i:", path.substring(2)];
|
||||
return ["", path];
|
||||
}),
|
||||
}));
|
||||
|
||||
vi.mock("@/deps.ts", () => ({
|
||||
normalizePath: mocks.normalizePath,
|
||||
Platform: {},
|
||||
requestUrl: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("@vrtmrz/livesync-commonlib/compat/string_and_binary/path", () => ({
|
||||
path2id_base: mocks.path2idBase,
|
||||
id2path_base: mocks.id2pathBase,
|
||||
expandFilePathPrefix: mocks.expandFilePathPrefix,
|
||||
isValidFilenameInLinux: vi.fn(),
|
||||
isValidFilenameInDarwin: vi.fn(),
|
||||
isValidFilenameInWidows: vi.fn(),
|
||||
isValidFilenameInAndroid: vi.fn(),
|
||||
stripAllPrefixes: vi.fn(),
|
||||
}));
|
||||
|
||||
describe("path ID normalisation", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it.each([
|
||||
["Folder/Note.md", "", "Folder/Note.md"],
|
||||
["Folder/Poem: Example.md", "", "Folder/Poem: Example.md"],
|
||||
["Folder/Poem: Example: Final Draft.md", "", "Folder/Poem: Example: Final Draft.md"],
|
||||
["i:Folder/Poem: Example.md", "i:", "Folder/Poem: Example.md"],
|
||||
])("normalises the complete path body for %s", async (filename, prefix, body) => {
|
||||
const { path2id } = await import("./utils.ts");
|
||||
|
||||
const result = await path2id(filename as never, false, false);
|
||||
|
||||
expect(mocks.normalizePath).toHaveBeenCalledWith(body);
|
||||
expect(mocks.path2idBase).toHaveBeenCalledWith(`${prefix}normalised(${body})`, false, false);
|
||||
expect(result).toBe(`${prefix}normalised(${body})`);
|
||||
});
|
||||
|
||||
it.each([
|
||||
["Folder/Note.md", "", "Folder/Note.md"],
|
||||
["Folder/Poem: Example.md", "", "Folder/Poem: Example.md"],
|
||||
["Folder/Poem: Example: Final Draft.md", "", "Folder/Poem: Example: Final Draft.md"],
|
||||
["i:Folder/Poem: Example.md", "i:", "Folder/Poem: Example.md"],
|
||||
])("preserves the path namespace while normalising %s", async (filename, prefix, body) => {
|
||||
mocks.id2pathBase.mockReturnValue(filename);
|
||||
const { id2path } = await import("./utils.ts");
|
||||
|
||||
const result = id2path(filename as never);
|
||||
|
||||
expect(mocks.normalizePath).toHaveBeenCalledWith(body);
|
||||
expect(result).toBe(`${prefix}normalised(${body})`);
|
||||
});
|
||||
});
|
||||
+5
-8
@@ -7,6 +7,7 @@ import {
|
||||
isValidFilenameInWidows,
|
||||
isValidFilenameInAndroid,
|
||||
stripAllPrefixes,
|
||||
expandFilePathPrefix,
|
||||
} from "@vrtmrz/livesync-commonlib/compat/string_and_binary/path";
|
||||
|
||||
import { Logger } from "@vrtmrz/livesync-commonlib/compat/common/logger";
|
||||
@@ -41,22 +42,18 @@ export async function path2id(
|
||||
obfuscatePassphrase: string | false,
|
||||
caseInsensitive: boolean
|
||||
): Promise<DocumentID> {
|
||||
const temp = filename.split(":");
|
||||
const path = temp.pop();
|
||||
const [prefix, path] = expandFilePathPrefix(filename);
|
||||
const normalizedPath = normalizePath(path as FilePath);
|
||||
temp.push(normalizedPath);
|
||||
const fixedPath = temp.join(":") as FilePathWithPrefix;
|
||||
const fixedPath = `${prefix}${normalizedPath}` as FilePathWithPrefix;
|
||||
|
||||
const out = await path2id_base(fixedPath, obfuscatePassphrase, caseInsensitive);
|
||||
return out;
|
||||
}
|
||||
export function id2path(id: DocumentID, entry?: EntryHasPath): FilePathWithPrefix {
|
||||
const filename = id2path_base(id, entry);
|
||||
const temp = filename.split(":");
|
||||
const path = temp.pop();
|
||||
const [prefix, path] = expandFilePathPrefix(filename);
|
||||
const normalizedPath = normalizePath(path as FilePath);
|
||||
temp.push(normalizedPath);
|
||||
const fixedPath = temp.join(":") as FilePathWithPrefix;
|
||||
const fixedPath = `${prefix}${normalizedPath}` as FilePathWithPrefix;
|
||||
return fixedPath;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user