// PICKING A POST ITEM's EXCERPT BY SELECTING ITS WORDS (umtool redesign L6, item 4): a selection in the post's // text (character offsets into cardText, post-item.mjs) made into the excerpt's span format the build reads -- // `entry.excerpt`, VERBATIM strings of the post's words in the post's order, each found by indexOf from the end // of the one before (post-item.mjs excerptRanges). Pure, no imports: the inspector's client component runs it, // and pnpm test:scripts checks it. const isWordChar = (c) => !!c && !/\s/.test(c); /** * A selection snapped OUT to whole words and trimmed of the space at its ends: `[start, end)` in `text`, or null * when it holds no word. * @param {string} text * @param {number} start * @param {number} end */ export function snapToWords(text, start, end) { let a = Math.max(0, Math.min(start, end)); let b = Math.min(text.length, Math.max(start, end)); while (a < b && /\s/.test(text[a])) a += 1; while (b > a && /\s/.test(text[b - 1])) b -= 1; if (a >= b) return null; while (a > 0 && isWordChar(text[a - 1])) a -= 1; while (b < text.length && isWordChar(text[b])) b += 1; return { start: a, end: b }; } /** * The ranges with `r` added, in the post's order, overlapping or touching ranges merged. * @param {Array<{ start: number, end: number }>} ranges * @param {{ start: number, end: number }} r */ export function addRange(ranges, r) { const all = [...ranges, r].sort((x, y) => x.start - y.start); const out = []; for (const x of all) { const last = out.at(-1); if (last && x.start <= last.end) last.end = Math.max(last.end, x.end); else out.push({ ...x }); } return out; } /** * Where the build will FIND each span (indexOf from the end of the one before): a span whose words occur earlier * in the post too would be read there, so it is widened a word to the left until it is found where it was * picked. Returns the ranges (possibly widened) in order. * @param {string} text * @param {Array<{ start: number, end: number }>} ranges */ export function settleRanges(text, ranges) { const out = []; let from = 0; for (const r0 of ranges) { let r = { ...r0 }; // Widen left, a word at a time, until the span is found where it stands (it always is at worst at `from`). while (text.indexOf(text.slice(r.start, r.end), from) !== r.start && r.start > from) { // The start of the word before: back over the space, then over its letters. let a = r.start; while (a > from && /\s/.test(text[a - 1])) a -= 1; while (a > from && isWordChar(text[a - 1])) a -= 1; if (a === r.start) break; r = { start: a, end: r.end }; } const last = out.at(-1); if (last && r.start <= last.end) last.end = Math.max(last.end, r.end); else out.push(r); from = r.end; } return out; } /** * The excerpt (the strings) the ranges spell. * @param {string} text * @param {Array<{ start: number, end: number }>} ranges * @returns {string[]} */ export const spansOf = (text, ranges) => ranges.map((r) => text.slice(r.start, r.end)); /** * The ranges an excerpt stands at (post-item.mjs excerptRanges' rule), or null when a span is not found. * @param {string} text * @param {string[]} spans */ export function rangesOf(text, spans) { const out = []; let from = 0; for (const sp of spans) { const at = text.indexOf(sp, from); if (at < 0) return null; out.push({ start: at, end: at + sp.length }); from = at + sp.length; } return out; } /** * The text cut into pieces for drawing: each `{ text, start, end, picked }`, a picked piece one range. * @param {string} text * @param {Array<{ start: number, end: number }>} ranges */ export function piecesOf(text, ranges) { const out = []; let at = 0; for (const r of ranges) { if (r.start > at) out.push({ text: text.slice(at, r.start), start: at, end: r.start, picked: false }); out.push({ text: text.slice(r.start, r.end), start: r.start, end: r.end, picked: true }); at = r.end; } if (at < text.length) out.push({ text: text.slice(at), start: at, end: text.length, picked: false }); return out; }