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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@levischuck/tiny-html

v0.1.1

Published

A minimal, dependency-free TypeScript HTML parser and renderer that is easily compatible with react, hono, preact.

Readme

tiny-html

A minimal, dependency-free TypeScript HTML parser and renderer that is easily compatible with react, hono, preact.

Usage

Basic Parsing and Rendering

import { readHtml, writeHtml, awaitHtmlNode } from '@levischuck/tiny-html';

// Parse HTML into HtmlNode structure
const result = readHtml('<div class="container"><h1>Hello</h1></div>');
console.log(result.node); // HtmlNode structure
console.log(result.doctype); // DOCTYPE if present
console.log(result.xml); // XML declaration if present

// Render HtmlNode back to HTML
const html = writeHtml(result);
// Output: <div className="container"><h1>Hello</h1></div>

// Render with options
const xhtml = writeHtml(result, { voidTrailingSlash: true });

// You can also render HtmlNode directly (without parsing first)
const node = { type: 'div', props: { children: 'Hello World' } };
const output = writeHtml(node);
// Output: <div>Hello World</div>

// Handle async HtmlNode trees (with Promise children)
const asyncNode = {
  type: 'div',
  props: {
    children: [
      Promise.resolve({ type: 'span', props: { children: 'Async content' } }),
      'Regular content'
    ]
  }
};
const resolved = await awaitHtmlNode(asyncNode);
const asyncHtml = writeHtml(resolved);

Converting to Framework Elements

Use htmlNodeTo to convert HtmlNode structures to React, Preact, or other framework elements:

import { createElement } from 'react';
import type { ReactNode } from 'react';
import { readHtml, htmlNodeTo } from '@levischuck/tiny-html';

const result = readHtml('<div><span>Hello React!</span></div>');
const reactElement: ReactNode = htmlNodeTo(result.node, createElement);
// Now you can render reactElement in your React component

This package provides a simple HTML parser and renderer with a structure compatible with React elements, making it easy to integrate with React-based rendering pipelines.

API

readHtml(html: string): ParseResult

Parses an HTML string into a ParseResult containing HtmlNode elements.

Returns:

interface ParseResult {
  xml?: string;           // XML declaration if present
  doctype?: string;       // DOCTYPE declaration if present
  node: HtmlNode | HtmlNode[];  // Parsed content as HtmlNode
}

writeHtml(input: HtmlNode | ParseResult, options?: WriterOptions): string

Renders a HtmlNode or ParseResult back to an HTML string.

Parameters:

  • input: HtmlNode | ParseResult - The content to render
  • options?: WriterOptions - Rendering options (see WriterOptions below)

Returns: HTML string

awaitHtmlNode(node: HtmlNode | Promise<HtmlNode>): Promise<HtmlNode>

Recursively awaits all Promise children in a HtmlNode tree. Useful for handling async content.

Parameters:

  • node: HtmlNode | Promise<HtmlNode> - The node tree to resolve

Returns: A new HtmlNode with all promises resolved

Note: Incompatible types (functions, symbols) are dropped during resolution.

htmlNodeTo<T>(node: HtmlNode, createElement: CreateElementFn<T>): T | null

Generic converter that transforms HtmlNode structures into any element type using a provided createElement function.

Parameters:

  • node: HtmlNode - The HtmlNode to convert
  • createElement: CreateElementFn<T> - Function that creates elements of type T
    • Signature: (type: string, props: CreateElementProps, ...children: T[]) => T

Returns: Converted element of type T, or null

Example with React:

import { createElement } from 'react';
import type { ReactNode } from 'react';
import { readHtml, htmlNodeTo } from '@levischuck/tiny-html';

const result = readHtml('<div><span>Hello</span></div>');
const reactElement = htmlNodeTo<ReactNode>(result.node, createElement);

Example with Preact:

import { h } from 'preact';
import { readHtml, htmlNodeTo } from '@levischuck/tiny-html';

const result = readHtml('<div><span>Hello</span></div>');
const preactElement = htmlNodeTo(result.node, h);

Types

HtmlNode

The core type representing parsed HTML content:

  • HTML element: { type: string, props: HtmlProps }
  • Primitives: string, number, boolean, null, undefined, bigint
  • Arrays: HtmlNode[]

HtmlElement

interface HtmlElement {
  type: string;
  props: HtmlProps;
}

HtmlProps

interface HtmlProps {
  [key: string]: string | number | boolean | HtmlStyle | HtmlNode | Promise<HtmlNode>;
  children?: HtmlNode | Promise<HtmlNode>;
}

HtmlStyle

interface HtmlStyle {
  [key: string]: string;
}

CreateElementFn<T>

Type for createElement functions used with htmlNodeTo.

type CreateElementFn<T> = (
  type: string,
  props: CreateElementProps,
  ...children: T[]
) => T;

CreateElementProps

Props object passed to createElement functions.

type CreateElementProps = Record<string, any>;

ParseResult

interface ParseResult {
  xml?: string;
  doctype?: string;
  node: HtmlNode | HtmlNode[];
}

WriterOptions

Options for controlling HTML output formatting.

interface WriterOptions {
  useCDataForScripts?: boolean;  // Wrap <script> content in CDATA
  useCDataForStyles?: boolean;   // Wrap <style> content in CDATA
  xml?: string;                  // XML declaration to prepend
  doctype?: string;              // DOCTYPE declaration to prepend
  voidTrailingSlash?: boolean;   // Add trailing slash to void elements (e.g., <br />)
}

Options:

  • useCDataForScripts?: boolean - When true, wraps <script> content in CDATA sections. Useful for XHTML output. Default: false
  • useCDataForStyles?: boolean - When true, wraps <style> content in CDATA sections. Useful for XHTML output. Default: false
  • xml?: string - XML declaration to prepend to the output (e.g., <?xml version="1.0" encoding="UTF-8"?>)
  • doctype?: string - DOCTYPE declaration to prepend to the output (e.g., <!DOCTYPE html>)
  • voidTrailingSlash?: boolean - When true, adds trailing slash to void elements like <br/>, <img/>. Useful for XHTML compatibility. Default: false

Why use this?

There are many other great libraries to parse HTML. I need to process things on the edge and I can rely on this being fast and available on every platform I want to use it. Also.. I'm tired of writing little parsers here and there just to put them into a hono-jsx like format for post-processing.

License

MIT Licensed.