223 lines
24 KiB
Plaintext
223 lines
24 KiB
Plaintext
const HTML_TAG_PATTERN = /<\/?[a-z][\s\S]*>/i;
|
|||
|
|
const DANGEROUS_TAGS = /* @__PURE__ */ new Set(["script", "style", "iframe", "object", "embed", "form", "input", "button", "textarea", "select", "svg", "math"]);
|
||
|
|
const ALLOWED_TAGS = /* @__PURE__ */ new Set([
|
||
|
|
"a",
|
||
|
|
"blockquote",
|
||
|
|
"br",
|
||
|
|
"code",
|
||
|
|
"del",
|
||
|
|
"div",
|
||
|
|
"em",
|
||
|
|
"h1",
|
||
|
|
"h2",
|
||
|
|
"h3",
|
||
|
|
"h4",
|
||
|
|
"h5",
|
||
|
|
"h6",
|
||
|
|
"hr",
|
||
|
|
"img",
|
||
|
|
"li",
|
||
|
|
"ol",
|
||
|
|
"p",
|
||
|
|
"pre",
|
||
|
|
"span",
|
||
|
|
"strong",
|
||
|
|
"table",
|
||
|
|
"tbody",
|
||
|
|
"td",
|
||
|
|
"th",
|
||
|
|
"thead",
|
||
|
|
"tr",
|
||
|
|
"ul"
|
||
|
|
]);
|
||
|
|
const GLOBAL_ATTRS = /* @__PURE__ */ new Set(["class", "title"]);
|
||
|
|
const TAG_ATTRS = {
|
||
|
|
a: /* @__PURE__ */ new Set(["href", "target", "rel", "title"]),
|
||
|
|
img: /* @__PURE__ */ new Set(["src", "alt", "title"]),
|
||
|
|
td: /* @__PURE__ */ new Set(["colspan", "rowspan"]),
|
||
|
|
th: /* @__PURE__ */ new Set(["colspan", "rowspan"])
|
||
|
|
};
|
||
|
|
const escapeHtml = (value) => value.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
||
|
|
const escapeAttr = (value) => escapeHtml(value).replace(/`/g, "`");
|
||
|
|
const isSafeUrl = (value) => {
|
||
|
|
const normalized = value.trim().replace(/[\u0000-\u001F\u007F\s]+/g, "");
|
||
|
|
if (!normalized) return false;
|
||
|
|
if (normalized.startsWith("#")) return true;
|
||
|
|
if (normalized.startsWith("/") && !normalized.startsWith("//")) return true;
|
||
|
|
if (/^(https?:|mailto:|tel:)/i.test(normalized)) return true;
|
||
|
|
return /^data:image\/(?:png|jpe?g|gif|webp);base64,/i.test(normalized);
|
||
|
|
};
|
||
|
|
const isAllowedAttr = (tagName, attrName) => {
|
||
|
|
if (attrName.startsWith("on")) return false;
|
||
|
|
return GLOBAL_ATTRS.has(attrName) || TAG_ATTRS[tagName]?.has(attrName);
|
||
|
|
};
|
||
|
|
const sanitizeFallback = (html) => html.replace(/<\s*(script|style|iframe|object|embed|svg|math)[^>]*>[\s\S]*?<\s*\/\s*\1\s*>/gi, "").replace(/\s+on[a-z]+\s*=\s*(?:"[^"]*"|'[^']*'|[^\s>]+)/gi, "").replace(/\s+(href|src)\s*=\s*(["'])\s*javascript:[\s\S]*?\2/gi, "");
|
||
|
|
export const sanitizeRichHtml = (html) => {
|
||
|
|
const source = html.trim();
|
||
|
|
if (!source) return "";
|
||
|
|
if (typeof DOMParser === "undefined") {
|
||
|
|
return sanitizeFallback(source);
|
||
|
|
}
|
||
|
|
const parser = new DOMParser();
|
||
|
|
const doc = parser.parseFromString(source, "text/html");
|
||
|
|
const walk = (node) => {
|
||
|
|
if (node.nodeType === Node.COMMENT_NODE) {
|
||
|
|
node.parentNode?.removeChild(node);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if (node.nodeType !== Node.ELEMENT_NODE) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
const element = node;
|
||
|
|
const tagName = element.tagName.toLowerCase();
|
||
|
|
if (DANGEROUS_TAGS.has(tagName)) {
|
||
|
|
element.remove();
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if (!ALLOWED_TAGS.has(tagName)) {
|
||
|
|
const children = Array.from(element.childNodes);
|
||
|
|
element.replaceWith(...children);
|
||
|
|
children.forEach(walk);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
Array.from(element.attributes).forEach((attribute) => {
|
||
|
|
const attrName = attribute.name.toLowerCase();
|
||
|
|
const attrValue = attribute.value;
|
||
|
|
if (!isAllowedAttr(tagName, attrName)) {
|
||
|
|
element.removeAttribute(attribute.name);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if ((attrName === "href" || attrName === "src") && !isSafeUrl(attrValue)) {
|
||
|
|
element.removeAttribute(attribute.name);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
if (tagName === "a" && element.getAttribute("href")) {
|
||
|
|
element.setAttribute("target", "_blank");
|
||
|
|
element.setAttribute("rel", "noopener noreferrer");
|
||
|
|
}
|
||
|
|
Array.from(element.childNodes).forEach(walk);
|
||
|
|
};
|
||
|
|
Array.from(doc.body.childNodes).forEach(walk);
|
||
|
|
return doc.body.innerHTML.trim();
|
||
|
|
};
|
||
|
|
const renderInlineMarkdown = (value) => {
|
||
|
|
let html = escapeHtml(value);
|
||
|
|
html = html.replace(/`([^`]+)`/g, "<code>$1</code>");
|
||
|
|
html = html.replace(/\*\*([^*]+)\*\*/g, "<strong>$1</strong>");
|
||
|
|
html = html.replace(/__([^_]+)__/g, "<strong>$1</strong>");
|
||
|
|
html = html.replace(/\*([^*]+)\*/g, "<em>$1</em>");
|
||
|
|
html = html.replace(/_([^_]+)_/g, "<em>$1</em>");
|
||
|
|
html = html.replace(/\[([^\]]+)\]\(([^)\s]+)(?:\s+"[^"]*")?\)/g, (_match, label, url) => {
|
||
|
|
const href = url.replace(/&/g, "&");
|
||
|
|
if (!isSafeUrl(href)) {
|
||
|
|
return label;
|
||
|
|
}
|
||
|
|
return `<a href="${escapeAttr(href)}" target="_blank" rel="noopener noreferrer">${label}</a>`;
|
||
|
|
});
|
||
|
|
return html.replace(/\n/g, "<br>");
|
||
|
|
};
|
||
|
|
const markdownToHtml = (markdown) => {
|
||
|
|
const lines = markdown.replace(/\r\n?/g, "\n").split("\n");
|
||
|
|
const html = [];
|
||
|
|
const paragraphs = [];
|
||
|
|
const listItems = [];
|
||
|
|
let listTag = "";
|
||
|
|
let inCodeFence = false;
|
||
|
|
let codeLines = [];
|
||
|
|
const flushParagraph = () => {
|
||
|
|
if (!paragraphs.length) return;
|
||
|
|
html.push(`<p>${renderInlineMarkdown(paragraphs.join("\n"))}</p>`);
|
||
|
|
paragraphs.length = 0;
|
||
|
|
};
|
||
|
|
const flushList = () => {
|
||
|
|
if (!listTag || !listItems.length) return;
|
||
|
|
html.push(`<${listTag}>${listItems.map((item) => `<li>${renderInlineMarkdown(item)}</li>`).join("")}</${listTag}>`);
|
||
|
|
listItems.length = 0;
|
||
|
|
listTag = "";
|
||
|
|
};
|
||
|
|
const flushCodeFence = () => {
|
||
|
|
html.push(`<pre><code>${escapeHtml(codeLines.join("\n"))}</code></pre>`);
|
||
|
|
codeLines = [];
|
||
|
|
};
|
||
|
|
lines.forEach((line) => {
|
||
|
|
if (/^```/.test(line.trim())) {
|
||
|
|
if (inCodeFence) {
|
||
|
|
inCodeFence = false;
|
||
|
|
flushCodeFence();
|
||
|
|
} else {
|
||
|
|
flushParagraph();
|
||
|
|
flushList();
|
||
|
|
inCodeFence = true;
|
||
|
|
codeLines = [];
|
||
|
|
}
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if (inCodeFence) {
|
||
|
|
codeLines.push(line);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if (!line.trim()) {
|
||
|
|
flushParagraph();
|
||
|
|
flushList();
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
const heading = /^(#{1,6})\s+(.+)$/.exec(line);
|
||
|
|
if (heading) {
|
||
|
|
flushParagraph();
|
||
|
|
flushList();
|
||
|
|
const level = heading[1].length;
|
||
|
|
html.push(`<h${level}>${renderInlineMarkdown(heading[2].trim())}</h${level}>`);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
const blockquote = /^>\s?(.*)$/.exec(line);
|
||
|
|
if (blockquote) {
|
||
|
|
flushParagraph();
|
||
|
|
flushList();
|
||
|
|
html.push(`<blockquote>${renderInlineMarkdown(blockquote[1] || "")}</blockquote>`);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
const unordered = /^\s*[-*+]\s+(.+)$/.exec(line);
|
||
|
|
if (unordered) {
|
||
|
|
flushParagraph();
|
||
|
|
if (listTag && listTag !== "ul") flushList();
|
||
|
|
listTag = "ul";
|
||
|
|
listItems.push(unordered[1] || "");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
const ordered = /^\s*\d+\.\s+(.+)$/.exec(line);
|
||
|
|
if (ordered) {
|
||
|
|
flushParagraph();
|
||
|
|
if (listTag && listTag !== "ol") flushList();
|
||
|
|
listTag = "ol";
|
||
|
|
listItems.push(ordered[1] || "");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
flushList();
|
||
|
|
paragraphs.push(line);
|
||
|
|
});
|
||
|
|
if (inCodeFence) {
|
||
|
|
flushCodeFence();
|
||
|
|
}
|
||
|
|
flushParagraph();
|
||
|
|
flushList();
|
||
|
|
return html.join("");
|
||
|
|
};
|
||
|
|
export const renderRichContent = (content) => {
|
||
|
|
const source = String(content || "").trim();
|
||
|
|
if (!source) return "";
|
||
|
|
const html = HTML_TAG_PATTERN.test(source) ? source : markdownToHtml(source);
|
||
|
|
return sanitizeRichHtml(html);
|
||
|
|
};
|
||
|
|
export const extractRichText = (content) => {
|
||
|
|
const html = renderRichContent(content);
|
||
|
|
if (!html) return "";
|
||
|
|
const spacedHtml = html.replace(/<br\s*\/?>/gi, " ").replace(/<\/(p|div|h[1-6]|li|blockquote|tr)>/gi, " </$1>");
|
||
|
|
if (typeof DOMParser === "undefined") {
|
||
|
|
return spacedHtml.replace(/<[^>]+>/g, " ").replace(/\s+/g, " ").trim();
|
||
|
|
}
|
||
|
|
const parser = new DOMParser();
|
||
|
|
const doc = parser.parseFromString(spacedHtml, "text/html");
|
||
|
|
return (doc.body.textContent || "").replace(/\s+/g, " ").trim();
|
||
|
|
};
|
||
|
|
|
||
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInJpY2gtY29udGVudC50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