mirror of
https://github.com/pi-hole/web.git
synced 2025-12-24 12:48:29 +00:00
Processs line squentially to always close the right number of <span> once we hit {COL_NC}
Signed-off-by: yubiuser <github@yubiuser.dev>
This commit is contained in:
@@ -90,26 +90,39 @@ function parseLines(ta, str) {
|
||||
|
||||
// Mapping of ANSI escape codes to their corresponding CSS class names.
|
||||
const ansiMappings = {
|
||||
"\[1m": "text-bold", //COL_BOLD
|
||||
"\[90m": "log-gray", //COL_GRAY
|
||||
"\[91m": "log-red", //COL_RED
|
||||
"\[32m": "log-green", //COL_GREEN
|
||||
"\[33m": "log-yellow", //COL_YELLOW
|
||||
"\[94m": "log-blue", //COL_BLUE
|
||||
"\[95m": "log-purple", //COL_PURPLE
|
||||
"\[96m": "log-cyan" //COL_CYAN
|
||||
"\u001B[1m": "text-bold", //COL_BOLD
|
||||
"\u001B[90m": "log-gray", //COL_GRAY
|
||||
"\u001B[91m": "log-red", //COL_RED
|
||||
"\u001B[32m": "log-green", //COL_GREEN
|
||||
"\u001B[33m": "log-yellow", //COL_YELLOW
|
||||
"\u001B[94m": "log-blue", //COL_BLUE
|
||||
"\u001B[95m": "log-purple", //COL_PURPLE
|
||||
"\u001B[96m": "log-cyan", //COL_CYAN
|
||||
};
|
||||
|
||||
// Replace ANSI escape codes with HTML tags and count opening spans
|
||||
for (const [ansiCode, cssClass] of Object.entries(ansiMappings)) {
|
||||
line = line.replaceAll(ansiCode, () => {
|
||||
spanCount++;
|
||||
return `<span class="${cssClass}">`;
|
||||
});
|
||||
}
|
||||
// Create a regex that matches all ANSI codes (including reset)
|
||||
/* eslint-disable-next-line no-control-regex */
|
||||
const ansiRegex = /(\u001B\[(?:1|90|91|32|33|94|95|96|0)m)/g;
|
||||
|
||||
// Replace [0m with the appropriate number of closing spans
|
||||
line = line.replaceAll("[0m", "</span>".repeat(spanCount)); //COL_NC
|
||||
// Process the line sequentially, replacing ANSI codes with their corresponding HTML spans
|
||||
// we use a counter to keep track of how many spans are open and close the correct number of spans when we encounter a reset code
|
||||
/* eslint-disable-next-line unicorn/prefer-string-replace-all */
|
||||
line = line.replace(ansiRegex, match => {
|
||||
if (match === "\u001B[0m") {
|
||||
// Reset/close all open spans
|
||||
const closingTags = "</span>".repeat(spanCount);
|
||||
spanCount = 0;
|
||||
return closingTags;
|
||||
}
|
||||
|
||||
if (ansiMappings[match]) {
|
||||
// Opening span
|
||||
spanCount++;
|
||||
return `<span class="${ansiMappings[match]}">`;
|
||||
}
|
||||
|
||||
return match; // Return unchanged if not recognized
|
||||
});
|
||||
|
||||
// Append the new text to the end of the output
|
||||
ta.append(line);
|
||||
|
||||
Reference in New Issue
Block a user