javascript DOMParser is parsing everything as <p>

  dom, domparser, html, javascript

this is how data is saved in database:

"<p><code><strong>const fs = require('fs');</strong></code></p><p><code><strong>const util = require('util');</strong></code></p><p><code><strong></strong></code></p><p><code><strong>// Convert fs.readFile into Promise version of same    </strong></code></p><p><code><strong>const readFile = util.promisify(fs.readFile);</strong></code></p><p><code><strong></strong></code></p><p><code><strong>function getStuff() {</strong></code></p><p><code><strong>  return readFile('test');</strong></code></p><p><code><strong>}</strong></code></p><p><code><strong></strong></code></p><p><code><strong>// Can't use `await` outside of an async function so you need to chain</strong></code></p><p><code><strong>// with then()</strong></code></p><p><code><strong>getStuff().then(data =&gt; {</strong></code></p><p><code><strong>  console.log(data);</strong></code></p><p><code><strong>})</strong></code></p><p><strong></strong></p>"

I parse the data :

  const document = new DOMParser().parseFromString(
    initialContent,
    "text/html"
  );
  console.log(document.body);

and this is what logged on console only with

tags

enter image description here

Source: Ask Javascript Questions

LEAVE A COMMENT