Files
bip-kit/test/parse-content.test.js
T
CatoandClaude Fable 5 a711d1834e feat: v0.2 — inline model, extended block vocabulary, react reference renderer (#18)
One vocabulary + one reference renderer so every fleet site reads long-form
at the same standard, while typography tokens stay per-site.

Parser (root export, still zero-dep; the v0.1 ten block types unchanged):
- Inline model: parseInline → text/strong/em/code/link/footnoteRef spans;
  every text-bearing block keeps `text` and gains additive `spans`.
- New blocks: h4, hr, figure (caption title), gallery (adjacent images
  merge), callout (GitHub > [!NOTE] syntax, nestable), pullquote (>> with
  — cite), mermaid, chart (tiny declarative spec; malformed specs THROW),
  math ($$), footnote defs, stats fence.
- Headings gain slugified, de-duplicated, umlaut-aware ids (kivvi's scheme).
- parseFrontmatter learns YAML arrays (inline + block lists), still dep-free.
- Helpers: extractToc, readingTime, slugify/createSlugger, parseChartSpec.

Renderer (new bip-kit/react subpath + bip-kit/styles.css):
- ArticleBody: async RSC that pre-awaits shiki/katex, then emits a fully
  synchronous tree (renderToString-safe). Server components for every block;
  client islands: Toc (scroll-spy, hidden under 3 headings), ReadingProgress,
  Lightbox, CopyButton. MermaidBlock on its own bip-kit/react/mermaid subpath
  so the mermaid peer never enters uninterested bundles (components override).
- shiki/katex are optional peers loaded via bundler-invisible dynamic import;
  honest degradation without them (mono <pre>, TeX source with data-math).
- styles.css: complete neutral theme — 70ch measure, real vertical rhythm,
  dark mode via vars only (data-theme wins over prefers-color-scheme),
  mobile-first, print-safe; every color/radius/font is a --bp-* custom
  property (theming contract documented in the README).

CI: ci.yml gains workflow_dispatch (re-arm strand — a GITHUB_TOKEN merge
cannot trigger workflows on its own; threadkit#28 pattern).

96 tests: v0.1 compat goldens (original shapes byte-identical after
stripping additive fields), goldens for every new syntax form and edge case,
renderToString smoke tests for every block type with and without the
optional peers (BIPKIT_DISABLE_PEERS seam), consumer-install pack test now
probing ./react, ./react/mermaid and ./styles.css.


Claude-Session: https://claude.ai/code/session_01WqKqMnHQHSmkGFfc5t7Rxn

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-09-05 16:03:53 +00:00

169 lines
6.1 KiB
JavaScript

import { test } from "node:test";
import assert from "node:assert/strict";
import { parseContentBlocks, parseFrontmatter } from "../dist/index.js";
// ── parseContentBlocks: v0.1 compatibility goldens ──────────────────────────
//
// v0.2 ADDS fields (heading `id`, inline `spans`/`itemSpans`) but never
// changes the v0.1 fields. These goldens assert exactly that: after dropping
// the additive fields, the original shapes are byte-identical. The additive
// fields themselves are asserted in parse-content-v02.test.js.
const strip = (blocks) =>
blocks.map((block) => {
const rest = { ...block };
delete rest.spans;
delete rest.itemSpans;
delete rest.id;
return rest;
});
const parseStripped = (body) => strip(parseContentBlocks(body));
test("headings: ## and ### become h2/h3, text trimmed", () => {
const blocks = parseStripped("## Roadmap \n### Q3 ");
assert.deepEqual(blocks, [
{ type: "h2", text: "Roadmap" },
{ type: "h3", text: "Q3" },
]);
});
test("consecutive plain lines join into one paragraph", () => {
const blocks = parseStripped("First line\nsecond line\n\nNew paragraph");
assert.deepEqual(blocks, [
{ type: "p", text: "First line second line" },
{ type: "p", text: "New paragraph" },
]);
});
test("CRLF input parses the same as LF", () => {
assert.deepEqual(
parseContentBlocks("## Title\r\n\r\nBody\r\n"),
parseContentBlocks("## Title\n\nBody\n"),
);
});
test("unordered list collects consecutive - items", () => {
const blocks = parseStripped("- one\n- two\n\n- three");
assert.deepEqual(blocks, [
{ type: "ul", items: ["one", "two"] },
{ type: "ul", items: ["three"] },
]);
});
test("ordered list collects consecutive numbered items", () => {
const blocks = parseStripped("1. first\n2. second\n10. tenth");
assert.deepEqual(blocks, [{ type: "ol", items: ["first", "second", "tenth"] }]);
});
test("blockquote keeps one entry per quoted line", () => {
const blocks = parseStripped("> line one\n> line two");
assert.deepEqual(blocks, [{ type: "blockquote", text: ["line one", "line two"] }]);
});
test("fenced code keeps language, inner newlines, and indentation", () => {
const blocks = parseContentBlocks("```ts\nconst a = 1;\n indented();\n```");
assert.deepEqual(blocks, [{ type: "code", lang: "ts", text: "const a = 1;\n indented();" }]);
});
test("mermaid fences are first-class mermaid blocks since v0.2", () => {
const [block] = parseContentBlocks("```mermaid\ngraph TD; A-->B\n```");
assert.deepEqual(block, { type: "mermaid", code: "graph TD; A-->B" });
});
test("an unterminated fence consumes to end of input without crashing", () => {
const blocks = parseContentBlocks("```\nno closing fence");
assert.deepEqual(blocks, [{ type: "code", lang: "", text: "no closing fence" }]);
});
test("GFM table needs a separator row; cells are trimmed", () => {
const blocks = parseContentBlocks("| A | B |\n|---|:--|\n| 1 | 2 |\n| 3 | 4 |");
assert.deepEqual(blocks, [
{
type: "table",
headers: ["A", "B"],
rows: [
["1", "2"],
["3", "4"],
],
},
]);
});
test("a pipe line without a separator row is a paragraph, not a table", () => {
const blocks = parseStripped("| just | text |");
assert.deepEqual(blocks, [{ type: "p", text: "| just | text |" }]);
});
test("standalone image line becomes an image block", () => {
const blocks = parseContentBlocks("![alt text](/img/shot.png)");
assert.deepEqual(blocks, [{ type: "image", alt: "alt text", src: "/img/shot.png" }]);
});
test("lone YouTube and Vimeo URLs become embed blocks", () => {
for (const url of [
"https://www.youtube.com/watch?v=dQw4w9WgXcQ",
"https://youtu.be/dQw4w9WgXcQ",
"https://vimeo.com/76979871",
]) {
assert.deepEqual(parseContentBlocks(url), [{ type: "embed", url }], url);
}
});
test("URLs from non-allowlisted hosts stay paragraphs", () => {
const blocks = parseStripped("https://evil.example.com/watch?v=abc");
assert.deepEqual(blocks, [{ type: "p", text: "https://evil.example.com/watch?v=abc" }]);
});
test("a URL inside prose does not split the paragraph", () => {
const blocks = parseStripped("Watch https://youtu.be/dQw4w9WgXcQ for context");
assert.deepEqual(blocks, [{ type: "p", text: "Watch https://youtu.be/dQw4w9WgXcQ for context" }]);
});
test("a paragraph ends where the next block type starts, without a blank line", () => {
const blocks = parseStripped("Some text\n- item");
assert.deepEqual(blocks, [
{ type: "p", text: "Some text" },
{ type: "ul", items: ["item"] },
]);
});
test("empty and whitespace-only input produce no blocks", () => {
assert.deepEqual(parseContentBlocks(""), []);
assert.deepEqual(parseContentBlocks("\n \n\t\n"), []);
});
// ── parseFrontmatter ────────────────────────────────────────────────────────
test("frontmatter is split from the body and parsed as key: value", () => {
const raw = "---\ntitle: Hello\ntags: a, b\n---\nBody here";
const { meta, body } = parseFrontmatter(raw);
assert.deepEqual(meta, { title: "Hello", tags: "a, b" });
assert.equal(body, "Body here");
});
test("matching quotes are stripped; mismatched quotes are kept", () => {
const { meta } = parseFrontmatter("---\na: \"quoted\"\nb: 'single'\nc: \"mismatched'\n---\n");
assert.equal(meta.a, "quoted");
assert.equal(meta.b, "single");
assert.equal(meta.c, "\"mismatched'");
});
test("values keep their own colons (URLs survive)", () => {
const { meta } = parseFrontmatter("---\nlink: https://example.com/x\n---\n");
assert.equal(meta.link, "https://example.com/x");
});
test("input without frontmatter is all body", () => {
const { meta, body } = parseFrontmatter("Just a body");
assert.deepEqual(meta, {});
assert.equal(body, "Just a body");
});
test("an unterminated frontmatter fence is treated as body, not swallowed", () => {
const raw = "---\ntitle: dangling\nno closing fence";
const { meta, body } = parseFrontmatter(raw);
assert.deepEqual(meta, {});
assert.equal(body, raw);
});