Skip to content
Kevin Liu's WikiSearch

kevin-wiki / ui/scripts

Purpose

Build- and check-time scripts for the viewer: bundle the wiki/ markdown into a form the Next app serves, and enforce the wiki's quality invariants (inline citations, gate passwords) so broken content can't ship.

Mental model & key files

  • bundle-wiki.mjs — packages wiki content plus referenced public image/video assets for the app to render and emits the public-only static search index; private-page-only data and assets stay out of public deployment artifacts.
  • export-okf.mjs + lib/okf-export.mjs — deterministically project the same public corpus into an OKF v0.2 Markdown tree served at /api/okf.
  • enforce-citations.ts + lib/citation-scan.ts — fail the build when facts lack [Source: ...] citations; test-citations.ts covers the scanner.
  • test-gate-passwords.ts — validates gated-content password config.
  • test-privacy-gating.ts — locks regression cases for people, career, finance, Dedalus, generated packs, the mail and cloud tool pages, operational logs, and the public stable objects on /system.
  • test-public-search-index.ts — proves the CDN search artifact includes every public entry, excludes every private entry, and contains no privacy metadata.
  • test-public-graph.ts — proves every number in the anonymous graph view matches the links it ships, and that no private slug or title is in it.
  • test-public-counts.ts — proves home's page, category and tag counts equal what the sidebar shows an anonymous viewer.
  • test-public-build-output.ts + lib/privacy-scan.ts — scans the build output, or a running server with --base, for private slugs anywhere and private titles where the site prints a title, in seven encodings, and checks that a private slug answers exactly like a missing one. With --base it also scans the headers of every crawled response and of each redirect followed to it, name and value (a name also from each of its --joined words on), for slugs and for titles anywhere, as undici gives them and decoded as UTF-8, with each Link and Location URL resolved against the URL that answered; a finding names a header only from a fixed list (NAMED_HEADERS). It holds every document, from its bytes and Content-Type, and the twin and the 404s for unlocked viewers, to the font rules in lib/font-heads.ts. The document test errs red (lib/served-page.ts): on a navigation an answer is a document unless each value of its Content-Type, split and parsed as WHATWG does, is a type no browser renders as one (text, Markdown, JSON, CSS, JavaScript, a font, an image, a download, an event stream or an RSC payload); an XML type counts when its body names the XHTML namespace. An RSC answer is one only in an HTML type. The same test decides which URLs the crawl fetches again as RSC, and a page that answers 200 in a type that renders none is a finding: a sitemap URL but its files (SITEMAP_FILES), a fixed page (CRAWL_PAGES), or a public README or _index page. The unlocked views need the server's key: run it with the server's WIKI_GATE_SECRET, or pass --skip-unlocked-views to view those pages anonymously alone (a crawl of production); without either it exits 1. Each view must give its answer, by status and by whether the page carries the 404 body, found by NotFoundBody's heading (NOT_FOUND_SENTENCE in src/lib/constants.ts, as rendered HTML, since every route's RSC payload carries the 404 as data). An anonymous viewer gets the 404 for a missing page, pack or path and for the private page. An unlocked viewer gets the private page they unlocked, without the 404 body, and the 404 body at 200 from the twin for a missing page; for a missing pack too, but a viewer with one page unlocked gets the password gate for every pack name. A missing path is a 404 to every viewer. For the viewer with one page unlocked, a pack and a made-up pack name of its length (both the password gate), and a private page not unlocked and a made-up page in its folder and of its length (both the 404 body), must agree in the redirects followed to them (each status, and each Location normalized for its path with its _rsc value masked), status, headers and canonical body for GET, RSC and HEAD, as an anonymous viewer's pairs must; the normalizer reads Next's segment lists under /wiki/ and /packs/, the twin's /unlocked included. Without a local build (.next/server) it says so and holds crawled pages to every font rule but the 400 and 600 placement, which needs the build's routes. It sends a crawled URL as RSC only when it answers a document (Vercel answers an RSC request to a file with its 404 page), compares an HTML answer to an RSC request as HTML, and treats Vercel's x-vercel-id, x-vercel-cache and age as headers that change per request. At most 4 requests are in flight, redirects included (lib/held-fetch.ts): the crawl follows each hop itself, reads its body before the next opens, and drops the cookie and authorization headers on a hop to another origin. Each hop times out after 30 seconds, and a network error or timeout is retried twice. pnpm test:script-libs (prebuild) runs lib/*.test.ts: held-fetch.test.ts checks the hops against two stub servers, and served-page.test.ts the document test against a stub site whose pages differ only in Content-Type, and for XML in body.
  • test-font-heads.ts — pnpm test:font-heads (prebuild) runs the font rules' reader on hand-written documents and prints, per case, the verdict the check must give (red when any engine may fetch a font against the rules; "false red" and "React's shape" mark the red cases no engine misfetches) and the reader's: quoting, case, character references, NUL, comments, raw text, <noscript>, <template>, <svg> and <math>, the head's end, the error page's id, Link headers, byte order marks and labels, origins and crossorigin. It then reads malformed megabyte pages and Link headers with runs of 100,000 spaces or tabs within a time bound, and checks that a finding never prints an href or a tag name outside its fixed list.
  • test-font-output.ts + lib/font-heads.ts + lib/woff2-cmap.ts — postbuild check of the site fonts: file hashes, Geist Mono's cmap, list and range, no font hint in any RSC payload, the font rules on every prerendered page from its bytes, and no PP Mori core file in a client chunk (ui/SKILL.md, Fonts).
  • test-case-paths.ts — names public wiki paths that differ only in case before the build; a warning on a case-insensitive disk, a failure under vercel build (VERCEL=1), which would otherwise stop with EEXIST.
  • measure-diagrams.ts — pnpm measure:diagrams draws each public page's Mermaid diagrams headless (agent-browser) for both themes into src/generated/diagrams/<key>.light.svg and .dark.svg, which markdown.ts inlines, and their sizes into src/lib/diagram-sizes.json; rerun after a public diagram changes.
  • measure-tweets.ts — pnpm measure:tweets --base <url> [--missing] measures each public tweet's opened card on a running site at a 1440px, 430px and 360px screen (headless agent-browser) into src/lib/tweet-sizes.json.
  • check-embed-sizes.ts + lib/public-embeds.ts — pnpm check:embed-sizes (prebuild) warns about a public diagram with no drawing or size or a public tweet with no size (--fail exits 1), and fails on a size key or drawing no public page uses.
  • test-content-css.ts — postbuild: every class in a prerendered page's rendered Markdown (code and inline SVG aside) has a rule in the emitted CSS; a class only a page's raw HTML uses goes in globals.css's @source inline list (--list prints that set).
  • test-readme-title.ts — a folder page prints exactly one h1, whether or not its README opens with a comment.
  • check-ui-tokens.mjs — reports type and tracking classes outside the token scale in .tsx under src; --fail exits 1 on any.
  • test-vercel-upload.mjs + lib/trace-reads.mjs — runs the build scripts that read outside ui/, records every file they open, and fails when .vercelignore would leave one out of the upload. The build scripts write into its scratch folder (bundle-wiki.mjs --out), so it never writes ui/, and a run stopped by a signal stops its script and removes the folder.

Patterns to follow / invariants

  • ESM; .mjs for plain Node scripts, .ts run via tsx.
  • Keep scan/validation logic in lib/ (pure) and the entrypoints thin + testable.
  • A failing citation/gate check should block the build — these are guardrails.

Common tasks → first action

  • Re-bundle after wiki edits → run bundle-wiki.mjs, then pnpm build:public-links: the bundle deletes .content, and every anonymous article reads .content/public-links.json and .content/public-mentions.json.
  • Add a content guardrail → put the predicate in lib/, call it from an enforce-*.ts entry, add a test-*.ts.
  • Verify the portable agent bundle → pnpm test:okf and node scripts/export-okf.mjs --check.

Gotchas

  • These run in the ui/ pnpm workspace, not the repo-root tsx environment.
  • Citation enforcement mirrors wiki/ conventions — keep it in sync with conventions-quality if the citation format changes.