diff --git a/src/utils/htmlToText.ts b/src/utils/htmlToText.ts new file mode 100644 index 0000000000..580f21b4e0 --- /dev/null +++ b/src/utils/htmlToText.ts @@ -0,0 +1,26 @@ +import React from 'react'; +import parse from 'html-react-parser'; + +const collectText = (node: React.ReactNode): string => { + if (typeof node === 'string' || typeof node === 'number') { + return String(node); + } + + if (Array.isArray(node)) { + return node.map(collectText).join(''); + } + + if (React.isValidElement(node)) { + return collectText(node.props.children); + } + + return ''; +}; + +export default function htmlToText(html: string): string { + if (!html) { + return ''; + } + + return collectText(parse(html)); +} \ No newline at end of file