import sanitizeHtml from "sanitize-html"; // The real guard for landing-page rich text (add-landing-pages design decision 8): an allow-list of what the editor // produces, applied on the server render. Saves also refuse scripts, but rows written another way still pass here. // Ported from babyboo's richText.ts, minus images, tables, iframes and colours. const OPTIONS: sanitizeHtml.IOptions = { allowedTags: ["p", "h2", "h3", "strong", "em", "u", "s", "blockquote", "hr", "br", "ul", "ol", "li", "a"], allowedAttributes: { a: ["href", "target", "rel"], p: ["style"], h2: ["style"], h3: ["style"] }, allowedStyles: { "*": { "text-align": [/^(left|center|right)$/] } }, allowedSchemes: ["https", "mailto", "tel"], allowProtocolRelative: false, transformTags: { a: (tagName, attribs): sanitizeHtml.Tag => { const href = attribs.href ?? ""; const out: sanitizeHtml.Attributes = href ? { href } : {}; if (/^https:/i.test(href)) Object.assign(out, { target: "_blank", rel: "noopener noreferrer nofollow" }); return { tagName, attribs: out }; }, }, }; export function sanitizePageHtml(html: string): string { return sanitizeHtml(html, OPTIONS); }