• Stars
    star
    521
  • Rank 84,952 (Top 2 %)
  • Language
    TypeScript
  • License
    Other
  • Created over 2 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

ultrahtml

A 1.75kB library for enhancing html. ultrahtml has zero dependencies and is compatible with any JavaScript runtime.

Features

  • Tiny, fault-tolerant and friendly HTML-like parser. Works with HTML, Astro, Vue, Svelte, and any other HTML-like syntax.
  • Built-in AST walk utility
  • Built-in transform utility for easy output manipulation
  • Automatic but configurable sanitization, see Sanitization
  • Handy html template utility
  • querySelector and querySelectorAll support using ultrahtml/selector

walk

The walk function provides full control over the AST. It can be used to scan for text, elements, components, or any other validation you might want to do.

Note > walk is async and must be awaited. Use walkSync if it is guaranteed there are no async components in the tree.

import { parse, walk, ELEMENT_NODE } from "ultrahtml";

const ast = parse(`<h1>Hello world!</h1>`);
await walk(ast, async (node) => {
  if (node.type === ELEMENT_NODE && node.name === "script") {
    throw new Error("Found a script!");
  }
});

walkSync

The walkSync function is identical to the walk function, but is synchronous. This should only be used when it is guaranteed there are no async components in the tree.

import { parse, walkSync, ELEMENT_NODE } from "ultrahtml";

const ast = parse(`<h1>Hello world!</h1>`);
walkSync(ast, (node) => {
  if (node.type === ELEMENT_NODE && node.name === "script") {
    throw new Error("Found a script!");
  }
});

render

The render function allows you to serialize an AST back into a string.

Note By default, render will sanitize your markup, removing any script tags. Pass { sanitize: false } to disable this behavior.

import { parse, render } from "ultrahtml";

const ast = parse(`<h1>Hello world!</h1>`);
const output = await render(ast);

transform

The transform function provides a straight-forward way to modify any markup. Sanitize content, swap in-place elements/Components, and more using a set of built-in transformers, or write your own custom transformer.

import { transform, html } from "ultrahtml";
import swap from "ultrahtml/transformers/swap";
import sanitize from "ultrahtml/transformers/sanitize";

const output = await transform(`<h1>Hello world!</h1>`, [
  swap({
    h1: "h2",
    h3: (props, children) => html`<h2 class="ultra">${children}</h2>`,
  }),
  sanitize({ allowElements: ["h1", "h2", "h3"] }),
]);

console.log(output); // <h2>Hello world!</h2>

Sanitization

ultrahtml/transformers/sanitize implements an extension of the HTML Sanitizer API.

Option Type Default Description
allowElements string[] undefined An array of strings indicating elements that the sanitizer should not remove. All elements not in the array will be dropped.
blockElements string[] undefined An array of strings indicating elements that the sanitizer should remove, but keep their child elements.
dropElements string[] ["script"] An array of strings indicating elements (including nested elements) that the sanitizer should remove.
allowAttributes Record<string, string[]> undefined An object where each key is the attribute name and the value is an Array of allowed tag names. Matching attributes will not be removed. All attributes that are not in the array will be dropped.
dropAttributes Record<string, string[]> undefined An object where each key is the attribute name and the value is an Array of dropped tag names. Matching attributes will be removed.
allowComponents boolean false A boolean value set to false (default) to remove components and their children. If set to true, components will be subject to built-in and custom configuration checks (and will be retained or dropped based on those checks).
allowCustomElements boolean false A boolean value set to false (default) to remove custom elements and their children. If set to true, custom elements will be subject to built-in and custom configuration checks (and will be retained or dropped based on those checks).
allowComments boolean false A boolean value set to false (default) to remove HTML comments. Set to true in order to keep comments.

Acknowledgements

More Repositories

1

clack

Effortlessly build beautiful command-line apps
TypeScript
5,170
star
2

astro-icon

Inline and sprite-based SVGs in Astro made easy!
MDX
1,072
star
3

microsite

Do more with less JavaScript. Microsite is a smarter, performance-obsessed static site generator powered by Preact and Snowpack.
TypeScript
877
star
4

natemoo-re

TypeScript
433
star
5

micromorph

A very tiny library for diffing DOM nodes
TypeScript
339
star
6

satori-html

An HTML adapter for Vercel's Satori
TypeScript
273
star
7

ultrafetch

