|
1 | 1 | import type { JSONContent } from "@tiptap/core"; |
2 | | -import { colorMap, defaultColorLUT, trueMarkOrUndefined, unescapeUnicode } from "./general"; |
| 2 | +import { colorMap, trueMarkOrUndefined, unescapeUnicode } from "./general"; |
3 | 3 |
|
4 | 4 | function hexToRgb(hex: string): [number, number, number] { |
5 | 5 | hex = hex.replace(/^#/, ""); |
@@ -50,58 +50,57 @@ export function deltaE(hex1: string, hex2: string): number { |
50 | 50 | } |
51 | 51 |
|
52 | 52 | const formattingCodes = [ |
53 | | - {key: "obfuscated", value: "k"}, |
54 | | - {key: "bold", value: "l"}, |
55 | | - {key: "strike", value: "m"}, |
56 | | - {key: "underline", value: "n"}, |
57 | | - {key: "italic", value: "o"}, |
58 | | -] |
| 53 | + { key: "obfuscated", value: "k" }, |
| 54 | + { key: "bold", value: "l" }, |
| 55 | + { key: "strike", value: "m" }, |
| 56 | + { key: "underline", value: "n" }, |
| 57 | + { key: "italic", value: "o" }, |
| 58 | +]; |
59 | 59 |
|
60 | 60 | export function translateMOTD(c: JSONContent) { |
61 | 61 | const char = "\\u00a7"; |
62 | 62 | const paragraphs = c.content!; |
63 | 63 |
|
64 | | - let data = ""; |
| 64 | + let data = ""; |
65 | 65 |
|
66 | 66 | for (const [i, p] of paragraphs.entries()) { |
67 | | - const content = p.content ?? []; |
68 | | - |
69 | | - |
| 67 | + const content = p.content ?? []; |
| 68 | + |
70 | 69 | for (const c of content) { |
71 | | - if (!c.text) { |
72 | | - continue; |
73 | | - } |
| 70 | + if (!c.text) { |
| 71 | + continue; |
| 72 | + } |
74 | 73 |
|
75 | 74 | let lowestDE = 999; |
76 | | - let lowestDEVal = ""; |
77 | | - let formatting = "" |
| 75 | + let lowestDEVal = ""; |
| 76 | + let formatting = ""; |
78 | 77 |
|
79 | 78 | const color = c.marks?.at(0)?.attrs?.color; |
80 | | - for (const c of colorMap) { |
81 | | - if (!color) { |
82 | | - continue; |
83 | | - } |
84 | | - |
85 | | - const dE = deltaE(color, c.value); |
86 | | - if (dE === 0) { |
87 | | - lowestDEVal = `${char}${c.code}` |
88 | | - break; |
89 | | - } |
90 | | - if (dE < lowestDE) { |
91 | | - lowestDE = dE |
92 | | - lowestDEVal = `${char}${c.code}` |
93 | | - } |
94 | | - } |
95 | | - |
96 | | - for (const code of formattingCodes) { |
97 | | - if (trueMarkOrUndefined(c, code.key)) { |
98 | | - formatting += `${char}${code.value}` |
99 | | - } |
100 | | - } |
101 | | - |
102 | | - data += `${char}r${lowestDEVal}${formatting}${unescapeUnicode(c.text)}` |
103 | | - } |
| 79 | + for (const c of colorMap) { |
| 80 | + if (!color) { |
| 81 | + continue; |
| 82 | + } |
| 83 | + |
| 84 | + const dE = deltaE(color, c.value); |
| 85 | + if (dE === 0) { |
| 86 | + lowestDEVal = `${char}${c.code}`; |
| 87 | + break; |
| 88 | + } |
| 89 | + if (dE < lowestDE) { |
| 90 | + lowestDE = dE; |
| 91 | + lowestDEVal = `${char}${c.code}`; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + for (const code of formattingCodes) { |
| 96 | + if (trueMarkOrUndefined(c, code.key)) { |
| 97 | + formatting += `${char}${code.value}`; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + data += `${char}r${lowestDEVal}${formatting}${unescapeUnicode(c.text)}`; |
| 102 | + } |
104 | 103 | if (i < paragraphs.length - 1) data += "\\n"; |
105 | | - } |
106 | | - return data |
| 104 | + } |
| 105 | + return data; |
107 | 106 | } |
0 commit comments