Lightweight Hypertext Abstract Syntax Tree.
A lightweight version of hast v2.4 with a more compact format for serialization.
npm install --save lhast
# or
yarn add lhast
interface Node {
type: string
}
interface Parent {
children: Node[]
}
interface ParentOf<T extends Node[]> extends Parent {
children: T
}
interface Root extends Node, ParentOf<RootContent[]> {
type: 'root'
}
interface Element extends Node, ParentOf<ElementContent[]> {
type: 'element'
tagName: string
properties: Properties
}
interface Text extends Node {
type: 'text'
value: string
}
type RootContent =
| Element
| Text
type ElementContent =
| Element
| Text
type Properties = Record<string, boolean | number | string | string[]>
type Node =
| Root
| Element
| Text
type Root = RootContent[]
type Element = ElementWithProperties | ElementWithoutProperties
type ElementWithProperties = [
tagName: string
, properties: Properties
, ...children: ElementContent[]
]
type ElementWithoutProperties = [
tagName: string
, ...children: ElementContent[]
]
type Text = string
type RootContent =
| Element
| Text
type ElementContent =
| Element
| Text
type Properties = Record<string, boolean | number | string | string[]>
function parseDocument(html: string): LHAST.Root
function parseFragment(html: string): LHAST.Root
function compact(root: LHAST.Root): LHASTCompact.Root
function uncompact(root: LHAST.COMPACT.Root): LHAST.Root
function validateLHAST(data: unknown): void
function validateLHASTCompact(data: unknown): void
function isLHAST(data: unknown): data is LHAST.Root
function isLHASTCompact(data: unknown): data is LHASTCompact.Root
const LHASTSchema
const LHASTCompactSchema
import * as Builder from 'lhast/utils/builder'
Each lhast node has a corresponding builder.
import * as Is from 'lhast/utils/is'
Each lhast node has a corresponding is
function.
import { flatMap } from 'lhast/utils/flat-map'
function flatMap(
node: LHAST.Node
, fn: (node: LHAST.Node) => AST.Node[]
): LHAST.Node[]
import { map } from 'lhast/utils/map'
function map(
node: LHAST.Node
, fn: (node: LHAST.Node) => AST.Node
): LHAST.Node
import { filter } from 'lhast/utils/filter'
function filter(
node: LHAST.Node
, predicate: (node: LHAST.Node) => unknown
): LHAST.Node | undefined
import { find } from 'lhast/utils/find'
function find<T extends LHAST.Node>(
node: LHAST.Node
, predicate: (node: LHAST.Node) => boolean
): T | undefined
import { findAll } from 'lhast/utils/find-all'
function* findAll<T extends LHAST.Node>(
node: LHAST.Node
, predicate: (node: LHAST.Node) => boolean
): Iterable<T>
import { traverseDescendantNodes } from 'lhast/utils/traverse-descendant-nodes'
function traverseDescendantNodes(node: LHAST.Node): Iterable<LHAST.Node>
import { reverse } from 'lhast/utils/reverse'
function reverse(root: LHAST.Root): LHAST.Root
import { addHelpers, addHelpersInPlace, NodeWithHelpers } from 'lhast/utils/add-helpers'
type NullOrNodeWithHelpers<T extends LHAST.Node | null> =
T extends null
? null
: NodeWithHelpers<NonNullable<T>>
type NodeWithHelpers<
Node extends LHAST.Node
, Sibling extends LHAST.Node | null = AST.Node | null
, Parent extends LHAST.Node | null = AST.Node | null
> =
Node extends LHAST.Root
? Mixin<Node, {
id: string
parent: null
index: null
previousSibling: null
nextSibling: null
children: Array<NodeWithHelpers<LHAST.RootContent, AST.RootContent, AST.Root>>
}>
: Node extends LHAST.Element
? Mixin<Node, {
id: string
parent: NullOrNodeWithHelpers<Parent>
index: number
previousSibling: NullOrNodeWithHelpers<Sibling>
nextSibling: NullOrNodeWithHelpers<Sibling>
children: Array<
NodeWithHelpers<
LHAST.ElementContent
, LHAST.ElementContent
, LHAST.Element
>
>
}>
: Mixin<Node, {
id: string
parent: NullOrNodeWithHelpers<Parent>
index: number | null
previousSibling: NullOrNodeWithHelpers<Sibling>
nextSibling: NullOrNodeWithHelpers<Sibling>
}>
function addHelpers<T extends LHAST.Node>(node: T): NodeWithHelpers<T>
function addHelpersInPlace<T extends LHAST.Node>(node: T): NodeWithHelpers<T>
import { removeHelpers, removeHelpersInPlace } from 'lhast/utils/remove-helpers'
function removeHelpers<T extends LHAST.Node>(node: NodeWithHelpers<T>): T
function removeHelpersInPlace<T extends LHAST.Node>(node: NodeWithHelpers<T>): T
import { withHelpers, withHelpersInPlace } from 'lhast/utils/with-helpers'
function withHelpers<T extends AST.Node, U>(
node: T
, fn: (node: NodeWithHelpers<T>) => U
): U
function withHelpersInPlace<T extends AST.Node, U>(
node: T
, fn: (node: NodeWithHelpers<T>) => U
): U