html - 在服务器端使用 NodeJS 将 html 转换为纯文本
问题描述
在服务器端,使用 Nodejs。我收到一条包含 HTML 的短信。我想要一个将 html 转换为纯文本的函数。请不要告诉我添加标签<plaintext>
或 <pre>
. (nodejs中不存在convert_to_html函数)
socket.on('echo', (text) => {
plaintext = convert_to_html(text);
socket.emit('echo', {
message: plaintext
});
});
理想结果:
输入:<h1>haha i am big</h1>
明文(我想要明文是什么):<h1 &60;haha i am big </h1 &60;
输出:<h1>haha i am big</h1>
当前结果:
输入:<h1>haha i am big</h1>
纯文本:<h1>haha i am big</h1>
输出:哈哈我很大
解决方案
可以在浏览器端使用insertAdjacementHTML方法,这里举个例子
socket.on("response", function (msg) {
const messages = document.getElementById("messages");
messages.insertAdjacentHTML("beforebegin", msg);
window.scrollTo(0, document.body.scrollHeight);
});
推荐阅读
- log4j2 - 如何将 Apache Pulsar 设置为 Log4j2 的附加程序?
- javascript - 如何在 Grapesjs 中添加 iframe?
- postgresql - 在 Heroku 上运行 pg:pull 时出现 pg_restore 错误 1
- spring-batch - Spring Batch - 创建两个数据源以及如何自定义使用其他属性
- spring - Apache Artemis 不会停止扫描过期时间
- excel - VB.NET 中的 VBA 等效项
- javascript - .not(this) 在点击时不起作用。我没有使用列表
- oracle - 使用 KEY 加载 CLOB
- python - 网格搜索适合不接受张量列表
- angular - Kendo Angular Editor setHTML:在特定位置或光标后插入 HTML 内容