Files
pezkuwi-fellows/.github/scripts/build-mdbook-summary.js
T
2024-02-12 16:28:41 +01:00

45 lines
1.8 KiB
JavaScript

/**
* mdBook relies on the creation of a special SUMMARY.md file
* https://rust-lang.github.io/mdBook/format/summary.html
* This script constructs the summary out of the available source files.
*/
const fs = require('fs');
const summaryPath = "mdbook/src/SUMMARY.md"
module.exports = async ({github, context}) => {
fs.writeFileSync(summaryPath, "# Summary\n\n[Introduction](introduction.md)\n") // Starting point.
const appendRfcsToSummary = (dirPath) => {
for (const filename of fs.readdirSync(dirPath)) {
if (!filename.endsWith(".md")) continue;
const filePath = dirPath + filename
const text = fs.readFileSync(filePath)
let title = text.toString().split(/\n/)
.find(line => line.startsWith("# ") || line.startsWith(" # "))
.replace("# ", "")
for (const markdownLink of title.matchAll(/\[(.*?)\]\((.*?)\)/g)) {
// Replace any [label](destination) markdown links in the title with just the label.
// This is because the titles are turned into links themselves,
// and we cannot have a link inside of a link - it breaks markdown and mdBook is not able to build.
title = title.replace(markdownLink[0], markdownLink[1])
}
// Relative path, without the src prefix (format required by mdbook)
const relativePath = filePath.replace("mdbook/src/", "")
fs.appendFileSync(summaryPath, `- [${title}](${relativePath})\n`)
}
}
fs.appendFileSync(summaryPath, "\n---\n\n# Newly Proposed\n\n")
appendRfcsToSummary("mdbook/src/new/")
fs.appendFileSync(summaryPath, "\n---\n\n# Proposed\n\n")
appendRfcsToSummary("mdbook/src/proposed/")
fs.appendFileSync(summaryPath, "\n---\n\n# Approved\n\n")
appendRfcsToSummary("mdbook/src/approved/")
fs.appendFileSync(summaryPath, "\n---\n\n# Stale\n\n")
appendRfcsToSummary("mdbook/src/stale/")
}