hastscript: Hyperscript for HAST Trees

9.0.1 · active · verified Sun Apr 19

hastscript is a utility library that provides a hyperscript interface for programmatically creating HTML Abstract Syntax Trees (hast trees). Analogous to React's `createElement` or Vue's `h` function, it simplifies the process of generating complex HTML structures in code by replacing repetitive object literal syntax with concise function calls. It also handles the normalization of attributes into the format required by the hast specification. The current stable version is 9.0.1, with frequent patch and minor releases and major versions typically introducing breaking changes like ESM-only support or Node.js version bumps. It is a core part of the unified ecosystem for processing content and specifically targets HTML, differentiating it from `unist-builder` (for generic unist nodes) or `xastscript` (for XML nodes).

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to create both HTML and SVG hast trees using the `h` and `s` functions with selectors, properties, and nested children.

import {h, s} from 'hastscript';

const htmlTree = h('.foo#some-id', [
  h('span', 'some text'),
  h('input', {type: 'text', value: 'foo'}),
  h('a.alpha', {class: 'bravo charlie', download: 'download'}, [
    'delta',
    'echo'
  ])
]);

const svgTree = s('svg', {viewbox: '0 0 500 500', xmlns: 'http://www.w3.org/2000/svg'}, [
  s('title', 'SVG `<circle>` element'),
  s('circle', {cx: 120, cy: 120, r: 100})
]);

console.log('HTML Tree:', JSON.stringify(htmlTree, null, 2));
console.log('SVG Tree:', JSON.stringify(svgTree, null, 2));

view raw JSON →