Files

36 lines
891 B
JavaScript
Raw Permalink Normal View History

2024-06-27 17:27:40 +10:00
import { htmlSafe } from "@ember/template";
import { escapeExpression } from "discourse/lib/utilities";
export function jsonToHtml(json) {
if (json === null) {
return "null";
}
2024-06-27 17:27:40 +10:00
if (typeof json !== "object") {
return escapeExpression(json);
}
2024-06-28 00:59:51 +02:00
2024-06-27 17:27:40 +10:00
let html = "<ul>";
2024-06-28 00:59:51 +02:00
for (let [key, value] of Object.entries(json)) {
2024-06-27 17:27:40 +10:00
html += "<li>";
2024-06-28 00:59:51 +02:00
key = escapeExpression(key);
if (typeof value === "object" && Array.isArray(value)) {
html += `<strong>${key}:</strong> ${jsonToHtml(value)}`;
} else if (typeof value === "object") {
html += `<strong>${key}:</strong> <ul><li>${jsonToHtml(value)}</li></ul>`;
2024-06-27 17:27:40 +10:00
} else {
if (typeof value === "string") {
2024-06-28 00:59:51 +02:00
value = escapeExpression(value).replace(/\n/g, "<br>");
2024-06-27 17:27:40 +10:00
}
2024-06-28 00:59:51 +02:00
html += `<strong>${key}:</strong> ${value}`;
2024-06-27 17:27:40 +10:00
}
2024-06-28 00:59:51 +02:00
2024-06-27 17:27:40 +10:00
html += "</li>";
}
2024-06-28 00:59:51 +02:00
2024-06-27 17:27:40 +10:00
html += "</ul>";
return htmlSafe(html);
}