Files
oh-my-opencode/web/scripts/generate-docs-content.mjs
T
2026-05-14 14:18:13 +09:00

60 lines
2.0 KiB
JavaScript

import { readFile, writeFile } from "node:fs/promises"
import path from "node:path"
import { Marked } from "marked"
const SECTIONS = [
{ id: "overview", file: "guide/overview.md" },
{ id: "installation", file: "guide/installation.md" },
{ id: "orchestration", file: "guide/orchestration.md" },
{ id: "agent-model-matching", file: "guide/agent-model-matching.md" },
{ id: "team-mode", file: "guide/team-mode.md" },
{ id: "cli", file: "reference/cli.md" },
{ id: "configuration", file: "reference/configuration.md" },
{ id: "features", file: "reference/features.md" },
{ id: "manifesto", file: "manifesto.md" },
]
const DOCS_ROOT = path.resolve(process.cwd(), "..", "docs")
const OUTPUT = path.resolve(process.cwd(), "lib", "docs-content.generated.ts")
const sectionIdByFile = new Map(SECTIONS.map((section) => [section.file, section.id]))
function rewriteDocsLink(sourceFile, href) {
if (!href || href.startsWith("#") || href.startsWith("//")) return href
if (/^[a-z][a-z0-9+.-]*:/i.test(href)) return href
const [hrefPath] = href.split("#", 1)
if (!hrefPath) return href
const sourceDirectory = path.posix.dirname(sourceFile)
const targetFile = path.posix.normalize(path.posix.join(sourceDirectory, hrefPath))
const sectionId = sectionIdByFile.get(targetFile)
return sectionId ? `#${sectionId}` : href
}
function createMarked(sourceFile) {
const marked = new Marked({ gfm: true, breaks: false })
marked.use({
walkTokens(token) {
if (token.type !== "link") return
token.href = rewriteDocsLink(sourceFile, token.href)
},
})
return marked
}
const sources = {}
for (const s of SECTIONS) {
const md = await readFile(path.join(DOCS_ROOT, s.file), "utf8")
sources[s.file] = await createMarked(s.file).parse(md)
}
const out =
"// Generated by scripts/generate-docs-content.mjs - DO NOT EDIT\n" +
"export const DOC_SOURCES: Record<string, string> = " +
JSON.stringify(sources, null, 2) +
"\n"
await writeFile(OUTPUT, out)
console.log("Generated " + OUTPUT + " with " + SECTIONS.length + " HTML-compiled docs")