Use mapping

Signed-off-by: yubiuser <github@yubiuser.dev>
This commit is contained in:
yubiuser
2025-06-24 21:42:03 +02:00
parent 9ad85da4cf
commit 9618f327e4

View File

@@ -88,17 +88,25 @@ function parseLines(ta, str) {
// Track the number of opening spans
let spanCount = 0;
// Mapping of ANSI escape codes to their corresponding CSS class names.
const ansiMappings = {
"\": "text-bold", //COL_BOLD
"\": "log-gray", //COL_GRAY
"\": "log-red", //COL_RED
"\": "log-green", //COL_GREEN
"\": "log-yellow", //COL_YELLOW
"\": "log-blue", //COL_BLUE
"\": "log-purple", //COL_PURPLE
"\": "log-cyan" //COL_CYAN
};
// Replace ANSI escape codes with HTML tags and count opening spans
/* eslint-disable prettier/prettier */
line = line.replaceAll("", () => { spanCount++; return '<span class="text-bold">'; }); //COL_BOLD
line = line.replaceAll("", () => { spanCount++; return '<span class="log-gray">'; }); //COL_GRAY
line = line.replaceAll("", () => { spanCount++; return '<span class="log-red">'; }); //COL_RED
line = line.replaceAll("", () => { spanCount++; return '<span class="log-green">'; }); //COL_GREEN
line = line.replaceAll("", () => { spanCount++; return '<span class="log-yellow">'; }); //COL_YELLOW
line = line.replaceAll("", () => { spanCount++; return '<span class="log-blue">'; }); //COL_BLUE
line = line.replaceAll("", () => { spanCount++; return '<span class="log-purple">'; }); //COL_PURPLE
line = line.replaceAll("", () => { spanCount++; return '<span class="log-cyan">'; }); //COL_CYAN
/* eslint-enable prettier/prettier */
for (const [ansiCode, cssClass] of Object.entries(ansiMappings)) {
line = line.replaceAll(ansiCode, () => {
spanCount++;
return `<span class="${cssClass}">`;
});
}
// Replace [0m with the appropriate number of closing spans
line = line.replaceAll("", "</span>".repeat(spanCount)); //COL_NC