mirror of
https://github.com/bitbaum/writingkit.git
synced 2026-12-04 05:37:40 +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>
99 lines
3.9 KiB
TypeScript
99 lines
3.9 KiB
TypeScript
/**
|
||
* A chart inside an article: a fenced ` ```chart ` block holding a small spec
|
||
* the writer can read and edit by hand. Pure parsing and fallbacks; drawing is
|
||
* `@bitbaum/chartkit`'s (see `@bitbaum/writingkit/react/chart`).
|
||
*
|
||
* Two spellings, both short enough to type on a phone:
|
||
*
|
||
* JSON — the block the Chart tool inserts:
|
||
* { "kind": "bar", "title": "…", "unit": "CHF", "source": "…",
|
||
* "series": [{ "name": "Sales", "points": [["2024", 10], ["2025", 14]] }] }
|
||
*
|
||
* CSV — optional `key: value` lines, then a header row and data rows
|
||
* (bip-kit's `series Name: Jan=12, Feb=30` lines are read too):
|
||
* title: Members by year
|
||
* kind: line
|
||
* year,Members
|
||
* 2024,120
|
||
* 2025,180
|
||
*
|
||
* Kinds: `bar` (columns, in the order given — periods read left to right),
|
||
* `ranking` (horizontal bars, largest first), `line`, `area`. A line or area
|
||
* whose labels are not periods or numbers is drawn as columns: a line between
|
||
* "Zurich" and "Basel" means nothing.
|
||
*
|
||
* Anywhere a chart cannot be drawn (an RSS reader, a share card, a plain
|
||
* Markdown renderer) the same numbers become a table: `chartTableMarkdown`.
|
||
* A spec that does not parse is reported in words, never drawn half.
|
||
*/
|
||
export declare const CHART_FENCE = "chart";
|
||
export declare const CHART_KINDS: readonly ["bar", "ranking", "line", "area"];
|
||
export type ArticleChartKind = (typeof CHART_KINDS)[number];
|
||
export declare const CHART_LIMITS: {
|
||
readonly series: 8;
|
||
readonly points: 120;
|
||
readonly label: 40;
|
||
readonly title: 160;
|
||
readonly source: 200;
|
||
readonly unit: 16;
|
||
/** The whole block, before parsing. */
|
||
readonly block: 20000;
|
||
};
|
||
export interface ArticleChartSeries {
|
||
name: string;
|
||
/** `[label, value]`; `null` is "no figure", drawn as a gap or "—", never zero. */
|
||
points: [string, number | null][];
|
||
}
|
||
export interface ArticleChart {
|
||
kind: ArticleChartKind;
|
||
title: string;
|
||
unit?: string;
|
||
/** Where the numbers come from, said under the chart. */
|
||
source?: string;
|
||
series: ArticleChartSeries[];
|
||
}
|
||
export type ParsedChart = {
|
||
ok: true;
|
||
chart: ArticleChart;
|
||
} | {
|
||
ok: false;
|
||
problem: string;
|
||
};
|
||
/** Parse the text inside a ` ```chart ` fence. */
|
||
export declare function parseChartBlock(source: string): ParsedChart;
|
||
/** Periods (2025, 2025-Q2, 2025-03, 2025-03-31) or plain numbers: a line can join them. */
|
||
export declare function chartHasOrderedLabels(chart: ArticleChart): boolean;
|
||
/** The kind that will actually be drawn: a line over non-periods is columns. */
|
||
export declare function drawnKind(chart: ArticleChart): ArticleChartKind;
|
||
/** The numbers as rows: first column the label, one column per series. */
|
||
export declare function chartTable(chart: ArticleChart): {
|
||
head: string[];
|
||
rows: string[][];
|
||
};
|
||
/** A GFM table with the title above and the source under: the chart without a drawing. */
|
||
export declare function chartTableMarkdown(chart: ArticleChart): string;
|
||
export type ArticleSegment = {
|
||
kind: "markdown";
|
||
text: string;
|
||
} | {
|
||
kind: "chart";
|
||
source: string;
|
||
parsed: ParsedChart;
|
||
};
|
||
/** Split Markdown into prose and ` ```chart ` blocks, in order. */
|
||
export declare function splitChartBlocks(markdown: string): ArticleSegment[];
|
||
/**
|
||
* Markdown for a renderer that cannot draw: every chart block becomes its
|
||
* table; a block that does not parse is left out rather than shown as code.
|
||
*/
|
||
export declare function chartsToTables(markdown: string): string;
|
||
/** A chart as the fenced block to put in the text. */
|
||
export declare function chartBlock(chart: ArticleChart): string;
|
||
/** Every number written in a text, as numbers ("1,200" and "1’200" are 1200). */
|
||
export declare function numbersInText(text: string): number[];
|
||
/**
|
||
* The values of a chart that do not appear in the text. A chart proposed by a
|
||
* model must draw only the writer's own numbers: an empty list means it does.
|
||
*/
|
||
export declare function chartValuesNotInText(chart: ArticleChart, text: string): number[];
|