I have some HTML content that is being returned from a CMS that I wish to display in a NextJS V13 app. I am using the html-react-parser package rather than dangerouslySetHtml to parse the HTML content as I also wish to replace embedded <video> tags with a custom <VideoPlayer> React component
My code looks like this:
"use client"
import parse, { DOMNode, Element, HTMLReactParserOptions } from "html-react-parser"
import { sanitize } from "isomorphic-dompurify"
import VideoPlayer from "@/components/video-player"
export default function ArticleContent({ content }: { content: string }) {
const parserOptions: HTMLReactParserOptions = {
replace: (domNode: DOMNode) => {
// Replace embedded <video> tags with our custom VideoPlayer component
if (domNode instanceof Element && domNode.name === "video") {
// TODO: Figure out correct type assertions so typesript doesn't complain
// @ts-ignore
const src = domNode.children[0].next.attribs.src
return <VideoPlayer src={src} />
}
},
}
return (
<div className="prose-img:rounded-2xl prose-img:shadow-xl prose-img:shadow-slate-900 dark:prose-img:shadow-gray-500">
{parse(sanitize(content), parserOptions)}
</div>
)
}
Typescirpt is telling me:
Property 'attribs' does not exist on type 'ChildNode'.
Property 'attribs' does not exist on type 'Comment'.ts(2339)
⚠ Error (TS2339)
... and "src" is typed as "any".
I've tried several different type assertions to try and get Typescript not to complain, but so far nothing seems to work. Can anyone point me in the right direction to figuring out how to type the domNode path so that TS will stop complaining about the assignment?