import sanitizeHtml from 'sanitize-html';
const FAQ_ALLOWED_TAGS = [
'p',
'br',
'strong',
'b',
'em',
'i',
'u',
'ul',
'ol',
'li',
'a',
'img',
'video',
'iframe',
'h2',
'h3',
'blockquote',
];
const FAQ_ALLOWED_ATTRIBUTES: Record = {
a: ['href', 'title', 'target', 'rel'],
img: ['src', 'alt', 'title'],
video: ['src', 'controls'],
iframe: ['src', 'allowfullscreen', 'title'],
};
/** Sanitizes staff-authored FAQ HTML before persistence or display. */
export function sanitizeFaqHtml(html: string): string {
return sanitizeHtml(html, {
allowedTags: FAQ_ALLOWED_TAGS,
allowedAttributes: FAQ_ALLOWED_ATTRIBUTES,
allowedSchemes: ['http', 'https', 'mailto'],
allowedSchemesByTag: {
iframe: ['http', 'https'],
img: ['http', 'https'],
video: ['http', 'https'],
},
transformTags: {
a: sanitizeHtml.simpleTransform('a', { rel: 'noopener noreferrer', target: '_blank' }),
},
});
}