import {AnsiUp} from 'ansi_up'; const replacements = [ [/\x1b\[\d+[A-H]/g, ''], // Move cursor, treat them as no-op [/\x1bM/g, ''], // Move cursor one line up, threat them as no-op. [/\x1b\[\d?[JK]/g, '\r'], // Erase display/line, treat them as a Carriage Return [/\x1b\]9;\d+.*?(\x07|\x1b\\)/g, ''], // ConEmu, treat them as no-op. ]; const ansi_up = new AnsiUp(); ansi_up.use_classes = true; // render ANSI to HTML export function renderAnsi(line) { // Reset ansi_up's state. Previous renders can influence the output of future renders, because ansi_up is stateful // and remembers things like unclosed opening tags for colors. Rather than creating a new AnsiUp, which calls the // expensive setup_palettes method, we reset the stateful members. // // Impacted members have been identified by analyzing the library for the state that is changed during `process_ansi`: // https://github.com/drudru/ansi_up/blob/07a4824757d4dfbb41236a4245a6ce37f21aeb91/ansi_up.ts#L599 and during // `ansi_to_html`: https://github.com/drudru/ansi_up/blob/07a4824757d4dfbb41236a4245a6ce37f21aeb91/ansi_up.ts#L563 ansi_up.bold = ansi_up.faint = ansi_up.italic = ansi_up.underline = false; ansi_up.fg = ansi_up.bg = null; ansi_up._buffer = ''; if (line.endsWith('\r\n')) { line = line.substring(0, line.length - 2); } else if (line.endsWith('\n')) { line = line.substring(0, line.length - 1); } if (line.includes('\x1b')) { for (const [regex, replacement] of replacements) { line = line.replace(regex, replacement); } } if (!line.includes('\r')) { return ansi_up.ansi_to_html(line); } // handle "\rReading...1%\rReading...5%\rReading...100%", // convert it into a multiple-line string: "Reading...1%\nReading...5%\nReading...100%" const lines = []; for (const part of line.split('\r')) { if (part === '') continue; const partHtml = ansi_up.ansi_to_html(part); if (partHtml !== '') { lines.push(partHtml); } } // the log message element is with "white-space: break-spaces;", so use "\n" to break lines return lines.join('\n'); } // link schemes ansi_up is allowed to emit; re-checked below because log output is // untrusted and a javascript: or data: href should not become clickable. const allowedLinkSchemes = new Set(['http:', 'https:']); // ansi_up renders OSC 8 hyperlink escape codes as tags with the href and text already // escaped, but without link attributes. add them here, and drop any link whose scheme is // not http(s) by replacing it with its text. the html is parsed in a detached