mirror of
https://github.com/bitbaum/writingkit.git
synced 2026-12-05 05:58:26 +01:00
* feat: charts in articles and suggestions the writer accepts or rejects (0.3.0) - ```chart blocks (JSON or CSV spec): parseChartBlock, splitChartBlocks, table fallbacks for RSS/share cards (chartsToTables), chartValuesNotInText so a proposed chart draws only the writer's own numbers - @bitbaum/writingkit/react/chart: ArticleChart / ArticleChartBlock drawn by @bitbaum/chartkit (new optional peer) - Suggestion, applySuggestion (refuses stale text), wordDiff, SuggestionCard - suggestMedia: a chart beside the paragraph with figures, else an illustration - ArticleComposer: renderChart in Preview; draft-from-brief is a suggestion Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019MDtmvT2nQyPBK4EfWf3Rn * feat(chart): read bip-kit's series-line format too Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019MDtmvT2nQyPBK4EfWf3Rn * fix(suggestions): show a rewrite whole; keep Publish the one primary action Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019MDtmvT2nQyPBK4EfWf3Rn --------- Co-authored-by: Claude <noreply@anthropic.com>
50 lines
2.0 KiB
JavaScript
50 lines
2.0 KiB
JavaScript
/**
|
||
* Where a picture or a chart would help a reader, read from the text alone.
|
||
* An article that works with numbers gets a chart proposed beside the
|
||
* paragraph that carries them; one that does not gets an illustration. This is
|
||
* the cheap, local first answer; a model may refine it, never replace the rule
|
||
* that a chart draws only numbers the writer wrote.
|
||
*/
|
||
import { numbersInText } from "./chart.js";
|
||
const YEAR = /^(1[89]|20)\d{2}$/;
|
||
/** The prose paragraphs of a Markdown body with their positions. */
|
||
export function proseParagraphs(body) {
|
||
const out = [];
|
||
const re = /(```|~~~)[\s\S]*?\1|[^\n]+(?:\n(?!\s*\n)[^\n]+)*/g;
|
||
let index = 0;
|
||
for (const match of body.matchAll(re)) {
|
||
const text = match[0];
|
||
if (/^(```|~~~)/.test(text))
|
||
continue;
|
||
const trimmed = text.trim();
|
||
if (!trimmed || /^#{1,6}\s/.test(trimmed) || /^!\[[^\]]*\]\([^)]*\)$/.test(trimmed))
|
||
continue;
|
||
const tokens = trimmed.match(/[+-]?\d[\d'’,]*(?:\.\d+)?%?/g) ?? [];
|
||
const figures = tokens.filter((t) => !YEAR.test(t)).length;
|
||
out.push({
|
||
index: index++,
|
||
end: (match.index ?? 0) + text.length,
|
||
text: trimmed,
|
||
figures,
|
||
});
|
||
}
|
||
return out;
|
||
}
|
||
/** Three figures in one paragraph is a comparison a reader would rather see. */
|
||
export const CHART_FIGURE_THRESHOLD = 3;
|
||
export function suggestMedia(body) {
|
||
const paragraphs = proseParagraphs(body);
|
||
const data = paragraphs
|
||
.filter((p) => p.figures >= CHART_FIGURE_THRESHOLD)
|
||
.sort((a, b) => b.figures - a.figures)[0];
|
||
if (data)
|
||
return { kind: "chart", paragraph: data };
|
||
// An illustration opens the piece best after its first paragraph.
|
||
return { kind: "illustration", paragraph: paragraphs[0] ?? null };
|
||
}
|
||
/** Whether the text holds enough numbers for a chart at all. */
|
||
export function hasChartableNumbers(body) {
|
||
return (numbersInText(body).length >= CHART_FIGURE_THRESHOLD &&
|
||
suggestMedia(body).kind === "chart");
|
||
}
|