Skip to content

marekweb/hast-util-insert

Repository files navigation

hast-util-insert

A hast utility to insert nodes into a tree, at a location in the tree that is specified by a selector string.

Install

This package is ESM only.

Install with npm:

npm install hast-util-insert

In Deno with esm.sh:

import { insert } from "https://esm.sh/hast-util-insert@1";

In browsers with esm.sh:

<script type="module">
  import { insert } from 'https://esm.sh/hast-util-insert@1?bundle'
</script>

Definition

function insert(tree: Tree, selector: string, nodes: InsertableOrFunction, action?: Action): Element | null;

Where:

  • tree: Tree is a hast tree
  • selector: string is an element selector string (like for querySelector) compatible with hast-util-select.
  • nodes: InsertableOrFunction is a hast node or a function that received the selected node and returns a node to insert.
  • action is one of:
    • "insert" (default) replaces the contents of the selected node
    • "prepend" inserts before the selected node's existing children
    • "append" inserts after the selected node's existing children

Use

import { h } from "hastscript";
import { insert } from "hast-util-insert";

// Starting with a newly created hast tree
const tree = h("div", [h(".planet", "Jupiter")]);

// Append the b element to the children of the .planet node
insert(tree, ".planet", h("b", "inserted bold tag"), "append");

console.log(inspect(tree));

License

MIT © Marek Zaluski