Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/vtbag/website. The @vtbag website. ⭐️ Please star to support this work! ⭐️
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898import fs from 'node:fs';import path from 'node:path';import GithubSlugger from 'github-slugger';import remarkGfm from 'remark-gfm';import remarkMdx from 'remark-mdx';import remarkParse from 'remark-parse';import { unified } from 'unified';
import { decode as decodeEntities } from 'html-entities';
const SITE_URL = 'https://vtbag.dev';const DOCS_DIR_DEFAULT = 'src/content/docs';const OUTPUT_ROOT_DEFAULT = 'vtbag.dev';const MIN_CHARS = 400;const MAX_CHARS = 1500;const OVERLAP_CHARS = 150;
function getCliArg(prefix: string): string | undefined { return process.argv.find((arg) => arg.startsWith(prefix))?.slice(prefix.length);}
const DOCS_DIR = path.resolve(getCliArg('--docs-dir=') ?? DOCS_DIR_DEFAULT);const OUTPUT_ROOT = path.resolve(getCliArg('--output-dir=') ?? OUTPUT_ROOT_DEFAULT);
type AstNode = { type: string; depth?: number; value?: string; lang?: string | null; meta?: string | null; alt?: string | null; url?: string; name?: string; ordered?: boolean; children?: AstNode[]; attributes?: Array<{ type?: string; name?: string; value?: unknown; }>;};
type Block = { text: string; isCode: boolean; forceOwnChunk?: boolean; kind?: 'table'; tableHeader?: string; tableSeparator?: string; tableRows?: string[];};
type Section = { sectionSlug: string; headingTrail: string[]; blocks: Block[]; size: number; parent?: Section;};
type ChunkFile = { id: string; text: string; metadata: { sourcePath: string; pageUrl: string; chunkUrl: string; headings: string[]; sectionSlug: string; sequence: number; charCount: number; split: boolean; oversize: boolean; next?: string; previous?: string; parent?: string; };};
type Manifest = { generatedAt: string; inputDir: string; outputDir: string; docs: number; chunks: number; warnings: string[]; items: Array<{ sourcePath: string; pageUrl: string; chunks: number; }>;};
function walk(dir: string): string[] { return fs.readdirSync(dir, { withFileTypes: true }).flatMap((entry) => { const full = path.join(dir, entry.name);
if (entry.isDirectory()) { return walk(full); }
if (entry.isSymbolicLink()) { try { const stat = fs.statSync(full); if (stat.isDirectory()) { return walk(full); } if (stat.isFile() && (entry.name.endsWith('.mdx') || entry.name.endsWith('.md'))) { return [full]; } } catch { return []; } }
if (entry.isFile() && (entry.name.endsWith('.mdx') || entry.name.endsWith('.md'))) { return [full]; }
return []; });}
function splitFrontmatter(raw: string): { title: string; body: string; } | undefined { const frontmatterMatch = raw.match(/^\s*---\n([\s\S]*?)\n---\n?/); if (!frontmatterMatch) return { title: '', body: raw };
const frontmatter = frontmatterMatch[1] ?? ''; const frontMatterLines = frontmatter.split('\n'); const draft = frontMatterLines.find((line) => /^draft:\s*true/.test(line)); if (draft) return undefined;
let title = get(frontMatterLines, "title"); const demoTitle = get(frontMatterLines, "demoTitle"); let demoText = get(frontMatterLines, "demoText");
if (demoTitle) { demoText = `${demoText}\n\n## ${title}\n\n`; title = demoTitle; } return { title, body: (demoText ? demoText : '') + raw.slice(frontmatterMatch[0].length), };
function get(lines: string[], key: string): string { const line = lines.find((line) => line.trim().startsWith(`${key}:`)) ?? ''; return line.replace(`${key}:`, '').trim().replace(/^"|"$/g, '').replace(/^'|'$/g, ''); }}
function stripHtmlTags(text: string): string { return decodeEntities(text.replace(/<[^>]+>/g, ' '));}
function collapseWhitespace(text: string): string { return text.replace(/\s+/g, ' ');}
function normalizeInlineText(text: string): string { // Preserve content inside inline code spans while normalizing surrounding whitespace. const segments = text.split(/(`[^`]*`)/g); return segments .map((segment) => { if (segment.startsWith('`') && segment.endsWith('`')) return segment; return collapseWhitespace(segment); }) .join('') .trim();}
function isInternalLinkTarget(target: string): boolean { const trimmed = target.trim(); if (!trimmed) return false;
if (trimmed.startsWith('#')) return true; if (trimmed.startsWith('/')) return true; if (trimmed.startsWith('./') || trimmed.startsWith('../')) return true; if (trimmed.startsWith(`${SITE_URL}/`) || trimmed === SITE_URL) return true;
return false;}
function toMarkdownLink(label: string, target: string): string { const linkLabel = label || target; return `[${linkLabel}](${target})`;}
function pathToPageUrl(filePath: string): string { const rel = path.relative(DOCS_DIR, filePath).replace(/\\/g, '/'); const noExt = rel.replace(/\.(mdx|md)$/i, ''); const route = noExt.replace(/\/index$/i, ''); if (!route) return `${SITE_URL}/`; return `${SITE_URL}/${route}/`;}
function pathToDocStem(filePath: string): string { const rel = path.relative(DOCS_DIR, filePath).replace(/\\/g, '/'); const noExt = rel.replace(/\.(mdx|md)$/i, ''); return noExt.replace(/\/index$/i, '') || 'index';}
function parseTree(body: string): AstNode { return unified() .use(remarkParse) .use(remarkGfm) .use(remarkMdx) .parse(body) as AstNode;}
function getStringAttribute(node: AstNode, attributeName: string): string { for (const attribute of node.attributes ?? []) { if (attribute.type !== 'mdxJsxAttribute' || attribute.name !== attributeName) continue; if (typeof attribute.value === 'string') return normalizeInlineText(attribute.value); } return '';}
function isIgnoredElement(name: string | undefined): boolean { if (!name) return false; return ['image', 'script', 'style', 'svg'].includes(name.toLowerCase());}
function isUppercaseComponentName(name: string | undefined): boolean { return Boolean(name && /^[A-Z]/.test(name));}
function renderInline(node: AstNode): string { switch (node.type) { case 'text': return decodeEntities(node.value ?? ''); case 'inlineCode': return `\`${node.value ?? ''}\``; case 'emphasis': return `*${renderInlineChildren(node.children)}*`; case 'strong': return `**${renderInlineChildren(node.children)}**`; case 'delete': return `~~${renderInlineChildren(node.children)}~~`; case 'break': return ' '; case 'link': { const label = renderInlineChildren(node.children); const target = decodeEntities(node.url ?? '').trim(); if (target && isInternalLinkTarget(target)) { return toMarkdownLink(label, target); } return label || target; } case 'image': return normalizeInlineText(node.alt ?? ''); case 'html': return normalizeInlineText(stripHtmlTags(node.value ?? '')); case 'mdxTextExpression': case 'mdxFlowExpression': case 'mdxjsEsm': return ''; case 'mdxJsxTextElement': case 'mdxJsxFlowElement': return renderJsxInline(node); default: return renderInlineChildren(node.children); }}
function renderInlineChildren(children: AstNode[] | undefined): string { if (!children || children.length === 0) return ''; return normalizeInlineText(children.map((child) => renderInline(child)).join(''));}
function renderHtmlBlock(node: AstNode): Block[] { const raw = (node.value ?? '').trim(); if (!raw) return [];
if (/^<\/?(script|style|svg)\b/i.test(raw)) return []; if (/^<\/?details\b/i.test(raw)) return [];
const summaryMatch = raw.match(/^<summary>([\s\S]*?)<\/summary>$/i); if (summaryMatch) { const summaryText = normalizeInlineText(stripHtmlTags(summaryMatch[1] ?? '')); return summaryText ? [{ text: `### ${summaryText}`, isCode: false }] : []; }
const text = normalizeInlineText(stripHtmlTags(raw)); return text ? [{ text, isCode: false }] : [];}
function renderCodeBlock(node: AstNode): Block[] { const info = [node.lang ?? '', node.meta ?? ''] .filter(Boolean) .join(' ') .trim(); const fence = info ? `\`\`\`${info}` : '```'; return [{ text: `${fence}\n${node.value ?? ''}\n\`\`\``, isCode: true, }];}
function renderTable(node: AstNode): Block[] { const rows = (node.children ?? []).map((row) => { const cells = (row.children ?? []).map((cell) => renderInlineChildren(cell.children)); return cells; }).filter((cells) => cells.length > 0);
if (rows.length === 0) return [];
const formatRow = (cells: string[]) => `| ${cells.join(' | ')} |`; const headerCells = rows[0]!; const bodyRows = rows.slice(1); const header = formatRow(headerCells); const separator = `| ${headerCells.map(() => '---').join(' | ')} |`; const tableRows = bodyRows.map((cells) => formatRow(cells)); const text = [header, separator, ...tableRows].join('\n');
return [{ text, isCode: false, kind: 'table', tableHeader: header, tableSeparator: separator, tableRows, }];}
function renderList(node: AstNode, warnings: string[]): Block[] { const ordered = Boolean(node.ordered); const blocks: Block[] = [];
(node.children ?? []).forEach((item, index) => { const itemBlocks = renderFlowChildren(item.children, warnings); if (itemBlocks.length === 0) return;
const prefix = ordered ? `${index + 1}. ` : '- '; const [firstBlock, ...rest] = itemBlocks; if (firstBlock) { blocks.push({ text: `${prefix}${firstBlock.text}`, isCode: firstBlock.isCode, }); } blocks.push(...rest); });
return blocks;}
function renderBlockquote(node: AstNode, warnings: string[]): Block[] { return renderFlowChildren(node.children, warnings).map((block) => ({ text: block.text .split('\n') .map((line) => `> ${line}`) .join('\n'), isCode: block.isCode, }));}
function renderDetails(node: AstNode, warnings: string[]): Block[] { const blocks: Block[] = [];
for (const child of node.children ?? []) { if (child.type === 'paragraph') { const paragraphChildren = child.children ?? []; const [firstInline, ...remainingInline] = paragraphChildren;
if (firstInline?.type === 'mdxJsxTextElement' && firstInline.name?.toLowerCase() === 'summary') { const summaryText = renderInlineChildren(firstInline.children); if (summaryText) blocks.push({ text: `### ${summaryText}`, isCode: false });
const bodyText = renderInlineChildren(remainingInline); if (bodyText) blocks.push({ text: bodyText, isCode: false }); continue; } }
const childName = child.name?.toLowerCase(); if ((child.type === 'mdxJsxTextElement' || child.type === 'mdxJsxFlowElement') && childName === 'summary') { const summaryText = renderInlineChildren(child.children); if (summaryText) blocks.push({ text: `### ${summaryText}`, isCode: false }); continue; } blocks.push(...renderFlowNode(child, warnings)); }
return blocks;}
function getSummaryHeading(blocks: Block[]): string { const firstBlock = blocks[0]?.text ?? ''; const match = firstBlock.match(/^###\s+(.+)$/); return match?.[1]?.trim() ?? '';}
function renderComponentPropBlocks(node: AstNode): Block[] { const values = ['title', 'label', 'heading'] .map((name) => getStringAttribute(node, name)) .filter(Boolean);
return [...new Set(values)].map((value) => ({ text: value, isCode: false }));}
function renderJsxInline(node: AstNode): string { const name = node.name?.toLowerCase(); if (isIgnoredElement(name)) return ''; if (name === 'summary') return renderInlineChildren(node.children); if (isUppercaseComponentName(node.name)) { const propText = ['title', 'label', 'heading'] .map((attribute) => getStringAttribute(node, attribute)) .filter(Boolean) .join(' '); const childText = renderInlineChildren(node.children); return normalizeInlineText([propText, childText].filter(Boolean).join(' ')); } return renderInlineChildren(node.children);}
function renderFlowNode(node: AstNode, warnings: string[]): Block[] { switch (node.type) { case 'paragraph': { const text = renderInlineChildren(node.children); return text ? [{ text, isCode: false }] : []; } case 'code': return renderCodeBlock(node); case 'list': return renderList(node, warnings); case 'blockquote': return renderBlockquote(node, warnings); case 'table': return renderTable(node); case 'html': return renderHtmlBlock(node); case 'mdxFlowExpression': case 'mdxjsEsm': return []; case 'mdxJsxTextElement': { const text = renderJsxInline(node); return text ? [{ text, isCode: false }] : []; } case 'mdxJsxFlowElement': { const name = node.name?.toLowerCase(); if (isIgnoredElement(name)) return []; if (name === 'details') return renderDetails(node, warnings); if (name === 'summary') { const summaryText = renderInlineChildren(node.children); return summaryText ? [{ text: `### ${summaryText}`, isCode: false }] : []; }
const propBlocks = isUppercaseComponentName(node.name) ? renderComponentPropBlocks(node) : []; const childBlocks = renderFlowChildren(node.children, warnings); return [...propBlocks, ...childBlocks]; } case 'heading': { const text = renderInlineChildren(node.children); if (!text) return []; return [{ text: `${'#'.repeat(node.depth ?? 2)} ${text}`, isCode: false }]; } default: return renderFlowChildren(node.children, warnings); }}
function renderFlowChildren(children: AstNode[] | undefined, warnings: string[]): Block[] { if (!children || children.length === 0) return []; return children.flatMap((child) => renderFlowNode(child, warnings));}
function joinBlocks(blocks: Block[]): string { return blocks.map((block) => block.text.trim()).filter(Boolean).join('\n\n').trim();}
function splitOversizedTableBlock(block: Block): Block[] { if (block.kind !== 'table' || !block.tableHeader || !block.tableSeparator || !block.tableRows) { return [block]; }
if (block.text.length <= MAX_CHARS) return [block];
const tableHeader = block.tableHeader; const tableSeparator = block.tableSeparator; const tableRows = block.tableRows;
return tableRows.map((row) => ({ text: [tableHeader, tableSeparator, row].join('\n'), isCode: false, forceOwnChunk: true, kind: 'table', tableHeader, tableSeparator, tableRows: [row], }));}
function expandBlocksForChunking(blocks: Block[]): Block[] { return blocks.flatMap((block) => splitOversizedTableBlock(block));}
function buildSectionChunks(blocks: Block[]): Array<{ text: string; oversize: boolean; }> { const expandedBlocks = expandBlocksForChunking(blocks); const fullText = joinBlocks(expandedBlocks); if (!fullText) return []; if (fullText.length <= MAX_CHARS) { return [{ text: fullText, oversize: false }]; }
const result: Array<{ text: string; oversize: boolean; }> = []; let chunkBlocks: Block[] = []; let previousBlocksForOverlap: Block[] = [];
const chunkLength = (items: Block[]) => joinBlocks(items).length;
const pushChunk = () => { if (chunkBlocks.length === 0) return; const text = joinBlocks(chunkBlocks); if (!text) return; result.push({ text, oversize: text.length > MAX_CHARS }); previousBlocksForOverlap = [...chunkBlocks]; chunkBlocks = []; };
for (const block of expandedBlocks) { if (block.forceOwnChunk) { pushChunk(); result.push({ text: block.text, oversize: block.text.length > MAX_CHARS }); previousBlocksForOverlap = [block]; continue; }
if (block.text.length > MAX_CHARS) { pushChunk(); result.push({ text: block.text, oversize: true }); previousBlocksForOverlap = [block]; continue; }
const candidateBlocks = [...chunkBlocks, block]; if (chunkLength(candidateBlocks) <= MAX_CHARS) { chunkBlocks = candidateBlocks; continue; }
pushChunk();
const overlapBlocks: Block[] = []; let overlapLength = 0; for (let index = previousBlocksForOverlap.length - 1; index >= 0; index -= 1) { const candidate = previousBlocksForOverlap[index]!; if (candidate.isCode) continue; overlapBlocks.unshift(candidate); overlapLength += candidate.text.length; if (overlapLength >= OVERLAP_CHARS) break; }
chunkBlocks = [...overlapBlocks, block]; }
pushChunk(); return result;}
function parseSections(filePath: string, warnings: string[]): { title: string; sections: Section[]; } | undefined { const raw = fs.readFileSync(filePath, 'utf8'); const split = splitFrontmatter(raw); if (!split) return undefined; const { title, body } = split; const tree = parseTree(body); const slugger = new GithubSlugger();
let currentHeadingTrail = title ? [title] : []; let currentSectionSlug = 'top'; let currentSectionBlocks: Block[] = []; const sections: Section[] = [];
const flushSection = () => { const content = joinBlocks(currentSectionBlocks); if (!content) { currentSectionBlocks = []; return; } sections.push({ sectionSlug: currentSectionSlug, headingTrail: [...currentHeadingTrail], blocks: currentSectionBlocks, size: content.length, }); currentSectionBlocks = []; };
for (const child of tree.children ?? []) { if (child.type === 'mdxJsxFlowElement' && child.name?.toLowerCase() === 'details') { const detailBlocks = renderDetails(child, warnings); const summaryHeading = getSummaryHeading(detailBlocks); if (summaryHeading) { flushSection(); const detailHeadingTrail = [...currentHeadingTrail, summaryHeading]; let detailSectionSlug = slugger.slug(summaryHeading); let detailSectionHeadingTrail = [...detailHeadingTrail]; let detailSectionBlocks: Block[] = [];
const flushDetailSection = () => { const content = joinBlocks(detailSectionBlocks); if (!content) { detailSectionBlocks = []; return; } sections.push({ sectionSlug: detailSectionSlug, headingTrail: [...detailSectionHeadingTrail], blocks: detailSectionBlocks, size: content.length, }); detailSectionBlocks = []; };
for (const block of detailBlocks.slice(1)) { const headingMatch = !block.isCode ? block.text.match(/^(#{2,6})\s+(.+)$/) : null; if (!headingMatch) { detailSectionBlocks.push(block); continue; }
flushDetailSection(); const headingText = headingMatch[2]?.trim() ?? ''; if (!headingText) continue; detailSectionSlug = slugger.slug(headingText); detailSectionHeadingTrail = [...detailHeadingTrail, headingText]; }
flushDetailSection(); continue; } }
if (child.type === 'heading') { const headingText = renderInlineChildren(child.children); const depth = child.depth!;
if (depth === 1) { if (!title && headingText) { currentHeadingTrail = [headingText]; } else if (headingText) { currentSectionBlocks.push({ text: `# ${headingText}`, isCode: false }); } continue; }
if (depth >= 2 && headingText) { flushSection(); while (currentHeadingTrail.length >= depth) { currentHeadingTrail.pop(); } currentHeadingTrail.push(headingText); currentSectionSlug = slugger.slug(headingText); continue; } }
currentSectionBlocks.push(...renderFlowNode(child, warnings)); }
flushSection();
return { title, sections, };}
function ensureDir(dirPath: string): void { fs.mkdirSync(dirPath, { recursive: true });}
function writeJson(filePath: string, data: unknown): void { ensureDir(path.dirname(filePath)); fs.writeFileSync(filePath, `${JSON.stringify(data, null, 2)}\n`, 'utf8');}
function sanitizeFilename(input: string): string { return input.replace(/[^a-zA-Z0-9._-]/g, '-');}
function maybeCleanOutput(): void { if (!process.argv.includes('--clean')) return; fs.rmSync(OUTPUT_ROOT, { recursive: true, force: true });}
function ensureDemoSymlink(): boolean { const linkPath = path.resolve(DOCS_DIR, 'demo'); const targetPath = path.resolve('src/pages/demo-explainer'); const relativeTarget = path.relative(path.dirname(linkPath), targetPath);
try { const stat = fs.lstatSync(linkPath); if (stat.isSymbolicLink()) { const existingTarget = fs.readlinkSync(linkPath); if (existingTarget === relativeTarget || path.resolve(path.dirname(linkPath), existingTarget) === targetPath) { return false; } } fs.rmSync(linkPath, { recursive: true, force: true }); } catch (error) { if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error; }
fs.symlinkSync(relativeTarget, linkPath, 'dir'); return true;}
function removeDemoSymlink(): void { const linkPath = path.resolve(DOCS_DIR, 'demo'); const targetPath = path.resolve('src/pages/demo-explainer'); const relativeTarget = path.relative(path.dirname(linkPath), targetPath);
try { const stat = fs.lstatSync(linkPath); if (!stat.isSymbolicLink()) return;
const existingTarget = fs.readlinkSync(linkPath); if (existingTarget !== relativeTarget && path.resolve(path.dirname(linkPath), existingTarget) !== targetPath) { return; }
fs.rmSync(linkPath, { force: true }); } catch (error) { if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error; }}
function buildFiles(files: string[]): void { maybeCleanOutput(); ensureDir(OUTPUT_ROOT);
const onlyArg = process.argv.find((arg) => arg.startsWith('--only=')); const onlyValues = (onlyArg?.slice('--only='.length) ?? '') .split(',') .map((s) => s.trim()) .filter(Boolean);
const warnings: string[] = []; const manifestItems: Manifest['items'] = []; let totalChunks = 0; let processedDocs = 0;
const fileMap = new Map<string, ChunkFile>();
for (const file of files) { const rel = path.relative(DOCS_DIR, file).replace(/\\/g, '/'); if (onlyValues.length > 0 && !onlyValues.includes(rel)) continue;
processedDocs += 1; const pageUrl = pathToPageUrl(file); const docStem = pathToDocStem(file); const parsedSections = parseSections(file, warnings); if (!parsedSections) continue; const { sections } = parsedSections;
let j = 0; for (let i = 1; i < sections.length; ++i) { const previous = sections[j]!; const current = sections[i]!; const heading = `${'#'.repeat(current.headingTrail.length)} ${current.headingTrail.slice(-1)[0]}`; const size = previous.size + current.size + 2 + heading.length; if (previous.headingTrail.length === current.headingTrail.length - 1 && size < MIN_CHARS) { previous.blocks.push({ text: heading, isCode: false }); previous.blocks.push(...current.blocks); previous.size = size; current.blocks = []; current.size = 0; console.log(`${rel}: Merged section "${current.headingTrail.slice(-1)[0]}" into previous section due to small size (${size} chars)`); } else { j = i; } } let docChunkCount = 0;
let lastChunk: ChunkFile | undefined = undefined; let lastSection: Section | undefined = undefined;
for (let i = 0; i < sections.length; ++i) { const section = sections[i]!; if (section.size === 0) continue; for (let j = i + 1; j < sections.length; ++j) { const next = sections[j]!; if (next.size === 0) continue; if (section.headingTrail.length < next.headingTrail.length && section.headingTrail.every((value, index) => value === next.headingTrail[index])) { next.parent = section; } else { break; }; } } for (const section of sections) { if (section.size === 0) continue;
if (lastSection?.parent !== section.parent) { lastChunk = undefined; } const sectionChunks = buildSectionChunks(section.blocks); const split = sectionChunks.length > 1;
sectionChunks.forEach((chunk, index) => { const sequence = index + 1; const headingsPrefix = section.headingTrail.map((heading, depth) => `${'#'.repeat(depth + 1)} ${heading}`).join('\n'); const text = `${headingsPrefix}\n\n${chunk.text}`.trim(); const page = split && sequence > 1 ? `${pageUrl}?s=${sequence}` : pageUrl; const chunkUrl = section.sectionSlug === 'top' ? page : `${page}#${section.sectionSlug}`; section; lastChunk && (lastChunk.metadata.next = chunkUrl); const id = `${docStem}::${section.sectionSlug}::${sequence}`; const json: ChunkFile = { id, text, metadata: { sourcePath: rel, pageUrl, chunkUrl, headings: section.headingTrail, sectionSlug: section.sectionSlug, sequence, charCount: text.length, split, oversize: chunk.oversize, previous: lastChunk?.metadata.chunkUrl!, parent: section.parent ? section.parent.sectionSlug === 'top' ? pageUrl : `${pageUrl}#${section.parent.sectionSlug}` : undefined!, }, }; lastChunk = json; if (chunk.oversize) { warnings.push(`oversize chunk: ${id} (${text.length} chars)`); }
const sequenceSuffix = split ? `-${String(sequence).padStart(2, '0')}` : ''; const outFile = path.join( OUTPUT_ROOT, sanitizeFilename(docStem), `${sanitizeFilename(section.sectionSlug)}${sequenceSuffix}.json`, );
fileMap.set(outFile, json); docChunkCount += 1; totalChunks += 1; }); lastSection = section; } fileMap.forEach((json, outFile) => { writeJson(outFile, json); }); fileMap.clear(); manifestItems.push({ sourcePath: rel, pageUrl, chunks: docChunkCount }); }
const manifest: Manifest = { generatedAt: new Date().toISOString(), inputDir: path.relative(process.cwd(), DOCS_DIR), outputDir: path.relative(process.cwd(), OUTPUT_ROOT), docs: processedDocs, chunks: totalChunks, warnings, items: manifestItems, };
writeJson(path.join(OUTPUT_ROOT, 'manifest.jsonc'), manifest);
console.log(`Chunk generation complete. docs=${processedDocs}, chunks=${totalChunks}`); if (warnings.length > 0) { console.log(`Warnings (${warnings.length}):`); for (const warning of warnings.slice(0, 20)) { console.log(`- ${warning}`); } if (warnings.length > 20) { console.log(`...and ${warnings.length - 20} more`); } }}
ensureDemoSymlink();
try { const allFiles = walk(DOCS_DIR).filter((file) => !( file.endsWith("/404.md") || file.endsWith("/baglog.mdx") || file.endsWith("/BasicAC.mdx") || file.endsWith("/BasicC.mdx") || file.endsWith("/BasicMC.mdx") || file.endsWith("/BasicAC2.mdx") || file.endsWith("/BasicC2.mdx") || file.endsWith("/BasicMC2.mdx") )); buildFiles(allFiles);} finally { removeDemoSymlink();}