Skip to content

Commit d8fb641

Browse files
committed
escaping and color bug fix
1 parent c00d706 commit d8fb641

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

src/lib/text/general.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export function defaultColorLUT(color: string): string | undefined {
2424
if (!color || color === "null") {
2525
return;
2626
}
27-
return colorMap.find((e) => e.value.toLowerCase() === color)?.name || color;
27+
return colorMap.find((e) => e.value.toUpperCase() === color)?.name || color;
2828
}
2929

3030
/**
@@ -50,6 +50,16 @@ export function isMarkType(c: JSONContent, type: string) {
5050
return c.marks?.find((e) => e.type === type);
5151
}
5252

53+
export function unescapeUnicode(str: string) {
54+
const matches = str.match(/(\\u[0-9a-f]{4})/gi);
55+
56+
if (!matches) {
57+
return str
58+
}
59+
60+
return str.replaceAll(/(\\u[0-9a-f]{4})/gi, decodeURIComponent(JSON.parse(`"${matches![0]}"`)));
61+
}
62+
5363
/**
5464
* Applies a gradient to a selection in an editor
5565
*

src/lib/text/nbt_or_json.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type {
66
TranslateOptions,
77
} from "$lib/types";
88
import { type JSONContent } from "@tiptap/core";
9-
import { colorMap, defaultColorLUT, isMarkType, trueMarkOrUndefined } from "./general";
9+
import { colorMap, defaultColorLUT, isMarkType, trueMarkOrUndefined, unescapeUnicode } from "./general";
1010

1111
const styleProps = [
1212
"color",
@@ -40,7 +40,7 @@ export function addTypeSpecificValues(
4040
) {
4141
switch (c.type) {
4242
case "text":
43-
current.text = c.text;
43+
current.text = unescapeUnicode(c.text!);
4444
break;
4545
case "score":
4646
current.score = {
@@ -340,12 +340,6 @@ export function translateJSON(
340340
for (const [i, p] of paragraphs.entries()) {
341341
const content = p.content ?? [];
342342
for (const c of content) {
343-
colorMap.find((e) => {
344-
console.log(e.value)
345-
console.log(c.marks?.at(0)?.attrs?.color);
346-
console.log(e.value.toUpperCase() == c.marks?.at(0)?.attrs?.color)
347-
e.value.toUpperCase() == c.marks?.at(0)?.attrs?.color;
348-
})?.name
349343
let current: MinecraftText = {
350344
color: defaultColorLUT(c.marks?.at(0)?.attrs?.color),
351345
bold: trueMarkOrUndefined(c, "bold"),

0 commit comments

Comments
 (0)