Files
discourse-ai/assets/javascripts/discourse/lib/utilities.js
T

39 lines
1013 B
JavaScript
Raw Normal View History

2024-06-27 17:27:40 +10:00
import { htmlSafe } from "@ember/template";
import { escapeExpression } from "discourse/lib/utilities";
2024-02-19 09:56:28 -08:00
export const IMAGE_MARKDOWN_REGEX =
/!\[(.*?)\|(\d{1,4}x\d{1,4})(,\s*\d{1,3}%)?(.*?)\]\((upload:\/\/.*?)\)(?!(.*`))/g;
2024-06-27 17:27:40 +10:00
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);
}