// Language-agnostic operator's-eye command highlighter, ported from // daemon-sec's terminal-highlight. Emits an HTML string (escaped) so it can be // used both server-side (Astro `set:html`) and in the client island — one // implementation, no drift. export const escapeHtml = (s: string) => String(s) .replace(/&/g, '&') .replace(//g, '>') .replace(/"/g, '"'); const TOKENISER = /'[^']*'|"[^"]*"|\S+|\s+/g; export function highlightCommand(source: string): string { return String(source) .split('\n') .map((line) => { if (/^\s*#/.test(line)) return `${escapeHtml(line)}`; let firstWord = true; let out = ''; const toks = line.match(TOKENISER) || []; for (const tok of toks) { if (/^\s+$/.test(tok)) { out += escapeHtml(tok); continue; } let cls = 'tk-base'; if (tok.startsWith('#')) cls = 'tk-muted'; else if (/REDACTED|PLACEHOLDER/.test(tok)) cls = 'tk-warn'; else if (/^(SQL>|PS>|>|\$|#)$/.test(tok)) cls = 'tk-kw'; else if (tok.startsWith('-')) cls = 'tk-flag'; else if (tok.startsWith("'") || tok.startsWith('"')) cls = 'tk-str'; else if (/^[\d.]+$/.test(tok) || /^\d{1,3}(\.\d{1,3}){3}/.test(tok)) cls = 'tk-num'; else if (/^(\||&&|>|>>|\\|;|\|\|)$/.test(tok)) cls = 'tk-op'; else if (/^[a-z]+:\/\//i.test(tok)) cls = 'tk-num'; else if (firstWord) cls = 'tk-cmd'; out += `${escapeHtml(tok)}`; if (!/^\s+$/.test(tok)) firstWord = false; } return out; }) .join('\n'); }