@liquidcars/atlas 0.1.48 → 0.1.49

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,84 @@
1
+ const escapeHtml = value => String(value ?? "").replace(/[&<>"']/g, char =>
2
+ ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;" })[char]);
3
+
4
+ const allowedTags = new Set(["P", "BR", "H1", "H2", "H3", "H4", "UL", "OL", "LI", "STRONG", "EM", "CODE", "PRE", "BLOCKQUOTE", "A", "HR", "TABLE", "THEAD", "TBODY", "TR", "TH", "TD"]);
5
+
6
+ export function sanitizePanelHtml(html, { title = "" } = {}) {
7
+ const document = new DOMParser().parseFromString(String(html || ""), "text/html");
8
+ const first = document.body.firstElementChild;
9
+ if (first && /^H[1-4]$/.test(first.tagName) && title && first.textContent.trim().toLocaleLowerCase() === String(title).trim().toLocaleLowerCase()) first.remove();
10
+ // Preserve href values while dropping all event handlers and other attributes.
11
+ for (const anchor of document.body.querySelectorAll("a")) {
12
+ const href = anchor.getAttribute("href") || "";
13
+ anchor.dataset.atlasSafeHref = /^(atlas:|https?:\/\/)/i.test(href) ? href : "";
14
+ }
15
+ const walk = node => {
16
+ for (const child of [...node.children]) {
17
+ const href = child.tagName === "A" ? child.dataset.atlasSafeHref : "";
18
+ for (const attribute of [...child.attributes]) child.removeAttribute(attribute.name);
19
+ if (!allowedTags.has(child.tagName)) {
20
+ child.replaceWith(...child.childNodes);
21
+ walk(node);
22
+ continue;
23
+ }
24
+ if (href) {
25
+ child.setAttribute("href", href);
26
+ if (/^https?:\/\//i.test(href)) {
27
+ child.setAttribute("target", "_blank");
28
+ child.setAttribute("rel", "noopener noreferrer");
29
+ }
30
+ }
31
+ walk(child);
32
+ }
33
+ };
34
+ walk(document.body);
35
+ return document.body.innerHTML;
36
+ }
37
+
38
+ function inlineMarkdown(source) {
39
+ const escaped = escapeHtml(source);
40
+ return escaped
41
+ .replace(/`([^`]+)`/g, "<code>$1</code>")
42
+ .replace(/\*\*([^*]+)\*\*/g, "<strong>$1</strong>")
43
+ .replace(/\*([^*]+)\*/g, "<em>$1</em>")
44
+ .replace(/\[([^\]]+)\]\((atlas:[^)]+|https?:\/\/[^)]+)\)/g, '<a href="$2">$1</a>');
45
+ }
46
+
47
+ export function renderPanelContent(content, options = {}) {
48
+ if (!content || typeof content !== "object") return "";
49
+ if (content.format === "html") return sanitizePanelHtml(content.value || "", options);
50
+ if (content.format !== "markdown") return "";
51
+ const blocks = [];
52
+ let list = [];
53
+ let listKind = "ul";
54
+ let code = [];
55
+ let inCode = false;
56
+ const flushList = () => {
57
+ if (list.length) blocks.push("<" + listKind + ">" + list.map(line => "<li>" + inlineMarkdown(line) + "</li>").join("") + "</" + listKind + ">");
58
+ list = [];
59
+ };
60
+ for (const line of String(content.value || "").split(/\r?\n/)) {
61
+ if (/^\s*```/.test(line)) {
62
+ flushList();
63
+ if (inCode) { blocks.push("<pre><code>" + escapeHtml(code.join("\n")) + "</code></pre>"); code = []; }
64
+ inCode = !inCode;
65
+ } else if (inCode) code.push(line);
66
+ else if (/^\s*([-*]|\d+\.) /.test(line)) {
67
+ const kind = /^\s*\d+\./.test(line) ? "ol" : "ul";
68
+ if (listKind !== kind) flushList();
69
+ listKind = kind;
70
+ list.push(line.replace(/^\s*([-*]|\d+\.) /, ""));
71
+ }
72
+ else {
73
+ flushList();
74
+ if (!line.trim()) continue;
75
+ if (/^\s*---+\s*$/.test(line)) { blocks.push("<hr>"); continue; }
76
+ if (/^>\s?/.test(line)) { blocks.push("<blockquote>" + inlineMarkdown(line.replace(/^>\s?/, "")) + "</blockquote>"); continue; }
77
+ const heading = /^(#{1,4})\s+(.+)$/.exec(line);
78
+ blocks.push(heading ? `<h${heading[1].length}>${inlineMarkdown(heading[2])}</h${heading[1].length}>` : "<p>" + inlineMarkdown(line) + "</p>");
79
+ }
80
+ }
81
+ flushList();
82
+ if (code.length) blocks.push("<pre><code>" + escapeHtml(code.join("\n")) + "</code></pre>");
83
+ return sanitizePanelHtml(blocks.join(""), options);
84
+ }