|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * Post-build script: converts every Antora HTML page into a Markdown sibling |
| 5 | + * so AI agents can fetch clean, low-token content via content negotiation. |
| 6 | + * |
| 7 | + * Uses dom-to-semantic-markdown (d2m) for conversion (preserves links in tables). |
| 8 | + * |
| 9 | + * Usage: node scripts/generate-markdown.mjs [buildDir] |
| 10 | + * Default buildDir = build/site |
| 11 | + */ |
| 12 | + |
| 13 | +import { readdir, readFile, writeFile, mkdir } from 'node:fs/promises'; |
| 14 | +import { join, relative, dirname } from 'node:path'; |
| 15 | +import { JSDOM } from 'jsdom'; |
| 16 | +import { convertHtmlToMarkdown } from 'dom-to-semantic-markdown'; |
| 17 | +import { encode } from 'gpt-tokenizer'; |
| 18 | + |
| 19 | +const BUILD_DIR = process.argv[2] || 'build/site'; |
| 20 | + |
| 21 | +// --------------------------------------------------------------------------- |
| 22 | +// Helpers |
| 23 | +// --------------------------------------------------------------------------- |
| 24 | + |
| 25 | +async function* walkHtml(dir) { |
| 26 | + for (const entry of await readdir(dir, { withFileTypes: true })) { |
| 27 | + const full = join(dir, entry.name); |
| 28 | + if (entry.isDirectory()) { |
| 29 | + yield* walkHtml(full); |
| 30 | + } else if (entry.name.endsWith('.html')) { |
| 31 | + yield full; |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +function extractTitle(doc) { |
| 37 | + const h1 = doc.querySelector('article.doc h1'); |
| 38 | + if (h1) return h1.textContent.trim(); |
| 39 | + const title = doc.querySelector('title'); |
| 40 | + if (title) return title.textContent.trim().replace(/ \|.*$/, ''); |
| 41 | + return 'Untitled'; |
| 42 | +} |
| 43 | + |
| 44 | +function buildFrontmatter(title, tokens) { |
| 45 | + return [ |
| 46 | + '---', |
| 47 | + `title: "${title.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}"`, |
| 48 | + `tokens: ${tokens}`, |
| 49 | + '---', |
| 50 | + '', |
| 51 | + ].join('\n'); |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * Convert article.doc HTML to Markdown using dom-to-semantic-markdown. |
| 56 | + */ |
| 57 | +function convertToMarkdown(articleHtml, dom) { |
| 58 | + return convertHtmlToMarkdown(articleHtml, { |
| 59 | + overrideDOMParser: new dom.window.DOMParser(), |
| 60 | + extractMainContent: false, // we already extracted article.doc |
| 61 | + enableTableColumnTracking: false, |
| 62 | + refifyUrls: false, |
| 63 | + websiteDomain: 'https://www.tiny.cloud', |
| 64 | + }); |
| 65 | +} |
| 66 | + |
| 67 | +// --------------------------------------------------------------------------- |
| 68 | +// Main |
| 69 | +// --------------------------------------------------------------------------- |
| 70 | + |
| 71 | +async function main() { |
| 72 | + const manifest = {}; |
| 73 | + let converted = 0; |
| 74 | + let skipped = 0; |
| 75 | + |
| 76 | + console.log(`Generating markdown siblings in ${BUILD_DIR} …`); |
| 77 | + |
| 78 | + for await (const htmlPath of walkHtml(BUILD_DIR)) { |
| 79 | + const html = await readFile(htmlPath, 'utf-8'); |
| 80 | + const dom = new JSDOM(html); |
| 81 | + const article = dom.window.document.querySelector('article.doc'); |
| 82 | + |
| 83 | + if (!article) { |
| 84 | + skipped++; |
| 85 | + continue; |
| 86 | + } |
| 87 | + |
| 88 | + const title = extractTitle(dom.window.document); |
| 89 | + const markdown = convertToMarkdown(article.innerHTML, dom); |
| 90 | + const tokens = encode(markdown).length; |
| 91 | + const frontmatter = buildFrontmatter(title, tokens); |
| 92 | + const fullMd = frontmatter + markdown + '\n'; |
| 93 | + |
| 94 | + const mdPath = htmlPath.replace(/\.html$/, '.md'); |
| 95 | + await mkdir(dirname(mdPath), { recursive: true }); |
| 96 | + await writeFile(mdPath, fullMd, 'utf-8'); |
| 97 | + |
| 98 | + const urlPath = '/' + relative(BUILD_DIR, dirname(htmlPath)) + '/'; |
| 99 | + manifest[urlPath] = tokens; |
| 100 | + converted++; |
| 101 | + } |
| 102 | + |
| 103 | + const manifestPath = join(BUILD_DIR, '_markdown-manifest.json'); |
| 104 | + await writeFile(manifestPath, JSON.stringify(manifest, null, 2) + '\n', 'utf-8'); |
| 105 | + |
| 106 | + console.log( |
| 107 | + `Done. ${converted} pages converted, ${skipped} skipped (no article.doc). Manifest → ${manifestPath}` |
| 108 | + ); |
| 109 | +} |
| 110 | + |
| 111 | +main().catch((err) => { |
| 112 | + console.error(err); |
| 113 | + process.exit(1); |
| 114 | +}); |
0 commit comments