From a8a9ec7c2fb45bc0ab31c104c010d22b39e142cb Mon Sep 17 00:00:00 2001 From: mohamed yahia Date: Tue, 21 Nov 2023 23:33:24 +0200 Subject: [PATCH 1/5] Implement obsidian links --- src/utils/extractWikiLinks.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/utils/extractWikiLinks.ts b/src/utils/extractWikiLinks.ts index 9b5b49a..710d6f0 100644 --- a/src/utils/extractWikiLinks.ts +++ b/src/utils/extractWikiLinks.ts @@ -71,10 +71,13 @@ const extractWikiLinks = ( linkSrc = node.data.permalink; text = node.children?.[0]?.value || ""; } + const to = !linkSrc.startsWith("http") + ? path.posix.join(directory, linkSrc) + : linkSrc; return { from: from, - to: linkSrc, + to: to, toRaw: linkSrc, text, embed: linkType === "embed", From 223a566ecdb551021488db1c6f098843ba981454 Mon Sep 17 00:00:00 2001 From: mohamed yahia Date: Tue, 21 Nov 2023 23:33:44 +0200 Subject: [PATCH 2/5] No need for this --- src/lib/process.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/process.ts b/src/lib/process.ts index 5861b2e..afa5798 100644 --- a/src/lib/process.ts +++ b/src/lib/process.ts @@ -59,7 +59,7 @@ export function processFile( fileInfo.url_path = pathToUrlResolver(relativePath); fileInfo.metadata = metadata; - fileInfo.links = links.map((link) => ({ ...link, from: relativePath })); + fileInfo.links = links; const filetype = metadata?.type || null; fileInfo.filetype = filetype; From 98a4b049aec0e035b49ac2798350040cab3c9c78 Mon Sep 17 00:00:00 2001 From: mohamed yahia Date: Tue, 21 Nov 2023 23:34:28 +0200 Subject: [PATCH 3/5] Update tests for links --- __mocks__/content/blog/blog1.mdx | 2 +- __mocks__/content/blog/blog2.mdx | 2 +- __mocks__/content/blog/blog3.mdx | 2 +- src/lib/process.spec.ts | 9 +++- src/utils/databaseUtils.ts | 11 +++-- src/utils/parseFile.spec.ts | 72 ++++++++++++++++++++++++++++---- 6 files changed, 82 insertions(+), 16 deletions(-) diff --git a/__mocks__/content/blog/blog1.mdx b/__mocks__/content/blog/blog1.mdx index fa21d3f..b5e71a2 100644 --- a/__mocks__/content/blog/blog1.mdx +++ b/__mocks__/content/blog/blog1.mdx @@ -6,4 +6,4 @@ draft: true # My Test Mdx Blog 1 -[[blog2]] +[[blog2.md]] diff --git a/__mocks__/content/blog/blog2.mdx b/__mocks__/content/blog/blog2.mdx index e8b5604..4aff4e4 100644 --- a/__mocks__/content/blog/blog2.mdx +++ b/__mocks__/content/blog/blog2.mdx @@ -8,4 +8,4 @@ tags: # My Test Mdx Blog 2 -[[../blog0]] +[[../blog0.md]] diff --git a/__mocks__/content/blog/blog3.mdx b/__mocks__/content/blog/blog3.mdx index ed61e17..eb98cbd 100644 --- a/__mocks__/content/blog/blog3.mdx +++ b/__mocks__/content/blog/blog3.mdx @@ -10,4 +10,4 @@ tags: # My Test Mdx Blog 2 -[[/blog/blog1]] +[[/blog/blog1.md]] diff --git a/src/lib/process.spec.ts b/src/lib/process.spec.ts index aabf98c..e2b47e9 100644 --- a/src/lib/process.spec.ts +++ b/src/lib/process.spec.ts @@ -22,7 +22,14 @@ describe("Can parse a file and get file info", () => { tags: ["tag1", "tag2", "tag3"], }); expect(fileInfo.links).toEqual([ - { linkSrc: "blog0.mdx", linkType: "normal" }, + { + embed: true, + from: "index.mdx", + internal: true, + text: "link", + to: "blog0.mdx", + toRaw: "blog0.mdx", + }, ]); }); }); diff --git a/src/utils/databaseUtils.ts b/src/utils/databaseUtils.ts index 45ab06a..729308d 100644 --- a/src/utils/databaseUtils.ts +++ b/src/utils/databaseUtils.ts @@ -40,11 +40,14 @@ export function mapLinksToInsert(filesToInsert: File[], file: any) { } function findFileToInsert(filesToInsert: File[], filePath: string) { - const normalizedFilePath = path.normalize(filePath); + const filePathWithoutExt = path.join( + path.dirname(filePath), + path.basename(filePath, path.extname(filePath)) + ); - return filesToInsert.find(({ file_path }) => { - const normalizedFile = path.normalize(file_path); - return normalizedFile === normalizedFilePath; + return filesToInsert.find(({ url_path }) => { + const normalizedFile = path.normalize(url_path || ""); + return normalizedFile === filePathWithoutExt; }); } diff --git a/src/utils/parseFile.spec.ts b/src/utils/parseFile.spec.ts index 28de0e3..041900e 100644 --- a/src/utils/parseFile.spec.ts +++ b/src/utils/parseFile.spec.ts @@ -20,10 +20,38 @@ describe("parseFile", () => { tags: ["a", "b", "c"], }; const expectedLinks = [ - { linkType: "normal", linkSrc: "Some Link" }, - { linkType: "normal", linkSrc: "blog/Some Other Link" }, - { linkType: "normal", linkSrc: "blog/Some Other Link" }, - { linkType: "embed", linkSrc: "Some Image.png" }, + { + embed: false, + from: "", + internal: true, + text: "", + to: "Some Link", + toRaw: "Some Link", + }, + { + embed: false, + from: "", + internal: true, + text: "", + to: "blog/Some Other Link", + toRaw: "blog/Some Other Link", + }, + { + embed: false, + from: "", + internal: true, + text: "", + to: "blog/Some Other Link", + toRaw: "blog/Some Other Link", + }, + { + embed: true, + from: "", + internal: true, + text: "", + to: "Some Image.png", + toRaw: "Some Image.png", + }, ]; const { metadata, links } = parseFile(source); expect(metadata).toEqual(expectedMetadata); @@ -37,10 +65,38 @@ describe("parseFile", () => { tags: ["a", "b", "c"], }; const expectedLinks = [ - { linkType: "normal", linkSrc: "/some/folder/Some Link" }, - { linkType: "normal", linkSrc: "/some/folder/blog/Some Other Link" }, - { linkType: "normal", linkSrc: "/some/folder/blog/Some Other Link" }, - { linkType: "embed", linkSrc: "/some/folder/Some Image.png" }, + { + embed: false, + from: "", + internal: true, + text: "", + to: "some/folder/Some Link", + toRaw: "/some/folder/Some Link", + }, + { + embed: false, + from: "", + internal: true, + text: "", + to: "some/folder/blog/Some Other Link", + toRaw: "/some/folder/blog/Some Other Link", + }, + { + embed: false, + from: "", + internal: true, + text: "", + to: "some/folder/blog/Some Other Link", + toRaw: "/some/folder/blog/Some Other Link", + }, + { + embed: true, + from: "", + internal: true, + text: "", + to: "some/folder/Some Image.png", + toRaw: "/some/folder/Some Image.png", + }, ]; const permalinks = [ "/some/folder/Some Link", From b8bc45a7f2b022ed82042642e510c11dbec1f6c9 Mon Sep 17 00:00:00 2001 From: mohamed yahia Date: Wed, 22 Nov 2023 17:32:41 +0200 Subject: [PATCH 4/5] Update tests for links --- src/lib/process.spec.ts | 9 ++++- src/utils/databaseUtils.ts | 11 +++--- src/utils/parseFile.spec.ts | 72 ++++++++++++++++++++++++++++++++----- 3 files changed, 79 insertions(+), 13 deletions(-) diff --git a/src/lib/process.spec.ts b/src/lib/process.spec.ts index aabf98c..e2b47e9 100644 --- a/src/lib/process.spec.ts +++ b/src/lib/process.spec.ts @@ -22,7 +22,14 @@ describe("Can parse a file and get file info", () => { tags: ["tag1", "tag2", "tag3"], }); expect(fileInfo.links).toEqual([ - { linkSrc: "blog0.mdx", linkType: "normal" }, + { + embed: true, + from: "index.mdx", + internal: true, + text: "link", + to: "blog0.mdx", + toRaw: "blog0.mdx", + }, ]); }); }); diff --git a/src/utils/databaseUtils.ts b/src/utils/databaseUtils.ts index 45ab06a..729308d 100644 --- a/src/utils/databaseUtils.ts +++ b/src/utils/databaseUtils.ts @@ -40,11 +40,14 @@ export function mapLinksToInsert(filesToInsert: File[], file: any) { } function findFileToInsert(filesToInsert: File[], filePath: string) { - const normalizedFilePath = path.normalize(filePath); + const filePathWithoutExt = path.join( + path.dirname(filePath), + path.basename(filePath, path.extname(filePath)) + ); - return filesToInsert.find(({ file_path }) => { - const normalizedFile = path.normalize(file_path); - return normalizedFile === normalizedFilePath; + return filesToInsert.find(({ url_path }) => { + const normalizedFile = path.normalize(url_path || ""); + return normalizedFile === filePathWithoutExt; }); } diff --git a/src/utils/parseFile.spec.ts b/src/utils/parseFile.spec.ts index 28de0e3..041900e 100644 --- a/src/utils/parseFile.spec.ts +++ b/src/utils/parseFile.spec.ts @@ -20,10 +20,38 @@ describe("parseFile", () => { tags: ["a", "b", "c"], }; const expectedLinks = [ - { linkType: "normal", linkSrc: "Some Link" }, - { linkType: "normal", linkSrc: "blog/Some Other Link" }, - { linkType: "normal", linkSrc: "blog/Some Other Link" }, - { linkType: "embed", linkSrc: "Some Image.png" }, + { + embed: false, + from: "", + internal: true, + text: "", + to: "Some Link", + toRaw: "Some Link", + }, + { + embed: false, + from: "", + internal: true, + text: "", + to: "blog/Some Other Link", + toRaw: "blog/Some Other Link", + }, + { + embed: false, + from: "", + internal: true, + text: "", + to: "blog/Some Other Link", + toRaw: "blog/Some Other Link", + }, + { + embed: true, + from: "", + internal: true, + text: "", + to: "Some Image.png", + toRaw: "Some Image.png", + }, ]; const { metadata, links } = parseFile(source); expect(metadata).toEqual(expectedMetadata); @@ -37,10 +65,38 @@ describe("parseFile", () => { tags: ["a", "b", "c"], }; const expectedLinks = [ - { linkType: "normal", linkSrc: "/some/folder/Some Link" }, - { linkType: "normal", linkSrc: "/some/folder/blog/Some Other Link" }, - { linkType: "normal", linkSrc: "/some/folder/blog/Some Other Link" }, - { linkType: "embed", linkSrc: "/some/folder/Some Image.png" }, + { + embed: false, + from: "", + internal: true, + text: "", + to: "some/folder/Some Link", + toRaw: "/some/folder/Some Link", + }, + { + embed: false, + from: "", + internal: true, + text: "", + to: "some/folder/blog/Some Other Link", + toRaw: "/some/folder/blog/Some Other Link", + }, + { + embed: false, + from: "", + internal: true, + text: "", + to: "some/folder/blog/Some Other Link", + toRaw: "/some/folder/blog/Some Other Link", + }, + { + embed: true, + from: "", + internal: true, + text: "", + to: "some/folder/Some Image.png", + toRaw: "/some/folder/Some Image.png", + }, ]; const permalinks = [ "/some/folder/Some Link", From c313c36a762e25be89c5246e21abbddfa527b15f Mon Sep 17 00:00:00 2001 From: mohamed yahia Date: Wed, 22 Nov 2023 17:36:01 +0200 Subject: [PATCH 5/5] undo changing tests --- __mocks__/content/blog/blog1.mdx | 2 +- __mocks__/content/blog/blog2.mdx | 2 +- __mocks__/content/blog/blog3.mdx | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/__mocks__/content/blog/blog1.mdx b/__mocks__/content/blog/blog1.mdx index b5e71a2..fa21d3f 100644 --- a/__mocks__/content/blog/blog1.mdx +++ b/__mocks__/content/blog/blog1.mdx @@ -6,4 +6,4 @@ draft: true # My Test Mdx Blog 1 -[[blog2.md]] +[[blog2]] diff --git a/__mocks__/content/blog/blog2.mdx b/__mocks__/content/blog/blog2.mdx index 4aff4e4..e8b5604 100644 --- a/__mocks__/content/blog/blog2.mdx +++ b/__mocks__/content/blog/blog2.mdx @@ -8,4 +8,4 @@ tags: # My Test Mdx Blog 2 -[[../blog0.md]] +[[../blog0]] diff --git a/__mocks__/content/blog/blog3.mdx b/__mocks__/content/blog/blog3.mdx index eb98cbd..ed61e17 100644 --- a/__mocks__/content/blog/blog3.mdx +++ b/__mocks__/content/blog/blog3.mdx @@ -10,4 +10,4 @@ tags: # My Test Mdx Blog 2 -[[/blog/blog1.md]] +[[/blog/blog1]]