2024-06-27 03:27:40 -04:00
|
|
|
import { htmlSafe } from "@ember/template";
|
|
|
|
import { escapeExpression } from "discourse/lib/utilities";
|
|
|
|
|
|
|
|
export function jsonToHtml(json) {
|
2024-07-02 02:38:33 -04:00
|
|
|
if (json === null) {
|
|
|
|
return "null";
|
|
|
|
}
|
|
|
|
|
2024-06-27 03:27:40 -04:00
|
|
|
if (typeof json !== "object") {
|
|
|
|
return escapeExpression(json);
|
|
|
|
}
|
2024-06-27 18:59:51 -04:00
|
|
|
|
2024-06-27 03:27:40 -04:00
|
|
|
let html = "<ul>";
|
2024-06-27 18:59:51 -04:00
|
|
|
|
|
|
|
for (let [key, value] of Object.entries(json)) {
|
2024-06-27 03:27:40 -04:00
|
|
|
html += "<li>";
|
2024-06-27 18:59:51 -04: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 03:27:40 -04:00
|
|
|
} else {
|
|
|
|
if (typeof value === "string") {
|
2024-06-27 18:59:51 -04:00
|
|
|
value = escapeExpression(value).replace(/\n/g, "<br>");
|
2024-06-27 03:27:40 -04:00
|
|
|
}
|
2024-06-27 18:59:51 -04:00
|
|
|
html += `<strong>${key}:</strong> ${value}`;
|
2024-06-27 03:27:40 -04:00
|
|
|
}
|
2024-06-27 18:59:51 -04:00
|
|
|
|
2024-06-27 03:27:40 -04:00
|
|
|
html += "</li>";
|
|
|
|
}
|
2024-06-27 18:59:51 -04:00
|
|
|
|
2024-06-27 03:27:40 -04:00
|
|
|
html += "</ul>";
|
|
|
|
return htmlSafe(html);
|
|
|
|
}
|