hedgedoc/frontend/src/components/common/html-to-react/html-to-react.tsx
Tilman Vatteroth c68fbea606 fix: migrate code to html-to-react v2
Signed-off-by: Renovate Bot <bot@renovateapp.com>
Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
2023-05-10 16:30:38 +02:00

32 lines
1 KiB
TypeScript

/*
* SPDX-FileCopyrightText: 2023 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import type { ParserOptions } from '@hedgedoc/html-to-react'
import { convertHtmlToReact } from '@hedgedoc/html-to-react'
import type DOMPurify from 'dompurify'
import { sanitize } from 'dompurify'
import React, { Fragment, useMemo } from 'react'
export interface HtmlToReactProps {
htmlCode: string
domPurifyConfig?: DOMPurify.Config
parserOptions?: ParserOptions
}
/**
* Renders
* @param htmlCode
* @param domPurifyConfig
* @param parserOptions
* @constructor
*/
export const HtmlToReact: React.FC<HtmlToReactProps> = ({ htmlCode, domPurifyConfig, parserOptions }) => {
const elements = useMemo(() => {
const sanitizedHtmlCode = sanitize(htmlCode, { ...domPurifyConfig, RETURN_DOM_FRAGMENT: false, RETURN_DOM: false })
return convertHtmlToReact(sanitizedHtmlCode, parserOptions)
}, [domPurifyConfig, htmlCode, parserOptions])
return <Fragment>{elements}</Fragment>
}