HAST Utility to Reparse Raw HTML Nodes

9.1.0 · active · verified Sun Apr 19

This package, `hast-util-raw`, provides a utility for re-parsing HAST (Hypertext Abstract Syntax Tree) documents, specifically targeting 'raw' nodes which contain unprocessed HTML strings. Leveraging the `parse5` HTML parser, it transforms these raw HTML strings into a proper HAST syntax tree, crucial for applications that require a fully-formed, inspectable, and transformable tree rather than mere HTML serialization. This is particularly vital when working with markdown that includes embedded HTML (often enabled via `allowDangerousHtml: true` in converters like `mdast-util-to-hast`), where the initial conversion preserves the HTML as raw strings. The utility ensures that all original data and positional information from the source HTML are meticulously retained during the reparsing process. Currently in stable version 9.1.0, the library maintains an active release cadence, frequently delivering bug fixes and minor features, with major versions introducing breaking changes, often related to Node.js version support or API enhancements. Its core differentiator lies in enabling robust processing of mixed markdown/HTML content by integrating raw HTML into the HAST ecosystem as manipulable nodes, supporting various output formats beyond simple HTML, such as React or MDX.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates processing a HAST tree containing a `type: 'raw'` node with an HTML string, converting it into a fully structured HAST tree with parsed elements.

import { h } from 'hastscript';
import { raw } from 'hast-util-raw';

// A HAST tree typically containing a 'raw' node.
// In real-world scenarios, this 'treeWithRaw' often comes from a markdown-to-hast
// converter like `mdast-util-to-hast` with `allowDangerousHtml: true`.
const treeWithRaw = {
  type: 'root',
  children: [
    h('header', [h('h1', 'Document Title')]),
    // This 'raw' node contains unparsed HTML string.
    // hast-util-raw will parse this string into proper HAST elements.
    {
      type: 'raw',
      value: '<p>This is <em>some</em> <strong>raw</strong> HTML content.</p><p>Another paragraph.</p>'
    },
    h('footer', [h('p', 'End of document.')])
  ]
};

// The `raw` function processes the tree, converting 'raw' nodes to full HAST elements.
const reformattedTree = raw(treeWithRaw);

console.log(JSON.stringify(reformattedTree, null, 2));

view raw JSON →