What is HAST (Hypertext Abstract Syntax Tree)?
mainunified and rehype), the format is language-agnostic.repository·main·Indexed 20 days ago
https://github.com/syntax-tree/hastA specification for representing HTML, SVG, and MathML as an abstract syntax tree. Designed as a lightweight, transformable 'virtual DOM' compatible with the unist ecosystem, hast provides a language-agnostic format for representing documents, elements, text, comments, and doctypes. It includes rules for property name camelcasing and value type reflection, and is supported by a wide range of utilities for parsing, transformation, and sanitization via the hast-util- ecosystem.
unified and rehype), the format is language-agnostic.Property values in HAST reflect the data type determined by the property name.
hidden are reflected as hidden: true.minlength="5" are reflected as minLength: 5.<div class="alpha bravo"> becomes properties: { className: ['alpha', 'bravo'] }.<div hidden="no"> is reflected as hidden: 'no' rather than being coerced to true.null or undefined values should be treated as if the property was not included.HAST provides a 'virtual' DOM designed to be lean, easy to transform, and capable of representing the entirety of HTML syntax (including comments and doctypes) while providing positional information.
value: string.children (which can be Comment, Doctype, Element, or Text).content of a <template> element, but it cannot be a child of another node.tagName (local name), properties (attributes), and children. If the tagName is 'template', it may also have a content field of type Root.<!doctype> declaration.HAST transforms HTML attribute names into camelcased property names to align with how attributes are reflected in the DOM.
Transformation Rules:
stroke-miterlimit) become camelcased (strokeMiterLimit).read-only) become camelcased (readOnly).itemid becomes itemId).Notable Exceptions:
class becomes className.for becomes htmlFor.allowfullscreen becomes allowFullScreen.autoplay becomes autoPlay.autocomplete becomes autoComplete.Note: For a complete list of transformations, use hastscript or the property-information package.
If you are using TypeScript, you can install the official type definitions for hast using npm.
npm install @types/hastBecause hast represents HTML, improper use can lead to Cross-Site Scripting (XSS) attacks. When dealing with user-provided input, you must ensure the tree is safe before processing or rendering it.
Use the hast-util-sanitize utility to sanitize nodes and mitigate security risks.
Text and comments are represented as nodes with a value property.
Text Example:
<span>Foxtrot</span> yields:
{
type: 'element',
tagName: 'span',
properties: {},
children: [{type: 'text', value: 'Foxtrot'}]
}Comment Example:
<!--Charlie--> yields:
{type: 'comment', value: 'Charlie'}When converting HTML to HAST, elements are mapped to objects containing their tag name, properties, and children.
Example: <a href="https://alpha.com" class="bravo" download></a> becomes:
{
type: 'element',
tagName: 'a',
properties: {
href: 'https://alpha.com',
className: ['bravo'],
download: true
},
children: []
}Beyond the core hast-util-* packages, there is a collection of specialized HTML-related utilities for working with specific standards and attributes:
html-tag-names, mathml-tag-names, svg-tag-names, html-void-elements.aria-attributes, html-element-attributes, svg-element-attributes, property-information.comma-separated-tokens, space-separated-tokens.web-namespaces, html-encodings, html-dangerous-encodings.a-rel, link-rel, meta-name.html-event-attributes, svg-event-attributes.The hast ecosystem provides a wide range of utilities for manipulating, parsing, and transforming HTML Abstract Syntax Trees. These utilities are typically prefixed with hast-util- (except for hastscript).
Key categories of utilities include:
hastscript (create trees), hast-util-from-html (parse HTML), hast-util-from-selector (parse CSS selectors to nodes).hast-util-to-dom (to DOM), hast-util-to-html (to HTML string), hast-util-to-jsx (to JSX), hast-util-to-mdast (to Markdown AST).hast-util-is-element (check if node is an element), hast-util-is-javascript (check if node is a script), hast-util-phrasing (check if node is phrasing content).hast-util-class-list (simulate classList), hast-util-find-and-replace (find/replace text), hast-util-sanitize (sanitize nodes).hast-util-select (provides querySelector, querySelectorAll, and matches).