Transform string with Minecraft color code to HTML
minecraft/decode-color-code-into-html
Generate pure html without css class
1const colors = new Map();
2
3colors.set("a", "#55ff55");
4colors.set("b", "#55ffff");
5colors.set("c", "#ff5555");
6colors.set("d", "#ff55ff");
7colors.set("e", "#ffff55");
8colors.set("f", "#ffffff");
9colors.set("g", "#ddd605");
10colors.set("0", "#000000");
11colors.set("1", "#0000aa");
12colors.set("2", "#00aa00");
13colors.set("3", "#00aaaa");
14colors.set("4", "#aa0000");
15colors.set("5", "#aa00aa");
16colors.set("6", "#ffaa00");
17colors.set("7", "#aaaaaa");
18colors.set("8", "#555555");
19colors.set("9", "#5555ff");
20
21export function convertMinecraftColorToSpan(text: string) {
22    if (!text) return text;
23
24    let result = "";
25    let color = "currentColor";
26    let bold = false;
27    let italic = false;
28    let underline = false;
29    let strikethrough = false;
30    let obfuscated = false;
31
32    let currentSpan = "";
33
34    for (let i = 0; i < text.length; i++) {
35        // Optional & support
36        if (text[i] == "§" || text[i] == "&") {
37            if (i + 1 < text.length) {
38                let c = text[i + 1];
39
40                if (currentSpan) {
41                    result += `<span style="color: ${color};`
42                    if (bold) result += "font-weight: bold;"
43                    if (italic) result += "font-style: italic;"
44                    if (underline) result += "text-decoration: underline;"
45                    if (strikethrough) result += "text-decoration: line-through;"
46                    if (obfuscated) result += "text-shadow: 0 0 0.5em white;"
47                    result += `">${currentSpan}</span>`;
48                    
49                    currentSpan = "";
50                }
51
52                if (c == "r") {
53                    color = "currentColor";
54                    bold = false;
55                    italic = false;
56                    underline = false;
57                    strikethrough = false;
58                    obfuscated = false;
59                } else if (c == "l") {
60                    bold = true;
61                } else if (c == "o") {
62                    italic = true;
63                } else if (c == "n") {
64                    underline = true;
65                } else if (c == "m") {
66                    strikethrough = true;
67                } else if (c == "k") {
68                    obfuscated = true;
69                } else if (colors.has(c)) {
70                    const newColor = colors.get(c);
71                    color = newColor;
72                }
73                i++;
74            }
75        } else if (text[i] == "\n") {
76            currentSpan += "<br>";
77        } else {
78            currentSpan += text[i];
79        }
80    }
81
82    if (currentSpan) {
83        result += `<span style="color: ${color};`
84        if (bold) result += "font-weight: bold;"
85        if (italic) result += "font-style: italic;"
86        if (underline) result += "text-decoration: underline;"
87        if (strikethrough) result += "text-decoration: line-through;"
88        if (obfuscated) result += "text-shadow: 0 0 0.5em white;"
89        result += `">${currentSpan}</span>`;
90    }
91
92    return result;
93}
Unauthorized reproduction of original content on this website is prohibited.