How to substitute parsed HTML elements with React components (e.g. Material UI) #1424
-
I am working on a project that uses Material UI, and I would like to find a way to use these when parsing an HTML string. For example, given the following string: <Box component="p" sx={{color: "red"}}>Test</Box> Looking at the docs, I figured parse('<p>test</p>', {
replace(domNode) {
if (domNode === "p") {
return <Box component="p" sx={{color: "red"}}>Test</Box>
}
},
}); However 'domNode' and it's various attributes don't tell you what sort of element is being parsed. So, unless I am missing something, using What would be the best way to achieve this? Any suggestions greatly appreciated, thank you in advance. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
You're on the right track with replace. Can you add a parse('<p>test</p>', {
replace(domNode) {
console.log(domeNode);
},
}); If you check parse('<p>test</p>', {
replace(domNode) {
- if (domNode === "p") {
+ if (domNode.name === "p") {
return <Box component="p" sx={{color: "red"}}>Test</Box>
}
},
}); See Stackblitz demo. |
Beta Was this translation helpful? Give feedback.
You'll need to use type assertion:
See README.