Skip to content

Commit 9a87f2f

Browse files
committed
fix bugs and support wide unicode
1 parent d8fb641 commit 9a87f2f

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

src/lib/text/general.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,21 @@ export function isMarkType(c: JSONContent, type: string) {
5151
}
5252

5353
export function unescapeUnicode(str: string) {
54-
const matches = str.match(/(\\u[0-9a-f]{4})/gi);
54+
const regex = /\\u(?:([0-9a-fA-F]{4})|\{([0-9a-fA-F]+)\})/g;
5555

56-
if (!matches) {
57-
return str
58-
}
56+
return str.replace(regex, (match, p1, p2) => {
57+
// p1 will contain the 4-digit hex if it's \uXXXX
58+
// p2 will contain the variable hex if it's \u{XXXXX}
59+
const hex = p1 || p2; // Get the hex value from whichever group matched
5960

60-
return str.replaceAll(/(\\u[0-9a-f]{4})/gi, decodeURIComponent(JSON.parse(`"${matches![0]}"`)));
61+
if (hex) {
62+
const codePoint = parseInt(hex, 16);
63+
return String.fromCodePoint(codePoint);
64+
}
65+
// If for some reason no hex was captured (shouldn't happen with this regex),
66+
// return the original match to avoid breaking the string.
67+
return match;
68+
});
6169
}
6270

6371
/**

src/lib/text/motd.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { JSONContent } from "@tiptap/core";
2-
import { colorMap, defaultColorLUT, trueMarkOrUndefined } from "./general";
2+
import { colorMap, defaultColorLUT, trueMarkOrUndefined, unescapeUnicode } from "./general";
33

44
function hexToRgb(hex: string): [number, number, number] {
55
hex = hex.replace(/^#/, "");
@@ -76,7 +76,7 @@ export function translateMOTD(c: JSONContent) {
7676
let lowestDEVal = "";
7777
let formatting = ""
7878

79-
const color = defaultColorLUT(c.marks?.at(0)?.attrs?.color);
79+
const color = c.marks?.at(0)?.attrs?.color;
8080
for (const c of colorMap) {
8181
if (!color) {
8282
continue;
@@ -99,7 +99,7 @@ export function translateMOTD(c: JSONContent) {
9999
}
100100
}
101101

102-
data += `${char}r${lowestDEVal}${formatting}${c.text}`
102+
data += `${char}r${lowestDEVal}${formatting}${unescapeUnicode(c.text)}`
103103
}
104104
if (i < paragraphs.length - 1) data += "\\n";
105105
}

0 commit comments

Comments
 (0)