From 198991cc2d1873c3a4f8d873d66abaa4de16c9c6 Mon Sep 17 00:00:00 2001 From: olayway Date: Fri, 31 Mar 2023 21:12:01 +0200 Subject: [PATCH] dirty but works --- packages/markdowndb/src/lib/markdowndb.ts | 50 +++++++++++++++---- .../markdowndb/src/utils/extractWikiLinks.ts | 1 - 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/packages/markdowndb/src/lib/markdowndb.ts b/packages/markdowndb/src/lib/markdowndb.ts index a845b3d0c..d5fecc3c2 100644 --- a/packages/markdowndb/src/lib/markdowndb.ts +++ b/packages/markdowndb/src/lib/markdowndb.ts @@ -41,7 +41,8 @@ export const indexFolder = async ( const filesToInsert = []; const tagsToInsert = []; const fileTagsToInsert = []; - const linksToInsert = []; + + const extractedLinks = []; for (const pathToFile of pathsToFiles) { let file; @@ -80,7 +81,11 @@ export const indexFolder = async ( file.metadata = JSON.stringify(file.metadata); } - filesToInsert.push(file); + // TODO temp + const { links, ...rest } = file; + + extractedLinks.push(...links); + filesToInsert.push(rest); } } } @@ -89,6 +94,22 @@ export const indexFolder = async ( await db.batchInsert("tags", tagsToInsert); await db.batchInsert("file_tags", fileTagsToInsert); + const linksToInsert = []; + + for (const link of extractedLinks) { + const { to } = link; + const destPath = to.replace(/^\//, ""); + // find the file with the same url path + const destFile = await db("files").where({ _url_path: destPath }).first(); + + linksToInsert.push({ + ...link, + to: destFile?._id, + }); + } + + await db.batchInsert("links", linksToInsert); + db.destroy(); }; @@ -135,10 +156,10 @@ const createLinksTable = async (db: Knex) => { await db.schema.createTable("links", (table) => { table.string("_id").primary(); table.enum("type", ["normal", "embed"]).notNullable(); - table.string("from"); - table.string("to"); - // table.foreign("from").references("files._id").onDelete("CASCADE"); - // table.foreign("to").references("files._id").onDelete("CASCADE"); + table.string("from").notNullable(); + table.string("to").notNullable(); + table.foreign("from").references("files._id").onDelete("CASCADE"); + table.foreign("to").references("files._id").onDelete("CASCADE"); }); } }; @@ -180,6 +201,7 @@ const createDatabaseFile: (path: string, folderPath: string) => DatabaseFile = ( let metadata = null; let type = null; + let links = []; // If it's not a md/mdx file, _url_path is just the relative path const pathRelativeToFolder = path.slice(folderPath.length + 1); @@ -215,16 +237,25 @@ const createDatabaseFile: (path: string, folderPath: string) => DatabaseFile = ( .toLowerCase(); }; - const links = extractWikiLinks({ + links = extractWikiLinks({ source, // TODO pass slug instead of file path as hrefs/srcs are sluggified too // (where will we get it from?) filePath: tempSluggify(`/${pathRelativeToFolder}`), ...extractWikiLinksConfig, + }).map((link) => { + const encodedPath = Buffer.from(path, "utf-8").toString(); + const linkId = crypto + .createHash("sha1") + .update(encodedPath) + .digest("hex"); + return { + ...link, + from: id, + _id: linkId, + }; }); - console.log({ links }); - const segments = pathRelativeToFolder.split("/"); const filename = segments.at(-1).split(".")[0]; @@ -248,6 +279,7 @@ const createDatabaseFile: (path: string, folderPath: string) => DatabaseFile = ( _url_path, // Should exist only for MD/MDX files filetype, metadata, + links, type, }; }; diff --git a/packages/markdowndb/src/utils/extractWikiLinks.ts b/packages/markdowndb/src/utils/extractWikiLinks.ts index bf6011367..61dd44227 100644 --- a/packages/markdowndb/src/utils/extractWikiLinks.ts +++ b/packages/markdowndb/src/utils/extractWikiLinks.ts @@ -29,7 +29,6 @@ const resolveLink = (link: string, sourcePath?: string) => { } const dir = path.dirname(sourcePath); const resolved = path.resolve(dir, link); - console.log({ link, sourcePath, dir, resolved }); return resolved; };