Files
atomic-design-poc/apps/ssp/src/app/showcase/highlight-ts.ts
T
ehoandClaude Sonnet 5 dd11eafe50 refactor: strip WP-/RB- ticket refs from apps and libs (RD-18)
204 WP-NN/RB-NN comments named a closed ticket instead of the code they
sit next to. git blame already records history and stays correct when
code moves; the comment does not. This sweep removes the reference and
keeps the sentence, across 95 files in apps/ and libs/ plus the
behaviour-spec generator's header text.

Eleven references stay: five story files justify an a11y disable per
the README's rule, and one line in a11y.mdx documents that convention.
Two sentences needed a rewrite, not a deletion, so the reference's
meaning survives its removal. behaviour-spec.mdx is regenerated, not
hand-edited.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-04 21:23:07 +02:00

50 lines
1.5 KiB
TypeScript

/**
* Tiny, dependency-free TS highlighter for the teaching showcase. Escapes HTML,
* then wraps line-comments, strings, and a fixed keyword set in `.c`/`.s`/`.k` spans (the
* classes `concepts.page` styles). Deliberately naive — good enough for the short, curated
* snippets shown here; not a real tokenizer. Input is always our OWN source (extracted by
* `scripts/gen-snippets.mjs` or authored inline), so the `[innerHTML]` sink is safe once
* the HTML metacharacters are escaped first. Pure.
*/
const KEYWORDS = [
'interface',
'type',
'export',
'import',
'from',
'const',
'let',
'return',
'function',
'switch',
'case',
'default',
'if',
'else',
'new',
'readonly',
'extends',
'as',
'void',
];
const escapeHtml = (s: string): string =>
s.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
export function highlightTs(code: string): string {
const kw = new RegExp(`\\b(${KEYWORDS.join('|')})\\b`, 'g');
return escapeHtml(code)
.split('\n')
.map((line) => {
// Line comment: everything from // to EOL is one comment span (skip the rest).
const c = line.indexOf('//');
const head = c === -1 ? line : line.slice(0, c);
const tail = c === -1 ? '' : `<span class="c">${line.slice(c)}</span>`;
const lit = head
.replace(/(['"`])(?:\\.|(?!\1).)*\1/g, (m) => `<span class="s">${m}</span>`) // strings
.replace(kw, '<span class="k">$1</span>'); // keywords
return lit + tail;
})
.join('\n');
}