-
Notifications
You must be signed in to change notification settings - Fork 220
Expand file tree
/
Copy pathgenerate-markdown.mjs
More file actions
265 lines (206 loc) · 8.41 KB
/
generate-markdown.mjs
File metadata and controls
265 lines (206 loc) · 8.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/usr/bin/env node
/**
* Post-build script: converts every Antora HTML page into a Markdown sibling
* so AI agents can fetch clean, low-token content via content negotiation.
*
* Usage: node scripts/generate-markdown.mjs [buildDir]
* Default buildDir = build/site
*/
import { readdir, readFile, writeFile, mkdir } from 'node:fs/promises';
import { join, relative, dirname } from 'node:path';
import { JSDOM } from 'jsdom';
import { convertHtmlToMarkdown } from 'dom-to-semantic-markdown';
import { encode } from 'gpt-tokenizer';
// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------
const BUILD_DIR = process.argv[2] || 'build/site';
const ADMONITION_TYPES = [ 'note', 'warning', 'tip', 'important', 'caution' ];
const HEADING_ANCHOR_SELECTOR = [
'h2', 'h3', 'h4', 'h5', 'h6'
].map((h) => `${h} > a.anchor`).join(', ');
// ---------------------------------------------------------------------------
// DOM helpers — small, pure-ish transforms operating on a single element
// ---------------------------------------------------------------------------
const removeAll = (root, selector) =>
root.querySelectorAll(selector).forEach((el) => el.remove());
const capitalize = (s) =>
s.charAt(0).toUpperCase() + s.slice(1);
const resolveAdmonitionType = (el) =>
ADMONITION_TYPES.find((t) => el.classList.contains(t)) ?? 'note';
const isCardLayoutRow = (tr) => {
const td = tr.querySelector('td');
return td !== null && td.querySelector('.lead, a.xref') !== null;
};
const isCardLayoutTable = (table) =>
!table.querySelector('thead') &&
[ ...table.querySelectorAll('tbody tr') ].every(isCardLayoutRow);
// ---------------------------------------------------------------------------
// DOM transforms — each receives (article, document) and mutates in-place
// ---------------------------------------------------------------------------
const stripNonContent = (article) => {
removeAll(article, 'style, script, .signup-promo');
};
const rewriteAdmonitions = (article, doc) => {
article.querySelectorAll('.admonitionblock').forEach((adm) => {
const content = adm.querySelector('td.content');
if (!content) return;
const bq = doc.createElement('blockquote');
const label = doc.createElement('strong');
label.textContent = capitalize(resolveAdmonitionType(adm)) + ': ';
bq.appendChild(label);
while (content.firstChild) {
bq.appendChild(content.firstChild);
}
adm.replaceWith(bq);
});
};
const extractDemoCode = (demo, doc) => {
const jsPane = demo.querySelector('[id*="_pane_js_"]');
const code = jsPane?.querySelector('code');
if (!code) return null;
const fragment = doc.createDocumentFragment();
const heading = doc.createElement('p');
heading.innerHTML = '<strong>Example</strong>';
fragment.appendChild(heading);
const pre = doc.createElement('pre');
const codeEl = doc.createElement('code');
codeEl.className = 'language-js';
codeEl.textContent = code.textContent;
pre.appendChild(codeEl);
fragment.appendChild(pre);
return fragment;
};
const rewriteLiveDemos = (article, doc) => {
article.querySelectorAll('.live-demo').forEach((demo) => {
const replacement = extractDemoCode(demo, doc);
replacement ? demo.replaceWith(replacement) : demo.remove();
});
};
const stripHeadingAnchors = (article) => {
removeAll(article, HEADING_ANCHOR_SELECTOR);
};
const buildListItem = (td, doc) => {
const link = td.querySelector('.lead a');
if (!link) return null;
const desc = td.querySelector('.lead ~ .paragraph');
const li = doc.createElement('li');
const strong = doc.createElement('strong');
strong.appendChild(link.cloneNode(true));
li.appendChild(strong);
if (desc) {
li.appendChild(doc.createTextNode(' \u2014 ' + desc.textContent.trim()));
}
return li;
};
const rewriteCardTables = (article, doc) => {
article.querySelectorAll('table.tableblock').forEach((table) => {
if (!isCardLayoutTable(table)) return;
const items = [ ...table.querySelectorAll('tbody td') ]
.map((td) => buildListItem(td, doc))
.filter(Boolean);
if (items.length === 0) return;
const ul = doc.createElement('ul');
items.forEach((li) => ul.appendChild(li));
table.replaceWith(ul);
});
};
// ---------------------------------------------------------------------------
// Preprocessing pipeline
// ---------------------------------------------------------------------------
const TRANSFORMS = [
stripNonContent,
rewriteAdmonitions,
rewriteLiveDemos,
stripHeadingAnchors,
rewriteCardTables,
];
const preprocess = (articleEl, doc) => {
const article = articleEl.cloneNode(true);
TRANSFORMS.forEach((fn) => fn(article, doc));
return article;
};
// ---------------------------------------------------------------------------
// Conversion
// ---------------------------------------------------------------------------
const D2M_OPTIONS = (dom) => ({
overrideDOMParser: new dom.window.DOMParser(),
extractMainContent: false,
enableTableColumnTracking: false,
refifyUrls: false,
websiteDomain: 'https://www.tiny.cloud',
});
const fixBlankAnchors = (md) =>
md.replace(/about:blank#/g, '#');
const toMarkdown = (articleEl, dom) => {
const article = preprocess(articleEl, dom.window.document);
const raw = convertHtmlToMarkdown(article.innerHTML, D2M_OPTIONS(dom));
return fixBlankAnchors(raw);
};
// ---------------------------------------------------------------------------
// Frontmatter
// ---------------------------------------------------------------------------
const escapeYaml = (s) =>
s.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
const buildFrontmatter = (title, tokens) =>
`---\ntitle: "${escapeYaml(title)}"\ntokens: ${tokens}\n---\n`;
// ---------------------------------------------------------------------------
// Title extraction
// ---------------------------------------------------------------------------
const extractTitle = (doc) =>
doc.querySelector('article.doc h1')?.textContent?.trim()
?? doc.querySelector('title')?.textContent?.trim()?.replace(/ \|.*$/, '')
?? 'Untitled';
// ---------------------------------------------------------------------------
// File walking
// ---------------------------------------------------------------------------
const collectHtmlFiles = async (dir) => {
const entries = await readdir(dir, { withFileTypes: true });
const nested = await Promise.all(
entries.map((entry) => {
const full = join(dir, entry.name);
return entry.isDirectory()
? collectHtmlFiles(full)
: entry.name.endsWith('.html') ? [ full ] : [];
})
);
return nested.flat();
};
// ---------------------------------------------------------------------------
// Single-page conversion
// ---------------------------------------------------------------------------
const convertPage = async (htmlPath) => {
const html = await readFile(htmlPath, 'utf-8');
const dom = new JSDOM(html);
const articleEl = dom.window.document.querySelector('article.doc');
if (!articleEl) return null;
const title = extractTitle(dom.window.document);
const markdown = toMarkdown(articleEl, dom);
const tokens = encode(markdown).length;
const content = buildFrontmatter(title, tokens) + markdown + '\n';
const mdPath = htmlPath.replace(/\.html$/, '.md');
await mkdir(dirname(mdPath), { recursive: true });
await writeFile(mdPath, content, 'utf-8');
return { path: '/' + relative(BUILD_DIR, dirname(htmlPath)) + '/', tokens };
};
// ---------------------------------------------------------------------------
// Main
// ---------------------------------------------------------------------------
const main = async () => {
console.log(`Generating markdown siblings in ${BUILD_DIR} …`);
const htmlFiles = await collectHtmlFiles(BUILD_DIR);
const pages = [];
for (const htmlPath of htmlFiles) {
const result = await convertPage(htmlPath);
if (result) pages.push(result);
}
const manifest = Object.fromEntries(pages.map(({ path, tokens }) => [ path, tokens ]));
const manifestPath = join(BUILD_DIR, '_markdown-manifest.json');
await writeFile(manifestPath, JSON.stringify(manifest, null, 2) + '\n', 'utf-8');
const skipped = htmlFiles.length - pages.length;
console.log(`Done. ${pages.length} pages converted, ${skipped} skipped (no article.doc). Manifest → ${manifestPath}`);
};
main().catch((err) => {
console.error(err);
process.exit(1);
});