Manipulating Raw HTML in Node with the Visitor Pattern
TL;DR Check out the Adding ids to Header Nodes section to see how to use the Visitor Pattern.
I recently wanted to add an id to every header tag in an HTML string. This sounds like an easy task right? A little jQuery and the problem is solved: $(":header").attr("id", "1")
is a rough solution.
The catch is that this was in Node, not the browser. In most situations I would reach for Cheerio, which replicates the jQuery API inside node, but I needed something lighter weight.
This is a story of parsing, recursing, and modifying raw HTML without comfortable, declarative APIs. It's surprisingly approachable with the help of the Visitor pattern.