How to use htmlparser2

node v0.12.18
version: 1.0.0
endpointsharetweet
var htmlparser = require('htmlparser2'); var request = require('request-promise'); var green = '#B5FFC8'; var blue = '#BBDAFF'; var options = { uri: 'https://en.wikipedia.org/wiki/Gem%C3%BCtlichkeit' }; var body = await request(options); var result = []; var color = green; var parser = new htmlparser.Parser({ onopentag: function(name, attributes){ result.push('<' + name + '>'); }, ontext: function(text){ //console.log(text); var parts = text.split(' '); for (var part of parts) { result.push('<span style="background-color: ' + color + '">' + part + '</span> '); color = color == green ? blue : green; } }, onclosetag: function(tagName){ result.push("</" + tagName + ">"); } }, {decodeEntities: true}); parser.write(body); parser.end(); result.join('');
Loading…

no comments

    sign in to comment