Files
CatoandClaude Opus 5.5 0f3f481a3b feat: charts in articles and suggestions the writer accepts or rejects (0.3.0) (#14)
* 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>
2026-11-15 02:26:30 +01:00

92 lines
3.8 KiB
JavaScript

import { test } from 'node:test';
import assert from 'node:assert/strict';
import {
CHART_TEMPLATE,
chartBlock,
chartTableMarkdown,
chartValuesNotInText,
chartsToTables,
drawnKind,
parseChartBlock,
splitChartBlocks,
} from '@bitbaum/writingkit';
const inner = (fence) => fence.split('\n').slice(1, -1).join('\n');
test('the block the Chart tool inserts parses', () => {
const parsed = parseChartBlock(inner(CHART_TEMPLATE));
assert.equal(parsed.ok, true);
assert.equal(parsed.chart.kind, 'bar');
assert.deepEqual(parsed.chart.series[0].points[1], ['2025', 14]);
});
test('CSV with key lines: several series, empty cells are gaps, not zero', () => {
const parsed = parseChartBlock(
'title: Members by year\nkind: line\nunit: people\nsource: Annual report\nyear,Zurich,Basel\n2024,120,80\n2025,"1,180",\n',
);
assert.equal(parsed.ok, true);
const { chart } = parsed;
assert.equal(chart.kind, 'line');
assert.equal(chart.unit, 'people');
assert.deepEqual(chart.series.map((s) => s.name), ['Zurich', 'Basel']);
assert.deepEqual(chart.series[0].points[1], ['2025', 1180]);
assert.deepEqual(chart.series[1].points[1], ['2025', null]);
});
test('problems are said in words', () => {
for (const [source, re] of [
['', /empty/],
['{ "title": "x", "series": [ }', /JSON/],
['{ "series": [{ "name": "a", "points": [["x", 1]] }] }', /title/],
['{ "kind": "pie", "title": "x", "series": [{ "points": [["x", 1]] }] }', /Kind/],
['title: x\nyear,a\n2024,lots', /not a number/],
]) {
const parsed = parseChartBlock(source);
assert.equal(parsed.ok, false, source);
assert.match(parsed.problem, re);
}
});
test('a line between places is drawn as columns; between years it stays a line', () => {
const places = parseChartBlock('title: t\nkind: line\ncity,n\nZurich,1\nBasel,2').chart;
const years = parseChartBlock('title: t\nkind: line\nyear,n\n2024,1\n2025,2').chart;
assert.equal(drawnKind(places), 'bar');
assert.equal(drawnKind(years), 'line');
});
test('without a drawing the chart is a table with its title and source', () => {
const { chart } = parseChartBlock('title: Votes | share\nsource: Office\nparty,share\nA,40\nB,');
const md = chartTableMarkdown(chart);
assert.match(md, /\*\*Votes \\\| share\*\*/);
assert.match(md, /\| A \| 40 \|/);
assert.match(md, /\| B \| — \|/);
assert.match(md, /Source: Office/);
});
test('split and fallback keep prose order; a broken block is left out for readers', () => {
const body = `Intro.\n\n${CHART_TEMPLATE}\n\nMiddle.\n\n\`\`\`chart\nnot a chart\n\`\`\`\n\nEnd.`;
const parts = splitChartBlocks(body);
assert.deepEqual(parts.map((p) => p.kind), ['markdown', 'chart', 'markdown', 'chart', 'markdown']);
assert.equal(parts[3].parsed.ok, false);
const tables = chartsToTables(body);
assert.match(tables, /^Intro\./);
assert.match(tables, /\| 2025 \| 14 \|/);
assert.doesNotMatch(tables, /not a chart/);
assert.match(tables, /End\.$/);
});
test('chartBlock round-trips, and a proposed chart may only draw the writer\'s numbers', () => {
const { chart } = parseChartBlock(inner(CHART_TEMPLATE));
assert.deepEqual(parseChartBlock(inner(chartBlock(chart))).chart, chart);
assert.deepEqual(chartValuesNotInText(chart, 'We had 10 in 2024, 14 in 2025 and 9 now.'), []);
assert.deepEqual(chartValuesNotInText(chart, 'We had 10, then 1,400.'), [14, 9]);
});
test("bip-kit's line format draws the same chart", () => {
const parsed = parseChartBlock('kind: line\ntitle: Weekly signups\nylabel: signups\nseries Organic: Jan=12, Feb=30\nseries Paid: Jan=4, Feb=9');
assert.equal(parsed.ok, true);
assert.equal(parsed.chart.unit, 'signups');
assert.deepEqual(parsed.chart.series[1], { name: 'Paid', points: [['Jan', 4], ['Feb', 9]] });
assert.equal(parseChartBlock('title: t\nseries A: Jan=x').ok, false);
});