@rustle/html-parse
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

@rustle/html-parse

NPM version

Simple html parser compatible with single tags without /.

Usage

import { parse, evaluate } from '@rustle/html-parse';

const built = parse(`
  <div>
    <br>
    <a href="xxx"/>
  </div>
`)

const ast = evaluate(built, (tag, props, ...children) => {
  return { tag, props, children };
})

console.log(ast); // [{ tag: 'div', props: null, children: [...] }]
// Get all `a` tags.
const a = [];

evaluate(built, (tag, props, ...children) => {
  if (tag === 'a') {
    a.push({ tag, props, children });
  }
})

CDN

<!DOCTYPE html>
<html lang='en'>
<body>
  <script src='https://unpkg.com/@rustle/html-parse/dist/htmlParse.umd.js'></script>
  <script>
    const { parse, evaluate } = FetchNpmPackage;
    // ...
  </script>
</body>
</html>

Caveats

  1. The special tag is marked with uppercase, because the html tags are all in lowercase, to avoid conflicts, the special tag is marked with uppercase.

    • comment node: COMMENT.
  2. For the wrong html syntax, it is impossible to achieve the same behavior as the browser. To be precise, this is more similar to an xml parser, so write standard html syntax as much as possible.

Readme

Keywords

Package Sidebar

Install

npm i @rustle/html-parse

Weekly Downloads

0

Version

1.0.0

License

MIT

Unpacked Size

21.5 kB

Total Files

7

Last publish

Collaborators

  • rustle