const {readFileSync} = require("fs") const {basename} = require("path") let [template, ...chapters] = process.argv.slice(2) function esc(str) { return str.replace(/[<>&"]/g, ch => ch == "<" ? "<" : ch == ">" ? ">" : ch == "&" ? "&" : """) } let toc = "" const section = /]*>]*?id="(h_.*?)".*?><\/a>(.*?)<\/h2>/g for (let chapter of chapters) { let text = readFileSync(chapter, "utf8") let base = basename(chapter) let title = /(.*?)<\/h1>/.exec(text)[1] toc += `
  • ${esc(title)}
      \n` for (let match; match = section.exec(text);) toc += `
    1. ${esc(match[2])}
    2. \n` toc += `
  • \n` } console.log(readFileSync(template, "utf8").replace("{{full_toc}}", toc))