Files
bip-kit/src/react/inline.tsx
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

62 lines
1.9 KiB
TypeScript

import type { ReactNode } from "react";
import type { Inline } from "../inline.js";
/**
* Href guard for rendered links. Blocks contained in a database (the Cat, a
* composer, an admin form) flow through the same renderer as committed files,
* so `javascript:` and friends must never become a clickable href — the link
* degrades to a plain span instead.
*/
const KNOWN_SAFE_SCHEME = /^(https?:|mailto:|tel:)/i;
const HAS_SCHEME = /^[a-z][a-z0-9+.-]*:/i;
export function safeHref(href: string): string | null {
const trimmed = href.trim();
if (KNOWN_SAFE_SCHEME.test(trimmed)) return trimmed;
if (!HAS_SCHEME.test(trimmed)) return trimmed; // relative, absolute path, #anchor
return null;
}
/** Render inline spans to React nodes (semantic bp-* classes only). */
export function renderInline(spans: Inline[]): ReactNode {
return spans.map((span, i) => {
switch (span.t) {
case "text":
return span.text;
case "strong":
return <strong key={i}>{renderInline(span.children)}</strong>;
case "em":
return <em key={i}>{renderInline(span.children)}</em>;
case "code":
return (
<code key={i} className="bp-inline-code">
{span.text}
</code>
);
case "link": {
const href = safeHref(span.href);
if (href === null) return <span key={i}>{renderInline(span.children)}</span>;
const external = /^https?:/i.test(href);
return (
<a
key={i}
className="bp-link"
href={href}
rel={external ? "noopener noreferrer" : undefined}
>
{renderInline(span.children)}
</a>
);
}
case "footnoteRef":
return (
<sup key={i} className="bp-fnref">
<a href={`#fn-${span.id}`} id={`fnref-${span.id}`} aria-label={`Footnote ${span.id}`}>
{span.id}
</a>
</sup>
);
}
});
}