import sanitizeHtml from "sanitize-html" const TRACKING_PIXEL_DIMENSION_THRESHOLD = 3 function isTrackingPixel(tagName: string, attributes: Record): boolean { if (tagName !== "img") return false const width = parseInt(attributes.width, 10) const height = parseInt(attributes.height, 10) if (!isNaN(width) && width <= TRACKING_PIXEL_DIMENSION_THRESHOLD) return true if (!isNaN(height) && height <= TRACKING_PIXEL_DIMENSION_THRESHOLD) return true return false } const SANITIZE_OPTIONS: sanitizeHtml.IOptions = { allowedTags: [ "h1", "h2", "h3", "h4", "h5", "h6", "p", "a", "ul", "ol", "li", "blockquote", "pre", "code", "em", "strong", "del", "br", "hr", "img", "figure", "figcaption", "table", "thead", "tbody", "tr", "th", "td", ], allowedAttributes: { a: ["href", "title", "rel"], img: ["src", "alt", "title", "width", "height"], }, allowedSchemes: ["http", "https"], exclusiveFilter: (frame) => isTrackingPixel(frame.tag, frame.attribs), } export function sanitizeEntryContent(htmlContent: string): string { return sanitizeHtml(htmlContent, SANITIZE_OPTIONS) }