From d04f22514ebe9b6cc3574837aff48ae2f869a6d2 Mon Sep 17 00:00:00 2001 From: mohamed yahia Date: Wed, 22 Nov 2023 19:20:02 +0200 Subject: [PATCH] Update obsidian links test --- src/lib/process.spec.ts | 3 +- src/utils/extractWikiLinks.spec.ts | 190 ++++++++++++++--------------- src/utils/extractWikiLinks.ts | 2 +- 3 files changed, 98 insertions(+), 97 deletions(-) diff --git a/src/lib/process.spec.ts b/src/lib/process.spec.ts index e2b47e9..c59d8d6 100644 --- a/src/lib/process.spec.ts +++ b/src/lib/process.spec.ts @@ -15,6 +15,7 @@ describe("Can parse a file and get file info", () => { ); expect(fileInfo.file_path).toBe(fullPath); + expect(fileInfo.url_path).toBe("index.mdx"); expect(fileInfo.extension).toBe("mdx"); expect(fileInfo.tags).toEqual(["tag1", "tag2", "tag3"]); expect(fileInfo.metadata).toEqual({ @@ -23,7 +24,7 @@ describe("Can parse a file and get file info", () => { }); expect(fileInfo.links).toEqual([ { - embed: true, + embed: false, from: "index.mdx", internal: true, text: "link", diff --git a/src/utils/extractWikiLinks.spec.ts b/src/utils/extractWikiLinks.spec.ts index 8cffd21..671eade 100644 --- a/src/utils/extractWikiLinks.spec.ts +++ b/src/utils/extractWikiLinks.spec.ts @@ -20,7 +20,7 @@ describe("extractWikiLinks", () => { to: "abc/page-1", toRaw: "page-1", text: "Page 1", - embed: true, + embed: false, internal: true, }, ]; @@ -36,7 +36,7 @@ describe("extractWikiLinks", () => { to: "abc/world.png", toRaw: "world.png", text: "hello", - embed: true, + embed: false, internal: true, }, ]; @@ -52,7 +52,7 @@ describe("extractWikiLinks", () => { to: "abc/world.mdx", toRaw: "world.mdx", text: "hello", - embed: true, + embed: false, internal: true, }, ]; @@ -68,7 +68,7 @@ describe("extractWikiLinks", () => { to: "abc/world", toRaw: "/world", text: "hello", - embed: true, + embed: false, internal: true, }, ]; @@ -109,98 +109,98 @@ describe("extractWikiLinks", () => { }); // TODO Obsidian wiki links - // describe("Obsidian wiki links", () => { - // test("should extract wiki links", () => { - // const source = "[[Page 1]] [[Page 2]] [[Page 3]]"; - // const expectedLinks = [ - // { - // from: "abc/foobar.md", - // to: "abc/Page 1.md", - // toRaw: "Page 1", - // text: "Page 1", - // embed: false, - // internal: true, - // }, - // { - // from: "abc/foobar.md", - // to: "abc/Page 2.md", - // toRaw: "Page 2", - // text: "Page 2", - // embed: false, - // internal: true, - // }, - // { - // from: "abc/foobar.md", - // to: "abc/Page 3.md", - // toRaw: "Page 3", - // text: "Page 3", - // embed: false, - // internal: true, - // }, - // ]; - // const links = extractWikiLinks("abc/foobar.md", source); - // expect(links).toHaveLength(expectedLinks.length); - // links.forEach((link) => { - // expect(expectedLinks).toContainEqual(link); - // }); - // }); + describe("Obsidian wiki links", () => { + test("should extract wiki links", () => { + const source = "[[Page 1]] [[Page 2]] [[Page 3]]"; + const expectedLinks = [ + { + embed: false, + from: "abc/foobar.md", + internal: true, + text: "", + to: "abc/Page 1", + toRaw: "Page 1", + }, + { + embed: false, + from: "abc/foobar.md", + internal: true, + text: "", + to: "abc/Page 2", + toRaw: "Page 2", + }, + { + embed: false, + from: "abc/foobar.md", + internal: true, + text: "", + to: "abc/Page 3", + toRaw: "Page 3", + }, + ]; + const links = extractWikiLinks("abc/foobar.md", source); + expect(links).toHaveLength(expectedLinks.length); + links.forEach((link) => { + expect(expectedLinks).toContainEqual(link); + }); + }); - // test("should extract wiki links with Obsidian-style shortest path", () => { - // const source = "[[Page 1]] [[Page 2]] [[Page 3]]"; - // const expectedLinks = [ - // { - // from: "abc/foobar.md", - // to: "abc/some/folder/Page 1.md", - // toRaw: "/some/folder/Page 1", - // text: "Page 1", - // embed: false, - // internal: true, - // }, - // { - // from: "abc/foobar.md", - // to: "abc/some/folder/Page 2.md", - // toRaw: "/some/folder/Page 2", - // text: "Page 2", - // embed: false, - // internal: true, - // }, - // { - // from: "abc/foobar.md", - // to: "abc/some/folder/Page 3.md", - // toRaw: "/some/folder/Page 3", - // text: "Page 3", - // embed: false, - // internal: true, - // }, - // ]; - // const permalinks = [ - // "/some/folder/Page 1", - // "/some/folder/Page 2", - // "/some/folder/Page 3", - // ]; - // const links = extractWikiLinks("abc/foobar.md", source, { permalinks }); - // expect(links).toHaveLength(expectedLinks.length); - // links.forEach((link) => { - // expect(expectedLinks).toContainEqual(link); - // }); - // }); + test("should extract wiki links with Obsidian-style shortest path", () => { + const source = "[[Page 1]] [[Page 2]] [[Page 3]]"; + const expectedLinks = [ + { + embed: false, + from: "abc/foobar.md", + internal: true, + text: "", + to: "abc/some/folder/Page 1", + toRaw: "/some/folder/Page 1", + }, + { + embed: false, + from: "abc/foobar.md", + internal: true, + text: "", + to: "abc/some/folder/Page 2", + toRaw: "/some/folder/Page 2", + }, + { + embed: false, + from: "abc/foobar.md", + internal: true, + text: "", + to: "abc/some/folder/Page 3", + toRaw: "/some/folder/Page 3", + }, + ]; + const permalinks = [ + "/some/folder/Page 1", + "/some/folder/Page 2", + "/some/folder/Page 3", + ]; + const links = extractWikiLinks("abc/foobar.md", source, { permalinks }); + expect(links).toHaveLength(expectedLinks.length); + links.forEach((link) => { + expect(expectedLinks).toContainEqual(link); + }); + }); - // test("should extract embedded wiki links", () => { - // const source = "![[My File.png]]]]"; - // const expectedLinks = [ - // { - // from: "abc/foobar.md", - // to: "abc/My File.png", - // toRaw: "My File.png", - // text: "My File.png", - // embed: true, - // internal: true, - // }, - // ]; - // const links = extractWikiLinks("abc/foobar.md", source); - // expect(links).toEqual(expectedLinks); - // }); - // }); + test("should extract embedded wiki links", () => { + const source = "![[My File.png]]]]"; + const expectedLinks = [ + { + from: "abc/foobar.md", + to: "abc/My File.png", + toRaw: "My File.png", + text: "", + embed: true, + internal: true, + }, + ]; + const links = extractWikiLinks("abc/foobar.md", source); + expect(links).toEqual(expectedLinks); + }); + }); // TODO fix this test // test("should return unique links", () => { @@ -226,7 +226,7 @@ describe("extractWikiLinks", () => { to: "https://example.com", toRaw: "https://example.com", text: "External Link", - embed: true, // Adjust this based on your requirements + embed: false, internal: false, }, ]; diff --git a/src/utils/extractWikiLinks.ts b/src/utils/extractWikiLinks.ts index 710d6f0..951d89d 100644 --- a/src/utils/extractWikiLinks.ts +++ b/src/utils/extractWikiLinks.ts @@ -44,7 +44,7 @@ const extractWikiLinks = ( to: to, toRaw: node.url, text: node.children?.[0]?.value || "", - embed: true, + embed: false, internal: !node.url.startsWith("http"), }; },