Skip to content

Commit 7379b34

Browse files
committed
fix title being undefined, allow pretty colours
1 parent 187f459 commit 7379b34

2 files changed

Lines changed: 38 additions & 8 deletions

File tree

src/datapack.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ConfigClass } from "./config";
33

44
export interface Datapack {
55
id: string;
6-
name: string;
6+
name: string | undefined;
77
description:
88
| string
99
| {
@@ -94,7 +94,7 @@ function detectModules(datapackZip: JSZip): Set<Module> {
9494
function writeConfigWidgetsToDocument(configObject: ConfigClass) {
9595
const widgets: Array<DocumentFragment> = configObject.get_widgets_html();
9696
const screen = document.getElementById("config-screen")!;
97-
widgets.forEach(element => {
97+
widgets.forEach((element) => {
9898
screen.appendChild(element);
9999
});
100-
}
100+
}

src/main.ts

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ function createDatapackDisplayElement(dp: Datapack): DocumentFragment {
4646
const template = document.getElementById("datapack-template") as HTMLTemplateElement;
4747
const clone = template.content.cloneNode(true) as DocumentFragment;
4848

49-
(clone.querySelector(".name") as HTMLElement).innerText = dp.name;
50-
(clone.querySelector(".description") as HTMLElement).innerText = descriptionToDisplayable(
51-
dp.description
52-
);
49+
const { name, description } = getNameDesc(dp.mcmeta);
50+
51+
(clone.querySelector(".name") as HTMLElement).innerHTML = name;
52+
(clone.querySelector(".description") as HTMLElement).innerHTML = description;
5353

5454
if (dp.icon) {
5555
(clone.querySelector("img") as HTMLImageElement).src = URL.createObjectURL(dp.icon);
@@ -58,7 +58,37 @@ function createDatapackDisplayElement(dp: Datapack): DocumentFragment {
5858
return clone;
5959
}
6060

61+
function getNameDesc(mcmeta: any): { name: string; description: string } {
62+
let name = mcmeta.pack.name;
63+
let description = descriptionToDisplayable(mcmeta.pack.description);
64+
65+
if (!name) {
66+
const splitDescription = description.split("\n");
67+
name = splitDescription[0];
68+
description = splitDescription.slice(1).join("\n");
69+
} else {
70+
name = sanitizeHtml(name);
71+
}
72+
73+
if (!description) {
74+
description = "No description available";
75+
}
76+
77+
return { name, description };
78+
}
79+
6180
function descriptionToDisplayable(description: Datapack["description"]): string {
62-
if (Array.isArray(description)) return description.map((desc) => desc.text).join("");
81+
console.log(description);
82+
if (Array.isArray(description))
83+
return description
84+
.map((desc) => ({ text: sanitizeHtml(desc.text), color: desc.color }))
85+
.map((desc) => `<span style="color: ${desc.color}">${desc.text}</span>`)
86+
.join("");
6387
else return description;
6488
}
89+
90+
function sanitizeHtml(unsafe: string): string {
91+
const div = document.createElement("div");
92+
div.innerText = unsafe;
93+
return div.innerHTML;
94+
}

0 commit comments

Comments
 (0)