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/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; 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/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", 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",