Luigit
repositories / will

will

owned by admin

scripts/render-docs.mjs

Raw
#!/usr/bin/env node
/**
 * Build-time docs renderer (spec: markdown -> immutable static HTML; marked
 * is a devDep, the service serves only static output).
 * Writes docs/dist/<page>.html plus a manifest.json for /api/docs.
 */
import { mkdir, readdir, readFile, writeFile } from 'node:fs/promises'
import { dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url'
import { Marked } from 'marked'

const root = join(dirname(fileURLToPath(import.meta.url)), '..')
const srcDir = join(root, 'docs')
const outDir = join(root, 'docs/dist')
const marked = new Marked()

const page = (title, body) => `<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>${title} · will docs</title>
<link rel="stylesheet" href="/assets/docs.css">
</head>
<body>
<main id="main">
<article>
${body}
</article>
</main>
</body>
</html>
`

const pages = []
await mkdir(outDir, { recursive: true })
for (const file of (await readdir(srcDir)).filter((f) => f.endsWith('.md')).sort()) {
  const name = file.replace(/\.md$/, '')
  const raw = await readFile(join(srcDir, file), 'utf8')
  const body = await marked.parse(raw)
  await writeFile(join(outDir, `${name}.html`), page(name, body), 'utf8')
  pages.push(name)
}
await writeFile(
  join(outDir, 'index.html'),
  page('docs', `<ul>${pages.map((p) => `<li><a href="/docs/${p}">${p}</a></li>`).join('\n')}</ul>`),
  'utf8',
)
await writeFile(join(outDir, 'manifest.json'), JSON.stringify({ pages }, null, 2), 'utf8')
console.log(`docs: rendered ${pages.length} pages -> docs/dist`)