Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

LancerStudio/ihtml-parser

Open more actions menu
 
 

Repository files navigation

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();

About

Forgiving HTML and XML parser

Resources

License

Security policy

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages

  • TypeScript 98.9%
  • HTML 1.1%
Morty Proxy This is a proxified and sanitized view of the page, visit original site.