Files
canina/frontend/application/lib/sanitizeBlogHtml.ts
devops 297aaadbe6
Deploy Canina / deploy (push) Successful in 2m12s
fix(blog): preserve sanitized content through hydration
2026-10-17 11:45:34 +00:00

33 lines
1.3 KiB
TypeScript

import sanitizeHtml from "sanitize-html";
const allowedTags = Array.from(new Set([
...sanitizeHtml.defaults.allowedTags,
"img", "iframe", "figure", "figcaption", "table", "thead", "tbody", "tfoot",
"tr", "th", "td", "video", "source", "details", "summary",
]));
export function sanitizeBlogHtml(content: unknown): string {
if (typeof content !== "string" || !content) return "";
return sanitizeHtml(content, {
allowedTags,
allowedAttributes: {
...sanitizeHtml.defaults.allowedAttributes,
"*": ["class", "id", "dir", "lang", "title"],
a: ["href", "name", "target", "rel", "class", "title"],
img: ["src", "alt", "title", "width", "height", "loading", "class"],
iframe: ["src", "title", "width", "height", "allow", "allowfullscreen", "frameborder", "scrolling", "loading", "class"],
video: ["src", "poster", "controls", "preload", "width", "height", "class"],
source: ["src", "type"],
td: ["colspan", "rowspan", "class"],
th: ["colspan", "rowspan", "scope", "class"],
},
allowedSchemes: ["http", "https", "mailto", "tel"],
allowedSchemesByTag: { img: ["http", "https", "data"] },
allowedIframeHostnames: ["www.youtube.com", "youtube.com", "www.aparat.com", "aparat.com"],
transformTags: {
a: sanitizeHtml.simpleTransform("a", { rel: "noopener noreferrer" }, true),
},
});
}