feat: support Obsidian wikilink images

Support Obsidian image wikilinks alongside standard markdown images, preserve mixed image order, and resolve Obsidian default Attachments/ paths.

Co-authored-by: Chao Zheng <10296164+zcqqq@users.noreply.github.com>
This commit is contained in:
Jim Liu 宝玉
2026-05-27 23:06:04 -05:00
parent 84cefc2784
commit 639e0b4193
9 changed files with 514 additions and 184 deletions
+58
View File
@@ -28,6 +28,32 @@ test("replaceMarkdownImagesWithPlaceholders rewrites markdown and tracks image m
]);
});
test("replaceMarkdownImagesWithPlaceholders supports Obsidian image wikilinks in document order", () => {
const result = replaceMarkdownImagesWithPlaceholders(
`Intro\n\n![[a.png]]\n\n![B](b.jpg)\n\n![[c.webp|C alt]]\n\n![[note]]`,
"IMG_",
);
assert.equal(result.markdown, `Intro\n\nIMG_1\n\nIMG_2\n\nIMG_3\n\n![[note]]`);
assert.deepEqual(result.images, [
{ alt: "", originalPath: "a.png", placeholder: "IMG_1" },
{ alt: "B", originalPath: "b.jpg", placeholder: "IMG_2" },
{ alt: "C alt", originalPath: "c.webp", placeholder: "IMG_3" },
]);
});
test("replaceMarkdownImagesWithPlaceholders supports Obsidian image wikilinks with paths", () => {
const result = replaceMarkdownImagesWithPlaceholders(
`![[Attachments/screenshot.png]]`,
"IMG_",
);
assert.equal(result.markdown, `IMG_1`);
assert.deepEqual(result.images, [
{ alt: "", originalPath: "Attachments/screenshot.png", placeholder: "IMG_1" },
]);
});
test("image extension and local fallback resolution handle common path variants", async (t) => {
assert.equal(getImageExtension("https://example.com/a.jpeg?x=1"), "jpeg");
assert.equal(getImageExtension("/tmp/figure"), "png");
@@ -45,6 +71,38 @@ test("image extension and local fallback resolution handle common path variants"
assert.equal(resolved, path.join(baseDir, "figure.webp"));
});
test("resolveImagePath falls back to Attachments subdirectory before extension variants", async (t) => {
const root = await makeTempDir("baoyu-md-attachments-");
t.after(() => fs.rm(root, { recursive: true, force: true }));
const baseDir = path.join(root, "article");
const tempDir = path.join(root, "tmp");
const attachmentsDir = path.join(baseDir, "Attachments");
await fs.mkdir(attachmentsDir, { recursive: true });
await fs.mkdir(tempDir, { recursive: true });
await fs.writeFile(path.join(baseDir, "figure.webp"), "webp");
await fs.writeFile(path.join(attachmentsDir, "figure.png"), "png");
const resolved = await resolveImagePath("figure.png", baseDir, tempDir, "test");
assert.equal(resolved, path.join(attachmentsDir, "figure.png"));
});
test("resolveImagePath prefers original path before Attachments fallback", async (t) => {
const root = await makeTempDir("baoyu-md-attachments-original-");
t.after(() => fs.rm(root, { recursive: true, force: true }));
const baseDir = path.join(root, "article");
const tempDir = path.join(root, "tmp");
const attachmentsDir = path.join(baseDir, "Attachments");
await fs.mkdir(attachmentsDir, { recursive: true });
await fs.mkdir(tempDir, { recursive: true });
await fs.writeFile(path.join(baseDir, "figure.png"), "png");
await fs.writeFile(path.join(attachmentsDir, "figure.png"), "attachment png");
const resolved = await resolveImagePath("figure.png", baseDir, tempDir, "test");
assert.equal(resolved, path.join(baseDir, "figure.png"));
});
test("resolveImagePath decodes URL-encoded filenames with spaces", async (t) => {
const root = await makeTempDir("baoyu-md-urlencoded-");
t.after(() => fs.rm(root, { recursive: true, force: true }));