@wayowe/rehype-webparser

0.10.3 • Public • Published

@wayowe/rehype-webparser

Parses HTML via Webparser to a HAST syntax tree.

Installation

npm install --save @wayowe/rehype-webparser

Usage

This example shows how we parse HTML with Webparser transform it into HAST compatible structure and finally compile that data back to HTML with hast-util-to-html.

Say we have the following file, example.html:

<!DOCTYPE html>
<title>Hello!</title>
<h1 id="world">World!</h1>

And our script, example.js, looks as follows:

const vfile = require('to-vfile')
const report = require('vfile-reporter')
const unified = require('unified')
const parse = require('@wayowe/rehype-webparser')
const toHTML = require('hast-util-to-html')

// A compiler is needed to inform unified how to transform it back to HTML
function stringify() {
  this.Compiler = compiler

  function compiler(tree) {
    return toHTML(tree)
  }
}

unified()
  .use(parse)
  //.use(yourPlugin)
  .use(stringify)
  .process(vfile.readSync('example.html'), function(err, file) {
    console.error(report(err || file))
    console.log(String(file))
  })

Now, running node example yields:

<!doctype html>
<title>Hello!</title>
<h1 id="world">World!</h1>

API

processor.use(webparser[, options])

Configure the processor to read HTML as input and process a HAST syntax tree.

options.*

Webparser options.

/@wayowe/rehype-webparser/

    Package Sidebar

    Install

    npm i @wayowe/rehype-webparser

    Weekly Downloads

    3

    Version

    0.10.3

    License

    MIT

    Unpacked Size

    13.1 kB

    Total Files

    8

    Last publish

    Collaborators

    • suiga