npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

html-to-json-rs

v0.0.6

Published

Package for converting HTML strings to JSON and vice versa using Rust and WebAssembly.

Downloads

174

Readme

HtmlToJson

Description

HtmlToJson is a package designed for converting HTML strings to JSON and vice versa using Rust and WebAssembly. While the package is relatively raw, it gets the job done.

The creation of this package was made possible by utilizing the HtmlEditor library.

Getting Started

Installation

To use HtmlToJson in your project, you can install it using npm:

npm install html-to-json-rs

Usage

After installing the package, you can use it in your project by importing it into your JavaScript or TypeScript code:

import {init, jsonToHtml, htmlToJson, NODES} from 'html-to-json-rs';

const main = async () => {
    /**
     * First you need to call init
     * Init is an async function, the rest are normal
     */
    await init();
    
    // Constants of all node types, for check obj_type field
    console.log(NODES);
    
    // Example: Convert HTML to JSON
    const htmlString = '<p>Hello, </p><span>World!</span>';
    const jsonResult = htmlToJson(htmlString);
    console.log(jsonResult);
    
    // Example: Convert JSON to HTML
    const jsonObject = [
        { 
            obj_type: 'Element',
            name: 'p',
            attrs: [],
            children: [{ Text: 'World!'}]
        }
    ];
    
    const htmlResult = jsonToHtml(JSON.stringify(jsonObject));
    console.log(htmlResult);
} 

JSON structure

Result of calling function htmlToJson is a json string with array of JsonObj, if you want to render it by yourself below the definition of this struct. The JsonObj type represents the core structure of the generated JSON. It has the following fields:

  • obj_type (String): Indicates the type of the JSON object. Possible values include:

    • "Element": Represents an HTML element.
    • "Text": Represents text content.
    • "Comment": Represents a comment in the HTML.
    • "Doctype": Represents the document type declaration.
  • text (String): The content of the Text or Comment element.

  • name (String): The name of the HTML element. For elements, this corresponds to the tag name.

  • attrs (Array of Tuples): Represents the attributes associated with the HTML element. Each attribute is a tuple of key-value pairs.

  • children (Array of JsonObj): Contains child elements if the current object is an HTML element. It represents the nested structure of the HTML.

  • id (String): Tag id, also present in the attrs array (maybe in the future it will not be in attrs)

  • class (String): Tag classes, also present in the attrs array (maybe in the future it will not be in attrs)

Fields text, attrs, children, id, class - are optional, if id is not presented in html tag, id field will be undefined.

Here's an example JSON structure for a simple HTML document:

<p class="ql-align-center">
    Hello <span id="fav" style="color: rgb(230, 0, 0);">World</span>
</p>
[
  {
    "obj_type": "Element",
    "name": "p",
    "attrs": [
      [
        "class",
        "ql-align-center"
      ]
    ],
    "class": "ql-align-center",
    "children": [
      {
        "obj_type": "Text",
        "name": "Text",
        "text": "Hello ",
        "attrs": [],
        "children": []
      },
      {
        "obj_type": "Element",
        "name": "span",
        "attrs": [
          ["style", "color: rgb(230, 0, 0);"],
          ["id", "fav"]
        ],
        "id": "fav",
        "children": [
          {
            "obj_type": "Text",
            "name": "Text",
            "text": "World",
            "attrs": [],
            "children": []
          }
        ]
      }
    ]
  }
]