mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-05-23 17:19:37 +02:00
[web] Merge Labs programme into the Feature flags system GitOrigin-RevId: db75e07bf3272becc11ef1eeda3850098b3daa9d
39 lines
1.0 KiB
TypeScript
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 }} />
|
|
}
|