/** Split page texts into overlapping chunks of ~1,200 characters aligned on paragraph boundaries; keeps page ranges. */ export type Chunk = { index: number; pageFrom: number; pageTo: number; text: string }; export function chunkPages(pages: string[], target = 1200, overlap = 150): Chunk[] { const out: Chunk[] = []; let buf = ""; let from = 1; let idx = 0; const flush = (pageTo: number) => { const t = buf.replace(/\s+/g, " ").trim(); if (t.length > 80) out.push({ index: idx++, pageFrom: from, pageTo, text: t }); buf = buf.slice(-overlap); }; pages.forEach((page, i) => { const pno = i + 1; if (!buf.trim()) from = pno; for (const para of page.split(/\n{2,}/)) { const p = para.replace(/-\n/g, "").replace(/\n/g, " ").trim(); if (!p) continue; if (buf.length + p.length > target && buf.length > target / 2) { flush(pno); from = pno; } buf += (buf ? "\n" : "") + p; } }); if (buf.trim()) flush(pages.length); return out; }