Files
botsmann/lib/longform/parse.ts
T
CatoandClaude Opus 5 a6906851d5 refactor(content): one markdown pipeline — bip-kit typed blocks replace MDX and two GitHub content repos (#183)
The blog and the Knowledge Center each fetched their markdown from a
SEPARATE GitHub repository at request time (g-but/botsmann-blog-content,
g-but/botsmann-knowledge-content) and rendered it through next-mdx-remote
with a hand-rolled component map per surface. Two maps for one markdown
vocabulary, already drifted: the blog slugged headings with rehype-slug, the
guides with a different inline regex; the guides highlighted code, the blog
did not; each had its own Callout. The content itself lived where nobody
reviewed it, behind a GitHub handle rename that now answers only via a 301.

Both surfaces now read content/*.md from this repo and render through
bip-kit's typed-block parser and reference renderer:

- content/blog/*.md (5 posts) and content/knowledge/**/*.md (10 guides) are
  vendered verbatim from the two content repos — word-level parity verified
  across all 15 files, zero words lost. Post images moved to public/blog/.
- The three app/blog/<slug>/page.mdx files came along as drafts. They were
  never routes: `page.mdx` only serves as a page when pageExtensions includes
  mdx, and this app never configured that, so all three were dead files. They
  are committed with `published: false` rather than deleted.
- lib/longform/ is the one pipeline both routes call: a fence-aware
  normalizer (`# ` → `## `, `*`/`+` bullets → `-`, nested items flattened —
  the guides carry 74 indented items and ~130 shell-comment `#` lines inside
  fences that a naive normalizer would have corrupted), then parse → blocks +
  TOC + reading time.
- setHighlighterLoader(() => import('shiki')) is registered at module scope.
  This app builds `output: standalone`, where bip-kit's zero-config shiki
  import is invisible to Next's file tracer — without the explicit literal,
  production ships unhighlighted while dev looks right.
- The TOC is derived from the same parse that renders the page, so a TOC
  anchor can no longer point at a heading id built by a different slug rule.
  types/knowledge.ts loses TableOfContentsItem accordingly.

Retired: next-mdx-remote, remark-gfm, rehype-slug, rehype-autolink-headings,
gray-matter (deps), components/blog/{Server,Client}MDXContent + MDXComponents,
components/knowledge/{Callout,CodeBlock,TableOfContents}. next.config drops
the raw.githubusercontent image pattern and the GitHub hosts from CSP
connect-src, and points frame-src at the privacy video players bip-kit uses.

Article typography now flows from the token SSOT: lib/longform/longform.css
maps globals.css onto bip-kit's --bp-* contract, replacing ~40 hardcoded
Tailwind greys. The mapping covers every --bp-* colour and pins the article
light, because Botsmann has no dark theme anywhere (zero `dark:` utilities,
no theme provider, light-only chrome) — letting bip-kit's dark defaults
through would put a dark article inside a white page. When the site grows a
dark tier, this file follows it by changing values in globals.css only.

tests/__tests__/lib/longform-content.test.ts walks every content file through
the real pipeline: committed content is bip-kit's trust boundary and a
malformed chart/stats fence throws at parse time, which without a test would
surface as a 500 on the live page.


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

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-07 14:24:40 +00:00

75 lines
2.6 KiB
TypeScript

/**
* The ONE long-form markdown pipeline. Both surfaces — the blog
* (content/blog/*.md) and the Knowledge Center (content/knowledge/**\/*.md) —
* parse through bip-kit's typed-block parser and render through its reference
* renderer (lib/longform/LongformBody).
*
* Typed blocks are the security model: markdown becomes a discriminated
* union and the renderer emits React elements from typed data, so there is no
* HTML/JSX passthrough for content to hide in. That is what let the MDX stack
* (next-mdx-remote + remark/rehype + a hand-rolled component map per surface)
* go away — an MDX file could evaluate arbitrary JSX; a markdown file cannot.
*/
import { extractToc, parseContentBlocks, readingTime } from 'bip-kit';
import type { ContentBlock, TocEntry } from 'bip-kit';
/**
* Normalize author-flavored markdown to bip-kit's deliberately scoped
* vocabulary, without changing meaning:
*
* - `* item` / `+ item` bullets → `- item` (bip-kit only parses `- `)
* - indented list items are flattened to top-level (the parser has no
* nesting; a flat item preserves the text, an unparsed one would merge
* into the previous paragraph — 74 such lines live in the guides)
* - `# Heading` → `## Heading` (every surface renders the title from
* frontmatter as the page's single h1; a body h1 is outside the
* vocabulary and would render as a literal `# …` paragraph)
*
* Code fences are left untouched — a `# comment` inside a ``` fence is a
* shell comment, not a heading, and the guides are full of them.
*/
export function normalizeLongformMarkdown(markdown: string): string {
const lines = markdown.replace(/\r\n/g, '\n').split('\n');
let inFence = false;
const out = lines.map((line) => {
if (line.trimStart().startsWith('```')) {
inFence = !inFence;
return line;
}
if (inFence) {
return line;
}
if (/^# (?!#)/.test(line)) {
return `#${line}`;
}
const bullet = line.match(/^(\s*)[*+] (.*)$/);
if (bullet) {
return `- ${bullet[2]}`;
}
const nested = line.match(/^\s+- (.*)$/);
if (nested) {
return `- ${nested[1]}`;
}
return line;
});
return out.join('\n');
}
export interface ParsedLongform {
blocks: ContentBlock[];
toc: TocEntry[];
/** Word-count based minutes (200 wpm, min 1). */
readingMinutes: number;
}
/** Parse a long-form markdown body into bip-kit typed blocks + TOC. */
export function parseLongform(markdown: string): ParsedLongform {
const blocks = parseContentBlocks(normalizeLongformMarkdown(markdown));
return {
blocks,
toc: extractToc(blocks),
readingMinutes: readingTime(blocks).minutes,
};
}