Sanitize rendered web content (#77)

This commit is contained in:
phucnguyen1707 2026-06-15 15:34:44 +07:00 committed by GitHub
parent cb192906bd
commit 59013cf0af
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 167 additions and 133 deletions

View file

@ -0,0 +1,33 @@
import sanitizeHtml from "sanitize-html";
const allowedTags = sanitizeHtml.defaults.allowedTags.concat([
"figure",
"figcaption",
"h1",
"h2",
"h3",
"h4",
"h5",
"h6",
"img"
]);
const allowedAttributes = {
...sanitizeHtml.defaults.allowedAttributes,
a: ["href", "name", "target", "rel"],
img: ["src", "alt", "title", "width", "height", "loading"],
code: ["class"],
pre: ["class"]
};
export function sanitizeRenderedHtml(html: string): string {
return sanitizeHtml(html, {
allowedTags,
allowedAttributes,
allowedSchemes: ["http", "https", "mailto", "tel"],
allowedSchemesByTag: {
img: ["http", "https"]
},
allowProtocolRelative: false
});
}