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. |
Answered by
remarkablemark
May 17, 2024
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. |
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment

You'll need to use type assertion:
See README.