Lightweight Hypertext Abstract Syntax Tree
npm install lhastA lightweight version of [hast v2.4] with a more compact format for serialization.
[hast v2.4]: https://github.com/syntax-tree/hast/tree/2.4.0
sh
npm install --save lhast
or
yarn add lhast
`API
$3
`ts
interface Node {
type: string
}interface Parent {
children: Node[]
}
interface ParentOf extends Parent {
children: T
}
interface Root extends Node, ParentOf {
type: 'root'
}
interface Element extends Node, ParentOf {
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
`$3
`ts
type Node =
| Root
| Element
| Texttype 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
`$3
`ts
function parseDocument(html: string): LHAST.Root
`$3
`ts
function parseFragment(html: string): LHAST.Root
`$3
`ts
function compact(root: LHAST.Root): LHASTCompact.Root
`$3
`ts
function uncompact(root: LHAST.COMPACT.Root): LHAST.Root
`$3
`ts
function validateLHAST(data: unknown): asserts data is LHAST.Root
`$3
`ts
function validateLHASTCompact(data: unknown): asserts data is LHASTCompact.Root
`$3
`ts
function isLHAST(data: unknown): data is LHAST.Root
`$3
`ts
function isLHASTCompact(data: unknown): data is LHASTCompact.Root
`$3
`ts
const LHASTSchema
`$3
`ts
const LHASTCompactSchema
`$3
#### builder
`ts
import * as Builder from 'lhast/utils/builder'
`Each lhast node has a corresponding builder.
#### is
`ts
import * as Is from 'lhast/utils/is'
`Each lhast node has a corresponding
is function.#### flatMap
`ts
import { flatMap } from 'lhast/utils/flat-map'function flatMap(
node: LHAST.Node
, fn: (node: LHAST.Node) => AST.Node[]
): LHAST.Node[]
`#### map
`ts
import { map } from 'lhast/utils/map'function map(
node: LHAST.Node
, fn: (node: LHAST.Node) => AST.Node
): LHAST.Node
`#### filter
`ts
import { filter } from 'lhast/utils/filter'function filter(
node: LHAST.Node
, predicate: (node: LHAST.Node) => unknown
): LHAST.Node | undefined
`#### find
`ts
import { find } from 'lhast/utils/find'function find(
node: LHAST.Node
, predicate: (node: LHAST.Node) => boolean
): T | undefined
`#### findAll
`ts
import { findAll } from 'lhast/utils/find-all'function* findAll(
node: LHAST.Node
, predicate: (node: LHAST.Node) => boolean
): Iterable
`#### traverseDescendantNodes
`ts
import { traverseDescendantNodes } from 'lhast/utils/traverse-descendant-nodes'function traverseDescendantNodes(node: LHAST.Node): Iterable
`#### reverse
`ts
import { reverse } from 'lhast/utils/reverse'function reverse(root: LHAST.Root): LHAST.Root
`#### addHelpers
`ts
import { addHelpers, addHelpersInPlace, NodeWithHelpers } from 'lhast/utils/add-helpers'type NullOrNodeWithHelpers =
T extends null
? null
: NodeWithHelpers>
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 id: string
parent: null
index: null
previousSibling: null
nextSibling: null
children: Array>
}>
: Node extends LHAST.Element
? Mixin id: string
parent: NullOrNodeWithHelpers
index: number
previousSibling: NullOrNodeWithHelpers
nextSibling: NullOrNodeWithHelpers
children: Array<
NodeWithHelpers<
LHAST.ElementContent
, LHAST.ElementContent
, LHAST.Element
>
>
}>
: Mixin id: string
parent: NullOrNodeWithHelpers
index: number | null
previousSibling: NullOrNodeWithHelpers
nextSibling: NullOrNodeWithHelpers
}>
function addHelpers(node: T): NodeWithHelpers
function addHelpersInPlace(node: T): NodeWithHelpers
`#### removeHelpers
`ts
import { removeHelpers, removeHelpersInPlace } from 'lhast/utils/remove-helpers'function removeHelpers(node: NodeWithHelpers): T
function removeHelpersInPlace(node: NodeWithHelpers): T
`#### withHelpers
`ts
import { withHelpers, withHelpersInPlace } from 'lhast/utils/with-helpers'function withHelpers(
node: T
, fn: (node: NodeWithHelpers) => U
): U
function withHelpersInPlace(
node: T
, fn: (node: NodeWithHelpers) => U
): U
``