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

@qnc/fragmake

v1.1.0

Published

HTML templating which supports pre-defined (native) Nodes

Readme

fragmake

A small templating utility for constructing DocumentFragments from a mix of html, content-to-be-escaped, and already-constructed Nodes. Uses tagged template literals, and all expressions are auto-escaped.

It's useful when you don't want to set up a full "component framework" (eg. react, vue, lit, etc.), or when you want to "inject" already-existing nodes/elements.

Node Injection

Template expressions include any type of Node. Those nodes get injected directly (maintaining event listeners and other properties) into the generated fragment. This makes it easy to use already-constructed DOM Nodes, regardless of what pattern or framework you used to create them.

const button = document.createElement('button')
button.onclick = ...
fragmake`<p>${button}</p>`.firstChild.firstChild == button // true

Note that while you can put placeholders anywhere in your template literal (including tag names, attribute names, attribute values), expressions containing Nodes can ONLY be located where it's legal to have an HTMLTemplateElement.

Basic usage

import {fragmake, FragmakeRenderable} from '@qnc/fragmake';

function profile_controls(username: string, log_out_button: HTMLElement, wrapper_class: string): DocumentFragment {
    return fragmake`
        <div class=${wrapper_class}>
            Logged in as ${username}. ${log_out_button}
        </div>
    `
}

fragmake

Our primary function. Intended for use with tagged template literals.

Produces a DocumentFragment from a template literal.

The template literal must represent BALANCED/COMPLETE html tags. If you give us unbalanced html, our behaviour is UNSPECIFIED.

If you give us invalid html, our behaviour is unspecified.

Template expressions can be strings, numbers, Nodes, or arrays (potentially nested) thereof. Strings and numbers will be escaped. Nodes will be injected directly into the resultant fragment.

A "simple" expression is one which contains only strings, numbers, or arrays (potentially nested) thereof. Ie, they contain no Nodes. Simple expressions can be placed anywhere in your template literal, including:

  • within tag names
  • within attribute names
  • within attribute values
  • as top-level or element content

Expressions containing Nodes, however, can only be placed where it's legal to have an HTMLTemplateElement (they will be replaced with a string representing a template element before we have the browser parse the html).

html_to_fragment

export function html_to_fragment(html: string): DocumentFragment

This is used internally by fragmake, but users might find it useful when "trusting" known-safe html.

Examples

import {fragmake, FragmakeRenderable} from '@qnc/fragmake';

// Static parts aren't escaped
console.log(fragmake`<i></i>`) // <i></i>

// You can insert regular characters
console.log(fragmake`<i>${"apple"}</i>`) // <i>apple</i>

// You can insert numbers
console.log(fragmake`<i>${1}</i>`)  // <i>1</i>

// You can insert arrays
console.log(fragmake`<i>${[1, 2, 3]}</i>`)  // <i>123</i>

// Special characters are escaped
console.log(
    fragmake`<i data-foo="${`'"`}">${"<>&"}</i>`
) // "<i>&lt;&gt;&quot;&#039;&amp;</i>",

// it's composable
const inner = fragmake`<i>${3}</i>`;
console.log(fragmake`<b>${inner}</b>`)  // <b><i>3</i></b>

// You can put expressions in attributes
console.log(
    fragmake`<a href="mailto:${'"John Doe" <[email protected]>'}">email John Doe</a>`
)   // <a href="mailto:&quot;John Doe&quot; &lt;[email protected]&gt;">email John Doe</a>

// You can inject existing nodes
const button: HTMLButtonElement = make_custom_button()
fragmake`<p>Hello! ${button}</p>`.firstChild.lastChild == button    // true

// Slightly more complex case
const users = [
    { first_name: "Alex", last_name: "Fischer" },
    { first_name: "John", last_name: "Doe" },
    { first_name: "Jane", last_name: "Doe" },
];
console.log(
    fragmake`
        <ul>
            ${users.map((u) => fragmake`<li>${u.first_name} ${u.last_name}</li>`)}
        </ul>
    `
)   
/*
    <ul>
        <li>Alex Fischer</li><li>John Doe</li><li>Jane Doe</li>
    </ul>
*/