Skip to content

Commit

Permalink
feat: add link-rewrite support to build script for #_ prefixed, relat…
Browse files Browse the repository at this point in the history
…ive md file links
  • Loading branch information
btlghrants committed Jan 17, 2024
1 parent ce3eceb commit d794f64
Showing 1 changed file with 42 additions and 18 deletions.
60 changes: 42 additions & 18 deletions build/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ function rewriteRemoteVideoLinks(content) {
)
}

function isInt(str) {
return Number.isInteger(Number(str))
}


const TOTAL = 'Total build time'
console.time(TOTAL)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)', '/)')

Expand Down

0 comments on commit d794f64

Please sign in to comment.