Skip to content

chrisnewtn/hast-util-find-parent

Repository files navigation

hast-util-find-parent

Tests Badge

A utility for finding an Element's immediate parent in its tree. Useful for when you want to remove or replace an Element in the tree.

If you want to find every ancestor of an Element, rather than just its immediate parent, unist-util-visit-parents is likely a better fit.

API

This package exports a single function findParent.

findParent(element, tree)

Finds the immediate parent of the passed element in the passed tree.

Parameters
  • element (Element) — A hast Element.
  • tree (Parent) — A hast Parent.
Returns

The immediate Parent of the passed element. This could be tree itself.

Example
import { h } from 'hastscript';
import { findParent } from 'hast-util-find-parent';

const element = h('h1', 'Hello, world!');

const tree = h('root',
  h('article', [element])
);

console.log(
  findParent(element, tree)
);

Yields:

{
  type: 'element',
  tagName: 'article',
  properties: {},
  children: [
    {
      type: 'element',
      tagName: 'h1',
      properties: {},
      children: [Array]
    }
  ]
}

About

A utility to find an Element's parent in its tree

Resources

License

Stars

Watchers

Forks

Packages

No packages published