|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import { readFileSync } from "node:fs"; |
| 4 | + |
| 5 | +// order matters here |
| 6 | +const parts = { |
| 7 | + header: "header.md", |
| 8 | + intro: "../spec/intro.md", |
| 9 | + syntax: "../spec/syntax.md", |
| 10 | + abnf: "../spec/message.abnf", |
| 11 | + formatting: "../spec/formatting.md", |
| 12 | + errors: "../spec/errors.md", |
| 13 | + functions: "../spec/functions/README.md", |
| 14 | + stringFunctions: "../spec/functions/string.md", |
| 15 | + numberFunctions: "../spec/functions/number.md", |
| 16 | + datetimeFunctions: "../spec/functions/datetime.md", |
| 17 | + uNamespace: "../spec/u-namespace.md", |
| 18 | + datamodel: "../spec/data-model/README.md", |
| 19 | + json: "../spec/data-model/message.json", |
| 20 | + appendices: "../spec/appendices.md", |
| 21 | + footer: "footer.md", |
| 22 | +}; |
| 23 | + |
| 24 | +for (const [name, path] of Object.entries(parts)) { |
| 25 | + parts[name] = readFileSync(path, "utf8").trim(); |
| 26 | +} |
| 27 | + |
| 28 | +parts.abnf = "## message.abnf\n\n```abnf\n" + parts.abnf + "\n```"; |
| 29 | +parts.json = "## message.json\n\n```json\n" + parts.json + "\n```"; |
| 30 | + |
| 31 | +// Strip title + table of contents |
| 32 | +const introStart = parts.intro.indexOf("## Introduction"); |
| 33 | +if (introStart < 0) throw new Error("Intro start not found"); |
| 34 | +parts.intro = parts.intro.substring(introStart); |
| 35 | + |
| 36 | +parts.functions = parts.functions.replace( |
| 37 | + /^#+ Table of Contents\n\n(.*?)\n\n/ms, |
| 38 | + "" |
| 39 | +); |
| 40 | + |
| 41 | +const result = Object.values(parts) |
| 42 | + .join("\n\n") |
| 43 | + .replace(/ +$/g, "") |
| 44 | + .replace(/\n{3,}/g, "\n\n") |
| 45 | + .replace(/\[(.+?)\]\((.+?)\)/g, fixLink); |
| 46 | +console.log(result); |
| 47 | + |
| 48 | +/** |
| 49 | + * @param {string} link |
| 50 | + * @param {string} label |
| 51 | + * @param {string} target |
| 52 | + */ |
| 53 | +function fixLink(link, label, target) { |
| 54 | + if (target === "../docs/why_mf_next.md") return label; |
| 55 | + if (target.endsWith("message.abnf")) return `[${label}](#messageabnf)`; |
| 56 | + if (target.endsWith("message.abnf")) return `[${label}](#messageabnf)`; |
| 57 | + if (target.endsWith("syntax.md")) return `[${label}](#syntax)`; |
| 58 | + const local = /^(?:\.\/)?[\w./]+\.md(#.+)/.exec(target); |
| 59 | + if (local) return `[${label}](${local[1]})`; |
| 60 | + const tr35 = /\/(tr35(?:-\w+)?)\.html(#.+)?$/.exec(target); |
| 61 | + if (tr35) return `[${label}](${tr35[1]}.md${tr35[2]})`; |
| 62 | + return link; |
| 63 | +} |
0 commit comments