[英]html to plaintext with NodeJS on server side
on the server-side, using Nodejs.在服务器端,使用 Nodejs。 I receive a text message containing HTML.
我收到一条包含 HTML 的短信。 I want a function that converts the html to plain text.
我想要一个 function 将 html 转换为纯文本。 And please don't tell me to add the tag
<plaintext>
or <pre>
.请不要告诉我添加标签
<plaintext>
或<pre>
。 (convert_to_html function doesn't exist in nodejs) (convert_to_html function 在nodejs中不存在)
socket.on('echo', (text) => {
plaintext = convert_to_html(text);
socket.emit('echo', {
message: plaintext
});
});
ideal results:理想结果:
input: <h1>haha i am big</h1>
输入:
<h1>haha i am big</h1>
plaintext(what i want plaintext to be): <h1 &60;haha i am big </h1 &60;
明文(我想要明文是什么):
<h1 &60;haha i am big </h1 &60;
output: <h1>haha i am big</h1>
output:
<h1>haha i am big</h1>
current result:当前结果:
input: <h1>haha i am big</h1>
输入:
<h1>haha i am big</h1>
plaintext: <h1>haha i am big</h1>
明文:
<h1>haha i am big</h1>
output: haha i am big output:哈哈我大
You can use the insertAdjacementHTML method on the browser side, here you go an example可以在浏览器端使用insertAdjacementHTML方法,这里以go为例
socket.on("response", function (msg) {
const messages = document.getElementById("messages");
messages.insertAdjacentHTML("beforebegin", msg);
window.scrollTo(0, document.body.scrollHeight);
});
still don't have a proper solution.仍然没有合适的解决方案。 while i wait for one, i will use reserved characters as a temporary solution.
当我等待一个时,我将使用保留字符作为临时解决方案。 https://devpractical.com/display-html-tags-as-plain-text/#:~:text=You%20can%20show%20HTML%20tags,the%20reader%20on%20the%20browser .
https://devpractical.com/display-html-tags-as-plain-text/#:~:text=You%20can%20show%20HTML%20tags,the%20reader%20on%20the%20browser 。
function parse_to_plain_text(html){
var result = "";
for (var i = 0; i < html.length; i++) {
var current_char = html[i];
if (current_char == ' '){
result += " "
}
else if (current_char == '<'){
result += "<"
}
else if (current_char == '>'){
result += ">"
}
else if (current_char == '&'){
result += "&"
}
else if (current_char == '"'){
result += """
}
else if (current_char == "'"){
result += "'"
}
else{
result += current_char;
}
}
return result;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.