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-types

v3.1.0

Published

Small lib of template tags, classes, and escaping functions for working with sync or async html.

Downloads

23

Readme

Html Types

Provides a number of small tools for working with HTML:

  1. Protocol definitions for objects that contain html data (or streams of html data)
  2. Simple classes that implement those protocols
  3. Template tag functions for generating html, with nesting and encoding of string data built in.
  4. Functions for generating safe HTML from javascript values
import html, { Html, isHtml, encode } from 'html-types';
import { type HtmlObject, type HtmlTemplateVariable } from 'html-types';

// 1. Protocols & typedefs:
const posts = [{
	title: 'My blog'
	body: {
		value: '<b>Welcome</b> to my blog!'
	}
},{
	title: 'I <3 my dog'
	body: {
		value: 'I really love my dog!'
	} as HtmlObject
}]

// 2. Classes & utilities:
const author = new Html('By <a href="http://paul.example/">Paul</a>');
isHtml(author) // True

// 3. Template tags:
const page = html`
	<h1>Paul's Blog</h1>
	${posts.map(post=>html`
		<h2>${post.title}</h2>
		<i>${author}</i>
		${post.body}
	`)}
`;

// 4. Source stringifiers:
console.log(encode(page));

More details

This library helps you work with HTML in a type-safe way, by creating a differentiation between HTML and non-html strings.

To make use of it, you just need to call one of the stringify functions whenever you need to output HTML:

import html from 'html-types';
import fs from 'fs';

console.log(html.encode('Solve the equation 4x + y > 5'));
// Output: Solve the equation 4x + y &gt; 5

These functions will output your string with the following values escaped:

| Char | Replacement | |---|---| | & | &amp; | | < | &lt; | | > | &gt; | | " | &quot; | | \ | &#39; | | / | &#x2F; | | ` | &#x60; | | = | &#x3D |

The stringify functions also accept other value types; null or undefined won't appear in the output, arrays and other iterables will be flattened (concatenated) by encode.

Templates

Often you'll want to mix html and non-html values in the same line of code. You can do this with the html template tag:

import html from 'html-types';

const title = `The <h1> Tag`
const body = html`This is a <i>super</i> useful tag!`

// The title will be escaped because it's a string,
// but the body won't because we've created it with the html tag
const source = html`<article>
	<h1>${title}</h1>

	${body}
</article>`;

// The source is an HTML type, so the only thing that will be
// escaped will be the "<h1>" from the title.
console.log(html.encode(source));

The html tag creates a special data type that encode understands as HTML content, which it won't escape. It will, however, escape nested values using the same rules as above - including outputing nested html templates verbatim.

The class: Html

So what if you want to create this data type without using a template? We provide a class for this. Html represents a chunk of HTML data:

import html, { Html, type HtmlTemplateVariable } from 'html-types';
import fs from 'fs';

const header = new Html(fs.readFileSync('./header.html'));

export default (content: HtmlTemplateVariable) => html`
<body>
	<header>
	${header}
	</header>
	<main>
	${content}
	</main>
`

The protocol: implementing your own html objects

The Html class is just a simple implementation of a protocols defined by this package. You can create your own implementations of objects that can be represented as HTML by implementing the protocol.

An object representing an html chunk must have the property value, which is a getter that returns a string. If you need to differentiate this from other objects that have a value property, you may give the object a type property with the string value 'text/html'.

Utilities

attrs

Use this function to generate HTML element attributes. This function takes a dict of key-value pairs and outputs an array of Html objects:


const input = html`<input${html.attrs({
	name: 'enabled',
	value: '231',
	checked: true,
	readonly: false,
	class: null,
})}>`;
// Output: <input name="enabled" value="231" checked>