fix(logs): rebuild legacy HTML safely

This commit is contained in:
2026-07-31 16:32:34 +00:00
parent 419d1b125d
commit 2e63be87c9
12 changed files with 438 additions and 119 deletions
+2 -54
View File
@@ -1,55 +1,3 @@
const logRegex = /<([RBGMCLSODYW]1?|1|\/)>/g;
import { formatLegacyLogHtml } from '@sammo-ts/common';
const convertMap: Record<string, string> = {
R: 'color: red;',
B: 'color: blue;',
G: 'color: green;',
M: 'color: magenta;',
C: 'color: cyan;',
L: 'color: limegreen;',
S: 'color: skyblue;',
O: 'color: orangered;',
D: 'color: orangered;',
Y: 'color: yellow;',
W: 'color: white;',
1: 'font-size: 0.9em;',
};
const convertMap2: Record<string, string> = {
1: 'font-size: 0.9em;',
};
export const formatLog = (text?: string): string => {
if (!text) {
return '';
}
let lastIndex = 0;
const result: string[] = [];
for (let match = logRegex.exec(text); match !== null; match = logRegex.exec(text)) {
const partAll = match[0];
const subPart = match[1];
const index = match.index;
if (lastIndex !== index) {
result.push(text.slice(lastIndex, index));
}
if (subPart === '/') {
result.push('</span>');
} else if (subPart.length === 2) {
result.push(`<span style="${convertMap[subPart[0]] ?? ''}${convertMap2[subPart[1]] ?? ''}">`);
} else {
result.push(`<span style="${convertMap[subPart] ?? ''}">`);
}
lastIndex = index + partAll.length;
}
if (lastIndex !== text.length) {
result.push(text.slice(lastIndex));
}
return result.join('');
};
export const formatLog = (text?: string): string => formatLegacyLogHtml(text);