Giter Site home page Giter Site logo

posthtml-parser's Introduction

posthtml-parser

npm version Build Status Coverage Status

Parse HTML/XML to PostHTML AST. More about PostHTML

Install

NPM install

$ npm install posthtml-parser

Usage

Input HTML

<a class="animals" href="#">
    <span class="animals__cat" style="background: url(cat.png)">Cat</span>
</a>
const parser = require('posthtml-parser')
const fs = require('fs')
const html = fs.readFileSync('path/to/input.html').toString()

console.log(parser(html)) // Logs a PostHTML AST

input HTML

<a class="animals" href="#">
    <span class="animals__cat" style="background: url(cat.png)">Cat</span>
</a>

Result PostHTMLTree

[{
    tag: 'a',
    attrs: {
        class: 'animals',
        href: '#'
    },
    content: [
        '\n    ',
            {
            tag: 'span',
            attrs: {
                class: 'animals__cat',
                style: 'background: url(cat.png)'
            },
            content: ['Cat']
        },
        '\n'
    ]
}]

PostHTML AST Format

Any parser being used with PostHTML should return a standard PostHTML Abstract Syntax Tree (AST). Fortunately, this is a very easy format to produce and understand. The AST is an array that can contain strings and objects. Any strings represent plain text content to be written to the output. Any objects represent HTML tags.

Tag objects generally look something like this:

{
    tag: 'div',
    attrs: {
        class: 'foo'
    },
    content: ['hello world!']
}

Tag objects can contain three keys. The tag key takes the name of the tag as the value. This can include custom tags. The optional attrs key takes an object with key/value pairs representing the attributes of the html tag. A boolean attribute has an empty string as its value. Finally, the optional content key takes an array as its value, which is a PostHTML AST. In this manner, the AST is a tree that should be walked recursively.

Options

directives

Type: Array
Default: [{name: '!doctype', start: '<', end: '>'}]
Description: Adds processing of custom directives. Note: The property name in custom directives can be String or RegExp type

License

MIT

posthtml-parser's People

Contributors

scrum avatar voischev avatar oantoro avatar shvaikalesh avatar hibiyasleep avatar awinogradov avatar mrmlnc avatar qfox avatar ambar avatar kisenka avatar

Watchers

Syg avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.