0

on the server-side, using Nodejs. I receive a text message containing HTML. I want a function that converts the html to plain text. And please don't tell me to add the tag <plaintext> or <pre>. (convert_to_html function doesn't exist in nodejs)

socket.on('echo', (text) => {
   plaintext = convert_to_html(text);
   socket.emit('echo', {
      message: plaintext
   });
});

ideal results:

input: <h1>haha i am big</h1>

plaintext(what i want plaintext to be): &lt;h1 &60;haha i am big &lt;/h1 &60;

output: <h1>haha i am big</h1>

current result:

input: <h1>haha i am big</h1>

plaintext: <h1>haha i am big</h1>

output: haha i am big

just a noob
  • 105
  • 1
  • 1
  • 6
  • It looks like what you mean by *plaintext* is *html entity escaping*. If this is the case, you may want to consider using a package like [entities](https://www.npmjs.com/package/entities) to help accomplish this. – stephancasas May 10 '21 at 02:17
  • There are html entities packages you can use for this – charlietfl May 15 '21 at 18:28

2 Answers2

0

You can use the insertAdjacementHTML method on the browser side, here you go an example

socket.on("response", function (msg) {
  const messages = document.getElementById("messages");
  messages.insertAdjacentHTML("beforebegin", msg);
  window.scrollTo(0, document.body.scrollHeight);
});
luisbar
  • 676
  • 5
  • 11
0

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.

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 += "&nbsp;"
    }
    else if (current_char == '<'){
      result += "&lt;"
    }
    else if (current_char == '>'){
      result += "&gt;"
    }
    else if (current_char == '&'){
      result += "&amp;"
    }
    else if (current_char == '"'){
      result += "&quot;"
    }
    else if (current_char == "'"){
      result += "&apos;"
    }
    else{
      result += current_char;
    }
  }
  return result;

}
just a noob
  • 105
  • 1
  • 1
  • 6
  • You dont need to go over every char and some like spaces dont need converting, to htmlentities is just a case of replaceing < with < and > with > `html.replace(//g, '>')` would suffice or you could add more – Lawrence Cherone May 15 '21 at 18:35