Files
archie/ui/src/lib/json-tokens.ts
T
sam 92effeb692 feat(webui): two-section quality gates, sweep the restating card prose
The quality gates card stacked its number and rows across the full row
width -- the rate pinned to the left edge, the counts to the right --
which reads as two places at once. The card now has two internal
sections, left to right: the rate, and the per-workflow rows that
explain it; the row collapses under 720px.

Also swept the restating descriptions: 'Work that passed its quality
gates' under Quality gates, 'Archie needs these before it can work on
its own' under Finish setting up, 'Next 30 days at the current rate'
under Token outlook, 'Last 50, newest first' under Live activity. A
title and a sentence that restate each other are noise; the table or
chart says the thing. CapturesCard's redaction note is a security fact,
not restatement, and stays. The unconfigured versions state is one
quiet line, not a card with a title, a description, and a sentence.
2026-09-23 15:29:28 +10:00

70 lines
2.2 KiB
TypeScript

/**
* JSON syntax highlighting, tokenised.
*
* Capture bodies and stored config documents are strings that are "valid JSON
* or not" — the reader's rule is: valid JSON renders with typed colour, and
* anything else renders plain, exactly as stored. There is no projection and
* no library: one pass with a JSON-aware scanner, every character of the
* source preserved verbatim in the output tokens.
*
* Keys and values are distinguished by what follows the string (a colon makes
* it a key), which is exactly how JSON itself decides.
*/
export type JsonTokenKind = "key" | "string" | "number" | "keyword" | "punct";
export interface JsonToken {
text: string;
kind: JsonTokenKind;
}
/** Matches one JSON token: a string (which may be a key), a number, a keyword. */
const SCANNER =
/("(?:\\.|[^"\\])*")(\s*:)?|(-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?)|(\btrue\b|\bfalse\b|\bnull\b)/g;
export function tokenizeJson(text: string): JsonToken[] | null {
if (!text.trim()) return null;
try {
JSON.parse(text);
} catch {
return null; // not JSON: the caller renders it plain
}
const tokens: JsonToken[] = [];
let last = 0;
for (const match of text.matchAll(SCANNER)) {
const [full, str, colon, num, keyword] = match;
const at = match.index ?? 0;
if (at > last) tokens.push({ text: text.slice(last, at), kind: "punct" });
if (str !== undefined) {
tokens.push({ text: str, kind: colon ? "key" : "string" });
if (colon) tokens.push({ text: colon, kind: "punct" });
last = at + full.length;
continue;
}
if (num !== undefined) {
tokens.push({ text: num, kind: "number" });
} else if (keyword !== undefined) {
tokens.push({ text: keyword, kind: "keyword" });
}
last = at + full.length;
}
if (last < text.length) tokens.push({ text: text.slice(last), kind: "punct" });
return tokens;
}
/** The token's colour class, from the design tokens only. */
export function tokenClass(kind: JsonTokenKind): string {
switch (kind) {
case "key":
return "text-primary";
case "string":
return "text-ok";
case "number":
return "text-warn";
case "keyword":
return "text-info";
default:
return "text-foreground";
}
}