From d794f64d9b6c8b13aaa6c511fb2d0540db08380b Mon Sep 17 00:00:00 2001 From: Barrett LaFrance Date: Wed, 17 Jan 2024 16:34:37 -0600 Subject: [PATCH] feat: add link-rewrite support to build script for #_ prefixed, relative md file links --- build/index.mjs | 60 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 42 insertions(+), 18 deletions(-) diff --git a/build/index.mjs b/build/index.mjs index e9421a578..25a6e9f7a 100644 --- a/build/index.mjs +++ b/build/index.mjs @@ -58,6 +58,11 @@ function rewriteRemoteVideoLinks(content) { ) } +function isInt(str) { + return Number.isInteger(Number(str)) +} + + const TOTAL = 'Total build time' console.time(TOTAL) @@ -230,29 +235,36 @@ for (const version of RUN.versions) { }) await activity(`Gen weight and new file name`, async (log) => { - const dirname = path.dirname(RUN.srcmd.file) - const dnames = dirname.split("_") - const basename = path.basename(RUN.srcmd.file) - const fnames = basename.split("_") - - const hasWeight = (name) => Number.isInteger(Number(name)) - - const dWeight = hasWeight(dnames[0]) ? Number.parseInt(dnames[0]) : null - let newdir = hasWeight(dnames[0]) - ? dnames.slice(1).join('_').trim() - : dirname.trim() - - let weight = hasWeight(fnames[0]) ? Number.parseInt(fnames[0]) : null - let newfile = hasWeight(fnames[0]) - ? fnames.slice(1).join('_').trim() - : basename.trim() + const filename = path.basename(RUN.srcmd.file) + let ancestors = path.dirname(RUN.srcmd.file).split("/") + let parent = ancestors.pop() + + ancestors = ancestors.map(a => { + const [prefix, ...rest] = a.split("_") + return isInt(prefix) + ? rest.join("_") + : [prefix, ...rest].join("_") + }) + ancestors = ancestors.join("/") + + const pParts = parent.split("_") + const pWeight = isInt(pParts[0]) ? Number.parseInt(pParts[0]) : null + let newdir = pWeight !== null + ? [ancestors, pParts.slice(1).join('_').trim()].filter(f => f).join("/") + : [ancestors, pParts.join('_').trim()].filter(f => f).join("/") + + const fParts = filename.split("_") + let weight = isInt(fParts[0]) ? Number.parseInt(fParts[0]) : null + let newfile = weight !== null + ? fParts.slice(1).join('_').trim() + : filename.trim() if (newfile === "README.md") { newfile = newfile.replace("README.md", "_index.md") - weight = dWeight + weight = pWeight } - newfile = newdir === '.' ? newfile : `${newdir}/${newfile}` + newfile = newdir === '.' ? newfile : [newdir, newfile].join("/") RUN.srcmd.weight = weight RUN.srcmd.newfile = newfile @@ -284,6 +296,18 @@ for (const version of RUN.versions) { // rewrite relative .md link paths to compensate Hugo-gen'd pretty path RUN.srcmd.content = RUN.srcmd.content.replaceAll('](./', '](../') + // rewrite .md link paths to strip filename number prefixes + Array.from(RUN.srcmd.content.matchAll(/\]\(\.\.\/[^)]*\)/g), m => m[0]).forEach(mdLink => { + let parts = mdLink.replace("](", "").replace(")", "").split("/") + parts = parts.map(part => { + const [prefix, ...rest] = part.split("_") + return isInt(prefix) ? rest.join("_") : part + }) + let newLink = `](${parts.join("/")})` + RUN.srcmd.content = RUN.srcmd.content.replaceAll(mdLink, newLink) + }) + + // rewrite .md link paths to match Hugo's pretty link format RUN.srcmd.content = RUN.srcmd.content.replaceAll('.md)', '/)')