node-html-to-object
TypeScript icon, indicating that this package has built-in type declarations

1.0.4 • Public • Published

html to object

This library was written to convert HTML string to JSON/JS Object.

Example

From

<div>
    <div>
        <p>First paragraph</p>
        <p>Second paragraph</p>
        <p class="my-class" id="my-id" data-custom="custom-data">
            Hello, world!
        </p>
    </div>
</div>

To

{
    "tag": "parse-root",
    "children": [
        {
            "tag": "div",
            "children": [
                {
                    "tag": "div",
                    "children": [
                        {
                            "tag": "p",
                            "children": [
                                {
                                    "type": "text",
                                    "text": "First paragraph"
                                }
                            ],
                            "closed": true,
                            "type": "Element",
                            "attributes": []
                        },
                        {
                            "tag": "p",
                            "children": [
                                {
                                    "type": "text",
                                    "text": "Second paragraph"
                                }
                            ],
                            "closed": true,
                            "type": "Element",
                            "attributes": []
                        },
                        {
                            "tag": "p",
                            "children": [
                                {
                                    "type": "text",
                                    "text": "Hello, world!"
                                }
                            ],
                            "closed": true,
                            "type": "Element",
                            "attributes": [
                                {
                                    "name": "class",
                                    "value": "my-class"
                                },
                                {
                                    "name": "id",
                                    "value": "my-id"
                                },
                                {
                                    "name": "data-custom",
                                    "value": "custom-data"
                                }
                            ]
                        }
                    ],
                    "closed": true,
                    "type": "Element",
                    "attributes": []
                }
            ],
            "closed": true,
            "type": "Element",
            "attributes": []
        }
    ],
    "closed": false,
    "attributes": [],
    "type": "Element"
}

Importing the module

// ES6 / TypeScript
import Parser from "node-html-to-object";

// CommonJS
const { Parser } = require("node-html-to-object");

Using the module

// input data
const html = '<div><div><p>First paragraph</p><p>Second paragraph</p><p class="my-class" id="my-id" data-custom="custom-data">Hello, world!</p></div></div>';

// conversion
const result = Parser.parse(html);

Package Sidebar

Install

npm i node-html-to-object

Weekly Downloads

4

Version

1.0.4

License

GPL-3.0

Unpacked Size

47.2 kB

Total Files

9

Last publish

Collaborators

  • roteklaue