Files
overleaf-cep/services/web/frontend/js/shared/components/labs/labs-description.tsx
Alexandre Bourdin 671df33da3 Merge pull request #32244 from overleaf/ab-labs-in-feature-flags
[web] Merge Labs programme into the Feature flags system

GitOrigin-RevId: db75e07bf3272becc11ef1eeda3850098b3daa9d
2026-03-26 09:06:51 +00:00

39 lines
1.0 KiB
TypeScript

import { FC, useMemo } from 'react'
import { micromark } from 'micromark'
import DOMPurify from 'dompurify'
const PURIFY_CONFIG = {
ALLOWED_TAGS: ['#text', 'p', 'em', 'strong', 'a'],
ALLOWED_ATTR: ['href'],
}
const LINK_REL = 'noreferrer noopener'
const LINK_TARGET = '_BLANK'
function sanitizeDescription(description: string) {
DOMPurify.addHook('afterSanitizeAttributes', node => {
if (node.nodeName === 'A') {
node.setAttribute('rel', LINK_REL)
node.setAttribute('target', LINK_TARGET)
}
})
try {
return DOMPurify.sanitize(micromark(description), PURIFY_CONFIG)
} finally {
DOMPurify.removeHook('afterSanitizeAttributes')
}
}
/**
* Renders a labs experiment description from markdown to sanitized HTML.
* Only bold, italic, and links are supported.
*/
export const LabsDescription: FC<{ description: string }> = ({
description,
}) => {
const html = useMemo(() => sanitizeDescription(description), [description])
return <div dangerouslySetInnerHTML={{ __html: html }} />
}