SPB Git
969 B · 17 lines typescript
1/** Split page texts into overlapping chunks of ~1,200 characters aligned on paragraph boundaries; keeps page ranges. */2export type Chunk = { index: number; pageFrom: number; pageTo: number; text: string };3export function chunkPages(pages: string[], target = 1200, overlap = 150): Chunk[] {4  const out: Chunk[] = []; let buf = ""; let from = 1; let idx = 0;5  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); };6  pages.forEach((page, i) => {7    const pno = i + 1; if (!buf.trim()) from = pno;8    for (const para of page.split(/\n{2,}/)) {9      const p = para.replace(/-\n/g, "").replace(/\n/g, " ").trim(); if (!p) continue;10      if (buf.length + p.length > target && buf.length > target / 2) { flush(pno); from = pno; }11      buf += (buf ? "\n" : "") + p;12    }13  });14  if (buf.trim()) flush(pages.length);15  return out;16}17
Files · 734affbc3f56470ca2e43d159e14b200279bfbfc
Menu

Theme