RFC-7234 compliant caching for any `fetch` implementation
TypeScript
222
star
8

astro-remote

Render remote HTML or Markdown content in Astro with full control over the output
Astro
173
star
9

ultramatter

<1kB frontmatter parser that supports a reasonable subset of YAML
TypeScript
123
star
10

animate-presence

Effortless element entrance/exit animations
TypeScript
99
star
11

proload

Searches for and loads your tool's JavaScript configuration files with full support for CJS, ESM, TypeScript and more.
JavaScript
81
star
12

astro-capo

TypeScript
57
star
13

eslexer

Incredibly tiny, fast JS tokenizer
TypeScript
57
star
14

astro-php

Please don't use this in production
JavaScript
44
star
15

ultraflag

<1kB CLI flag parser
TypeScript
44
star
16

feed

Astro
43
star
17

magic-element

Automatic custom-elements with zero boilerplate
TypeScript
34
star
18

tslint-stencil

Contributes some helpful tslint rules for Stencil projects
TypeScript
22
star
19

tinyshell

A tiny `node:child_process` wrapper for shell scripts
TypeScript
21
star
20

konami-listener

Vanilla web component that listens for the Konami Code
JavaScript
20
star
21

ultramark

TypeScript
19
star
22

tsdx-monorepo

TypeScript
19
star
23

goldmark

JavaScript
19
star
24

project-template

TypeScript
19
star
25

tiny-decode

Decode HTML entities the browser-friendly way
JavaScript
18
star
26

prettier-plugin-style-order

Prettier plugin to automatically sort style declarations
JavaScript
16
star
27

stencil-forms

πŸ“πŸ€― Stencil component to automatically manage form state
TypeScript
14
star
28

miles-of-code

Astro
13
star
29

microsite-demo

TypeScript
11
star
30

stencil-tools

Editor enhancements and utilities for Stencil projects
TypeScript
10
star
31

emojex

Emoji-based Regular Expressions
TypeScript
10
star
32

astro-client-hints

TypeScript
9
star
33

astro-run

TypeScript
9
star
34

clack.cc

Astro
9
star
35

astro-form

Astro
7
star
36

raycast-microblog

TypeScript
7
star
37

topic

TypeScript
7
star
38

exo-store

TypeScript
6
star
39

astro-breadcrumbs

Astro
6
star
40

astro-beep

JavaScript
6
star
41

gamepad

Provides a simple, event-based API for Gamepads on the web
TypeScript
5
star
42

esm-create

npm init, but better
JavaScript
5
star
43

experiment--astro-editor

Astro
5
star
44

orbit

Astro
4
star
45

blocks

TypeScript
3
star
46

require-or-import

JavaScript
3
star
47

stencil-markdown

Stencil plugin that allows Markdown files to be imported as FunctionalComponents
TypeScript
3
star
48

a11y-types

Typescript Definitions for ARIA attribute constants
TypeScript
2
star
49

microstream

TypeScript
2
star
50

bottom-sheet

Bottom sheet web component
TypeScript
2
star
51

stencil-hooks

TypeScript
2
star
52

typescript-plugin-stencil

Typescript plugin for a smoother Developer Experience within Stencil projects
TypeScript
2
star
53

midu-astro

Astro
2
star
54

hibiscus

A *VERY ROUGH* proof of concept using Stencil to dynamically load and animate an SVG-based font.
TypeScript
1
star
55

astro-bundle-demo

JavaScript
1
star
56

node-shqxzf

Created with StackBlitz ⚑️
1
star
57

use-linked-context

JavaScript
1
star
58

pronouns

HTML
1
star
59

astro-playground

TypeScript
1
star
60

astro-dev-plugin-sample

Astro
1
star
61

actions-playground

Shell
1
star
62

test-social-web

1
star
63

astro-template-blog

1
star
64

astro-gh-pages-test

Astro
1
star
65

astro-minimal

JavaScript
1
star
66

microsite-templates

TypeScript
1
star
67

text-wrap-balance

1
star
68

node-wfhqwq

JavaScript
1
star
69

git-comments-test

1
star
70

astro-demo-named-slots

Astro
1
star
71

vscode-filename-snippets

TEST
TypeScript
1
star
72

astro-vercel-rewrites

Astro
1
star
73

action-test

TypeScript
1
star
74

bug--astro-netlify-libsql

TypeScript
1
star
75

natemoo-re.github.io

Public site
HTML
1
star