@lancer/ihtml-parser
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

ihtml-parser

NPM version

A fork of htmlparser2 with added interpolation syntax. Made for use with the Lancer html framework.

Installation

npm install @lancer/ihtml-parser

Usage

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:

const Parser = require("@lancer/ihtml-parser");
const parser = new htmlparser2.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();

Package Sidebar

Install

npm i @lancer/ihtml-parser

Weekly Downloads

8

Version

1.0.1

License

MIT

Unpacked Size

107 kB

Total Files

24

Last publish

Collaborators

  • soundly_typed