3

I am really new to node. I was working on REST calls. I get a request from Postman(using it to check REST api calls) with a URL. I need to make a few word level changes on the contents of that URL

e.g if the url is received is https://en.wikipedia.org/wiki/Main_Page I need to fetch its content(currently doing it with the get request on page) and change all small 'the' to "THE" (captialised). I need to make many-many such word level changes

For this I created a DOM model of the HTML page using htmlparser2.DomHandler() in node htmlparser2 (https://www.npmjs.com/package/htmlparser2). And i was able to traverse the DOM and change the word.

Now i have a new DOM with changed content and i need to convert it back to HTML and send the new html as response back to postman(client). So that i can display the changed content on my own web-page

So my question is how to create HTML back from DOM Tree created in html-parser2 in node.js

PS: I am able to send the original html without making a DOM and display original content DOM : Document Object Model

1 Answers1

0

Just use the htmlparser-to-html library.

Install it:

npm install --save htmlparser-to-html

Require it:

const html = require('htmlparser-to-html')

Let's say you parsed the doctype tag and output the result dom object to the console.log:

[ { name: '!doctype',
    data: '!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"',
    type: 'directive'} ]

Convert the dom object using the function html():

html({ name: '!doctype',
       data: '!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"',
       type: 'directive'})

Result — the same tag you parsed:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
revelt
  • 2,312
  • 1
  • 25
  • 37