1.0.0 • Published 8 months ago

html-to-obj-mk v1.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
8 months ago

HTML to Object Parser

This JavaScript library allows you to parse HTML and convert it into a structured JavaScript object. It's designed to work both in Node.js and in the browser environment.

Installation

You can install this library via npm:

npm install html-to-object-parser
## Usage

### In Node.js

```javascript
const fs = require("fs");
const parseHTML = require("html-to-object-parser");

const fileName = process.argv[2];

if (!fileName) {
  console.error("Please provide an HTML file as an argument.");
  process.exit(1);
}

fs.readFile(fileName, "utf8", (err, data) => {
  if (err) {
    console.error(`Error reading file: ${err.message}`);
    process.exit(1);
  }

  const htmlObject = parseHTML(data);
  console.log(JSON.stringify(htmlObject, null, 2));
});
```

### In the Browser

```html
<!DOCTYPE html>
<html>
  <head>
    <script src="htmlToObj.js"></script>
  </head>
  <body>
    <div style="background-color: yellow; font-size: 14px" id="first-div">
      Hello, friends
      <p class="para" style="font-family: monospace; font-size: 11px">
        Lorem ipsum dolor sit
      </p>
      <footer style="width: auto; height: 100px; color: blue">
        <span> This is the end </span>
      </footer>
    </div>

    <script>
      const htmlString = document.querySelector("#first-div").outerHTML;
      const htmlObject = parseHTML(htmlString);
      console.log(JSON.stringify(htmlObject, null, 2));
    </script>
  </body>
</html>
```

## API

### `parseHTML(html)`

This function parses the given HTML string and returns an object representing the HTML structure.

- `html`: The HTML string to parse.

### `parseElement(element, Node)`

This function recursively parses an HTML element into an object. It's used internally by `parseHTML`.

- `element`: The HTML element to parse.
- `Node`: The Node object from the window environment.

### `parseStyle(style)`

This function parses the CSS style of an HTML element into an object. It's used internally by `parseElement`.

- `style`: The CSS style of an HTML element.
1.0.0

8 months ago