A fork of htmlparser2 with added interpolation syntax. Made for use with the Lancer html framework.
npm install @lancer/ihtml-parser
ihtml-parser extends htmlparser2 to support {{}} for interpolation. The biggest difference in usage is receiving string | Interpolation instead of string during the parsing process.
Note that {{}} is not arbitrary string interpolation; it can only appear as an element's attribute or within an element's text.
Here's a quick example:
constParser=require("@lancer/ihtml-parser");constparser=newhtmlparser2.Parser({onopentag(name,attributes,iAttributes){/* * `attributes` has type { [attrName: string]: string } | null * * However, for attributes with interpolations (e.g. foo="{{bar}}" or {{foo}}="bar"), * a third argument is present with type (ParsedAttribute[] | null) * * where ParsedAttribute = [DynamicContent, DynamicContent] * where DynamicContent = Array<string | Interpolation> * where Interpolation = { code: string } * * (Note that Interpolation is a class) */console.log("Open tag",name,attributes);},ontext(text){/* * Before, `text` had type string * * Now, it has type string | Interpolation */console.log("-->",text);},});parser.write(`Xyz <a href="{{ myLink.url }}">Go to {{myLink.text}}</ a>`);parser.end();