|
| 1 | +/** |
| 2 | + * Syntax highlighting for the five snippets on the page, and nothing more. |
| 3 | + * |
| 4 | + * Hand-rolled rather than a library: the page ships three Swift snippets, one |
| 5 | + * note block and one shell line, and a general-purpose highlighter would be |
| 6 | + * the largest thing in the bundle by a wide margin for the sake of maybe forty |
| 7 | + * tokens. The grammars below are tuned to exactly what `copy.ts` contains; a |
| 8 | + * construct they do not know falls through as plain text, never as a wrong |
| 9 | + * colour. |
| 10 | + * |
| 11 | + * The one invariant worth a test: concatenating the tokens' text gives back |
| 12 | + * the input byte for byte. Highlighting is paint, not content — the copy |
| 13 | + * button and the accessibility tree both see the original string. |
| 14 | + */ |
| 15 | + |
| 16 | +export type TokenKind = "plain" | "keyword" | "type" | "string" | "comment" | "heading" | "field"; |
| 17 | + |
| 18 | +export type Token = { kind: TokenKind; text: string }; |
| 19 | + |
| 20 | +type Rule = { kind: TokenKind; re: RegExp }; |
| 21 | + |
| 22 | +/** |
| 23 | + * Swift. Keywords and compiler directives take the accent; capitalised |
| 24 | + * identifiers read as types (which on this page they all are: modules, |
| 25 | + * namespaces, views, sinks); strings and comments get their own inks. |
| 26 | + */ |
| 27 | +const swift: Rule[] = [ |
| 28 | + { kind: "comment", re: /\/\/[^\n]*/y }, |
| 29 | + { kind: "string", re: /"(?:[^"\\]|\\.)*"/y }, |
| 30 | + { kind: "keyword", re: /#(?:if|endif|else|elseif)\b|\b(?:import|let|var|func|struct|return|some|true|false|nil)\b/y }, |
| 31 | + // A type has a lowercase letter in it; an all-caps word (`DEBUG`) is a |
| 32 | + // compilation condition and stays plain, as it does in Xcode. |
| 33 | + { kind: "type", re: /\b[A-Z](?=[A-Za-z0-9_]*[a-z])[A-Za-z0-9_]*\b/y }, |
| 34 | + { kind: "plain", re: /[A-Za-z_][A-Za-z0-9_]*|\s+|./y }, |
| 35 | +]; |
| 36 | + |
| 37 | +/** |
| 38 | + * The note block. A heading line, then `**Field**: value` lines, then the |
| 39 | + * comment as prose. Quoted text inside a value (the element's label) reads as |
| 40 | + * a string, the selector after the dash in the heading reads as a type: it is |
| 41 | + * the identifier an agent greps for. |
| 42 | + */ |
| 43 | +const markdown: Rule[] = [ |
| 44 | + { kind: "heading", re: /^##[^\n]*/my }, |
| 45 | + { kind: "field", re: /^\*\*[^*\n]+\*\*:/my }, |
| 46 | + { kind: "string", re: /"(?:[^"\\]|\\.)*"/y }, |
| 47 | + { kind: "plain", re: /[^\n"*]+|./y }, |
| 48 | +]; |
| 49 | + |
| 50 | +/** One shell line: the command word is the keyword, its argument a path. */ |
| 51 | +const sh: Rule[] = [ |
| 52 | + { kind: "comment", re: /#[^\n]*/y }, |
| 53 | + { kind: "keyword", re: /^\s*[a-z][\w-]*/my }, |
| 54 | + { kind: "string", re: /\S*\//y }, |
| 55 | + { kind: "plain", re: /\S+|\s+/y }, |
| 56 | +]; |
| 57 | + |
| 58 | +const grammars: Record<string, Rule[]> = { swift, markdown, sh }; |
| 59 | + |
| 60 | +export function tokenize(code: string, language: string): Token[] { |
| 61 | + const rules = grammars[language]; |
| 62 | + if (!rules) return [{ kind: "plain", text: code }]; |
| 63 | + |
| 64 | + const out: Token[] = []; |
| 65 | + let i = 0; |
| 66 | + while (i < code.length) { |
| 67 | + let matched = false; |
| 68 | + for (const { kind, re } of rules) { |
| 69 | + re.lastIndex = i; |
| 70 | + const m = re.exec(code); |
| 71 | + if (m && m[0].length > 0) { |
| 72 | + push(out, kind, m[0]); |
| 73 | + i += m[0].length; |
| 74 | + matched = true; |
| 75 | + break; |
| 76 | + } |
| 77 | + } |
| 78 | + if (!matched) { |
| 79 | + // Unreachable while every grammar ends in a catch-all, but a silent |
| 80 | + // infinite loop is the worst possible failure, so advance regardless. |
| 81 | + push(out, "plain", code.charAt(i)); |
| 82 | + i += 1; |
| 83 | + } |
| 84 | + } |
| 85 | + return out; |
| 86 | +} |
| 87 | + |
| 88 | +/** Merge adjacent tokens of one kind so the DOM carries as few spans as the text allows. */ |
| 89 | +function push(out: Token[], kind: TokenKind, text: string) { |
| 90 | + const last = out[out.length - 1]; |
| 91 | + if (last && last.kind === kind) last.text += text; |
| 92 | + else out.push({ kind, text }); |
| 93 | +} |
0 commit comments