npm install react-html-parser
# or
yarn add react-html-parser
import React from 'react';
import ReactHtmlParser, { processNodes, convertNodeToElement, htmlparser2 } from 'react-html-parser';
class HtmlComponent extends React.Component {
render() {
const html = '<div>Example HTML string</div>';
return <div>{ ReactHtmlParser(html) }</div>;
}
}
import ReactHtmlParser from "react-html-parser";
// 解析html内容
const parseHtmlContent = (htmlContent) => {
const parseContent = ReactHtmlParser(htmlContent, {
transform: (node) => {
if (node.type == "text") {
const parseText = ReactHtmlParser(node.data, {
transform: (node) => {
return node;
},
});
return parseText;
}
},
});
return parseContent;
};
import Prism from "prismjs";
import ReactHtmlParser, { convertNodeToElement } from "react-html-parser";
// 解析html内容
const parseHtmlContent = (htmlContent) => {
const new_anchorList = [];
const parseContent = ReactHtmlParser(htmlContent, {
transform: (node) => {
if (node.type == "text") {
const parseText = ReactHtmlParser(node.data, {
transform: (node, index) => {
if (node.type === "tag" && node.name === "code") {
if ("class" in node.attribs) {
node.attribs.class = node.attribs.class + " line-numbers";
} else {
node.attribs.class = "language-javascript line-numbers";
}
node.attribs["data-prismjs-copy"] = "复制代码";
node.attribs["data-prismjs-copy-error"] = "复制失败";
node.attribs["data-prismjs-copy-success"] = "复制成功(〃'▽'〃)";
return convertNodeToElement(node, index);
}
},
});
return parseText;
}
},
});
return parseContent;
};
// 如果dom元素已经渲染完成
useEffect(() => {
if (content.length > 0) {
try {
Prism.highlightAll();
} catch (error) {
console.log(error);
}
}
}, [content]);
此页面不支持夜间模式!
已进入夜间模式!
已进入普通模式!
搜索框不允许为空
签到成功!经验+5!芋圆币+2!
签到失败!今日已签到!
需要登录社区账号才可以进入!