hast-util-heading-rank is a specialized utility within the unified ecosystem, designed to determine the rank (or level) of HTML heading elements (h1-h6) within a HAST (Hypertext Abstract Syntax Tree) node. The current stable version is 3.0.0. As part of the unified collective, it maintains a consistent release cadence, with major versions often dropping support for unmaintained Node.js versions and introducing breaking changes, particularly concerning module systems and type definitions. Its primary differentiator is its focused role in parsing and analyzing HAST structures, providing a clean, immutable API to query heading levels without altering the tree. This contrasts with related utilities like `hast-util-shift-heading`, which is designed to modify heading ranks, or `hast-util-heading`, which merely checks if a node is a heading.
npm install hast-util-heading-rankVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to use `headingRank` to determine the numerical rank of various HAST heading nodes, including non-heading elements and plain text, showing that it returns `undefined` for non-headings.
Migrate your codebase to use ES module `import` syntax. For example, change `const { headingRank } = require('hast-util-heading-rank')` to `import { headingRank } from 'hast-util-heading-rank'`.Upgrade your Node.js environment to version 16 or newer. Use nvm or your package manager to update.
Always use the main entry point: `import { headingRank } from 'hast-util-heading-rank'`. Do not rely on internal file paths.Ensure that the input passed to `headingRank` is always a valid HAST `Node`. Validate inputs if they might come from external sources.
Update your code to expect and handle `undefined` instead of `null` for non-heading results. For example, `if (result === undefined)` or `if (typeof result === 'undefined')`.
Update `@types/hast` in your project to a compatible version (typically the latest major) to ensure correct type definitions and avoid TypeScript errors.
Change `const { headingRank } = require('hast-util-heading-rank')` to `import { headingRank } from 'hast-util-heading-rank'`.Ensure that the `node` argument is always a valid HAST `Node` object. You might need to validate or transform your input before passing it to `headingRank`.
Add a type guard to check if the result is `undefined` before trying to use it. For example: `const rank = headingRank(node); if (typeof rank !== 'undefined') { /* use rank */ }`.No dependency data recorded yet.