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 🙏

© 2025 – Pkg Stats / Ryan Hefner

htmlforge

v0.0.18

Published

![ci](https://github.com/tlonny/htmlforge/actions/workflows/release.yml/badge.svg)

Downloads

387

Readme

HTMLForge

ci

A minimal, zero-dependency library for building fully-styled HTML in TypeScript/JavaScript.

Features

  • Zero dependencies.
  • Efficient and ergonomic inline styling (using de-duplicated dynamic classes).
  • Reusable "Component"-pattern for composing common UIs.

Quick Look

import { HTMLDocument, NodeElement, NodeText } from "htmlforge"

const html = new HTMLDocument()
html.attributeAdd("lang", "en-GB")
html.head.childAdd(
    new NodeElement("title").childAdd(
        new NodeText("Acme Title")
    )
)

html.body.childAdd(
    new NodeElement("div")
        .styleAdd("width", "100%")
        .styleAdd("background-color", "blue")
        .styleAdd("background-color", "red", { pseudoSelector: ":hover" })
        .childAdd(new NodeText("Hello world"))
)

const validHTML = html.toString()

Installation

Install the package from npm:

npm install htmlforge

Usage

HTML structure

An HTMLForge HTMLDocument instance is a tree of nodes. Nodes come in a few flavors:

  • NodeElement: represents tags (e.g., <div>, <span>), can hold attributes and inline styles, and can nest any other node as a child.
  • NodeText: holds plain text content that will be HTML-escaped.
  • NodeRaw: holds raw HTML without escaping.
  • NodeFragment: groups a collection of child nodes without introducing a wrapping element.

Creating an HTML document

Create a new HTML document using new HTMLDocument(), set attributes on the root <html> element via html.attributeAdd, and work directly with html.head and html.body to populate content. The constructor accepts optional parameters:

  • indentCount (default 4): number of spaces used for pretty-print indentation in toString().
  • signatureDisplay (default true): whether to include the <!-- Generated by HTMLForge --> signature comment.
import { HTMLDocument, NodeElement, NodeText } from "htmlforge"

const html = new HTMLDocument({ indentCount: 2, signatureDisplay: false })
    .attributeAdd("lang", "en")
    .attributeAdd("data-theme", "dark")

html.body
    .styleAdd("margin", "auto")
    .childAdd(new NodeText("Hello world"))

NodeElement nodes

NodeElement supports:

  • attributeAdd(name, value) for HTML attributes
  • styleAdd(property, value, options?) for inline styles (with optional pseudoSelector, mediaQuery parameters)
  • childAdd(node) to nest children nodes

These calls are chainable to keep element construction compact.

import { NodeElement, NodeText } from "htmlforge"

const card = new NodeElement("section")
    .attributeAdd("aria-label", "profile card")
    .styleAdd("border", "1px solid #ccc")
    .childAdd(
        new NodeElement("h2").childAdd(new NodeText("Ada Lovelace"))
    )
    .childAdd(
        new NodeElement("p")
            .styleAdd("color", "#555")
            .childAdd(new NodeText("First computer programmer."))
    )

NodeFragment nodes

NodeFragment groups child nodes without adding a wrapper element. It only supports childAdd (also chainable).

import { NodeFragment, NodeElement, NodeText } from "htmlforge"

const listItems = new NodeFragment()
    .childAdd(new NodeElement("li").childAdd(new NodeText("One")))
    .childAdd(new NodeElement("li").childAdd(new NodeText("Two")))
    .childAdd(new NodeElement("li").childAdd(new NodeText("Three")))

Text and Raw nodes

  • NodeText holds HTML-escaped text content (no additional methods).
  • NodeRaw injects raw HTML as-is (no additional methods).
import { NodeText, NodeRaw } from "htmlforge"

const safeText = new NodeText("<em>Escaped</em> output")
const rawHtml = new NodeRaw("<em>Unescaped</em> output")

Define your own nodes

Implement the INode interface to build reusable components. Compose a private NodeElement (style/shape it however you like) and proxy its build() method. Anything that implements INode can be passed to childAdd on NodeElement or NodeFragment.

import type { INode } from "htmlforge"
import { NodeElement, NodeText } from "htmlforge"

class Alert implements INode {
    private readonly el = new NodeElement("div")
        .attributeAdd("role", "alert")
        .styleAdd("padding", "12px 16px")
        .styleAdd("background-color", "#fffae6")

    constructor(message: string) {
        this.el.childAdd(new NodeText(message))
    }

    // Optional: expose childAdd to let callers inject arbitrary child nodes
    childAdd(child: INode) {
        this.el.childAdd(child)
        return this
    }

    build() {
        return this.el.build()
    }
}