repositories

daemon-sec-lotl

DÆMONBins: GTFOBins × LOLBAS × WADComs × LOOBins in one filterable catalog

clonegit clone https://git.daemon-sec.xyz/daemon-sec-lotl.git
highlight.tsTypeScript41 lines1.6 KiBraw download
// 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, '&lt;')
    .replace(/>/g, '&gt;')
    .replace(/"/g, '&quot;');

const TOKENISER = /'[^']*'|"[^"]*"|\S+|\s+/g;

export function highlightCommand(source: string): string {
  return String(source)
    .split('\n')
    .map((line) => {
      if (/^\s*#/.test(line)) return `<span class="tk-muted">${escapeHtml(line)}</span>`;
      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 += `<span class="${cls}">${escapeHtml(tok)}</span>`;
        if (!/^\s+$/.test(tok)) firstWord = false;
      }
      return out;
    })
    .join('\n');
}