Git Product home page Git Product logo

hast-util-to-estree's Introduction

hast-util-to-estree

Build Coverage Downloads Size Sponsors Backers Chat

hast utility to transform a tree to estree JSX.

Install

npm:

npm install hast-util-to-estree

Use

Say we have the following HTML file, example.html:

<!doctype html>
<html lang=en>
<title>Hi!</title>
<link rel=stylesheet href=index.css>
<h1>Hello, world!</h1>
<a download style="width:1;height:10px"></a>
<!--commentz-->
<svg xmlns="http://www.w3.org/2000/svg">
  <title>SVG `&lt;ellipse&gt;` element</title>
  <ellipse
    cx="120"
    cy="70"
    rx="100"
    ry="50"
  />
</svg>
<script src="index.js"></script>

And our script, example.js, is:

var fs = require('fs')
var parse5 = require('parse5')
var fromParse5 = require('hast-util-from-parse5')
var toEstree = require('hast-util-to-estree')
var recast = require('recast')

var hast = fromParse5(parse5.parse(String(fs.readFileSync('example.html'))))

var estree = toEstree(hast)

estree.comments = null // `recast` doesn’t like comments on the root.
console.log(recast.prettyPrint(estree).code)

Now, node example (and prettier), yields:

;<>
  <html lang="en">
    <head>
      <title>{'Hi!'}</title>
      {'\n'}
      <link rel="stylesheet" href="index.css" />
      {'\n'}
    </head>
    <body>
      <h1>{'Hello, world!'}</h1>
      {'\n'}
      <a
        download
        style={{
          width: '1',
          height: '10px'
        }}
      />
      {'\n'}
      {/*commentz*/}
      {'\n'}
      <svg xmlns="http://www.w3.org/2000/svg">
        {'\n  '}
        <title>{'SVG `<ellipse>` element'}</title>
        {'\n  '}
        <ellipse cx="120" cy="70" rx="100" ry="50" />
        {'\n'}
      </svg>
      {'\n'}
      <script src="index.js" />
      {'\n'}
    </body>
  </html>
</>

API

toEstree(tree, options?)

Transform a hast tree to an estree (JSX).

options
  • space (enum, 'svg' or 'html', default: 'html') — Whether node is in the 'html' or 'svg' space. If an svg element is found when inside the HTML space, toEstree automatically switches to the SVG space when entering the element, and switches back when exiting
Returns

estree — a Program node, whose last child in body is most likely an ExpressionStatement whose expression is a JSXFragment or a JSXElement.

Typically, there is only one node in body, however, this utility also supports embedded MDX nodes in the HTML (when mdast-util-mdx is used with mdast to parse markdown before passing its nodes through to hast). When MDX ESM import/exports are used, those nodes are added before the fragment or element in body.

Note

Comments are both attached to the tree in their neighbouring nodes (recast and babel style), and added as a comments array on the program node (espree style). You may have to do program.comments = null for certain compilers.

There aren’t many great estree serializers out there that support JSX. recast does a fine job. Or use estree-util-build-jsx to turn JSX into function calls and then serialize with whatever (astring, escodegen).

Security

You’re working with JavaScript. It’s not safe.

Related

Contribute

See contributing.md in syntax-tree/.github for ways to get started. See support.md for ways to get help.

This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.

License

MIT © Titus Wormer

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.