Skip to content

Commit

Permalink
feat: add link rewrite to support numbered files in landing page READ…
Browse files Browse the repository at this point in the history
…ME.md
  • Loading branch information
btlghrants committed Jan 18, 2024
1 parent d794f64 commit f6bdb2f
Showing 1 changed file with 45 additions and 10 deletions.
55 changes: 45 additions & 10 deletions build/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,40 @@ function isInt(str) {
return Number.isInteger(Number(str))
}

function rewriteNumberedFileLinks(content) {
Array.from(content.matchAll(/\]\([^)]*\)/g), m => m[0]).forEach(mdLink => {
let parts = mdLink.replace("](", "").replace(")", "").split("/")
if (parts[0].startsWith("http")) { return }

parts = parts.map(part => {
const [prefix, ...rest] = part.split("_")
return isInt(prefix) ? rest.join("_") : part
})
let newLink = `](${parts.join("/")})`
content = content.replaceAll(mdLink, newLink)
})
return content
}

function rewriteReadmeFileLinks(content) {
Array.from(content.matchAll(/\]\([^)]*\)/g), m => m[0]).forEach(mdLink => {
let parts = mdLink.replace("](", "").replace(")", "").split("/")
if (parts.at(-1) === "README.md") { parts.pop() }

let newLink = `](${parts.join("/")})`
content = content.replaceAll(mdLink, newLink)
})
return content
}

function rewriteFileLinksAsLowerCase(content) {
Array.from(content.matchAll(/\]\([^)]*\)/g), m => m[0]).forEach(mdLink => {
let newLink = mdLink.toLowerCase()
content = content.replaceAll(mdLink, newLink)
})
return content
}


const TOTAL = 'Total build time'
console.time(TOTAL)
Expand Down Expand Up @@ -296,17 +330,11 @@ 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)
})
RUN.srcmd.content = rewriteNumberedFileLinks(RUN.srcmd.content)

RUN.srcmd.content = rewriteReadmeFileLinks(RUN.srcmd.content)

RUN.srcmd.content = rewriteFileLinksAsLowerCase(RUN.srcmd.content)

// rewrite .md link paths to match Hugo's pretty link format
RUN.srcmd.content = RUN.srcmd.content.replaceAll('.md)', '/)')
Expand Down Expand Up @@ -345,12 +373,19 @@ for (const version of RUN.versions) {
// trim 'docs' out of link paths
idxBody = idxBody.replaceAll('](./docs/', '](./')

idxBody = rewriteReadmeFileLinks(idxBody)

idxBody = rewriteFileLinksAsLowerCase(idxBody)

// rewrite .md link paths to match Hugo's pretty link format
idxBody = idxBody.replaceAll('.md)', '/)')

// rewrite raw githubusercontent video links into video tags
idxBody = rewriteRemoteVideoLinks(idxBody)

// rewrite numbered file links
idxBody = rewriteNumberedFileLinks(idxBody)

const idxContent = [idxFront, idxBody].join("\n")
await fs.writeFile(idxMd, idxContent, { encoding: 'utf8' })

Expand Down

0 comments on commit f6bdb2f

Please sign in to comment.