HAST Interactive Content Utility

3.0.0 · active · verified Sun Apr 19

hast-util-interactive is a focused utility within the unified ecosystem, specifically designed to determine if a given hast (Hypertext Abstract Syntax Tree) node constitutes "interactive content" according to the HTML specification. This package is particularly useful for tools that analyze or transform HTML content and need to identify elements like `<a>` with `href`, `<button>`, `<input>`, or `<video controls>` to enforce accessibility rules, validate content, or apply specific styling. The current stable version is 3.0.0. Maintained by the syntax-tree collective, it follows a release cadence tied to Node.js LTS cycles and hast ecosystem updates, providing TypeScript types for enhanced developer experience. Its primary differentiator is its precise implementation of the HTML interactive content algorithm for hast nodes, offering a reliable predicate function rather than a full parsing or transformation engine.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to use the `interactive` function to check various hast nodes against the HTML specification for interactive content.

import { interactive } from 'hast-util-interactive';
import type { Element, Text, Root } from 'hast';

// Example 1: Non-interactive anchor (no href)
const nonInteractiveLink: Element = {
  type: 'element',
  tagName: 'a',
  properties: {},
  children: [{type: 'text', value: 'Non-clickable link'}]
};
console.log('Is nonInteractiveLink interactive?', interactive(nonInteractiveLink)); // => false

// Example 2: Interactive anchor (with href)
const interactiveLink: Element = {
  type: 'element',
  tagName: 'a',
  properties: {href: '#section'},
  children: [{type: 'text', value: 'Clickable link'}]
};
console.log('Is interactiveLink interactive?', interactive(interactiveLink)); // => true

// Example 3: Interactive button
const buttonElement: Element = {
  type: 'element',
  tagName: 'button',
  properties: {},
  children: [{type: 'text', value: 'Submit'}]
};
console.log('Is buttonElement interactive?', interactive(buttonElement)); // => true

// Example 4: Video with controls
const videoElement: Element = {
  type: 'element',
  tagName: 'video',
  properties: {controls: true, src: 'movie.mp4'},
  children: []
};
console.log('Is videoElement interactive?', interactive(videoElement)); // => true

// Example 5: Input field
const inputElement: Element = {
  type: 'element',
  tagName: 'input',
  properties: {type: 'text', value: 'Hello'},
  children: []
};
console.log('Is inputElement interactive?', interactive(inputElement)); // => true

view raw JSON →