HAST Link Body OK Checker
hast-util-is-body-ok-link is a utility package within the `unified` ecosystem, specifically designed for working with HAST (HTML Abstract Syntax Tree) nodes. Its primary function is to determine if a given `link` element adheres to the “Body OK” criteria as defined by the WHATWG HTML standard, meaning it can safely exist within the `<body>` section of an HTML document rather than being restricted to the `<head>`. This utility is currently at version `3.0.1` and is maintained as part of the broader `rehypejs` collective. Releases typically align with major `unified` ecosystem updates, ensuring compatibility with other `hast` and `rehype` plugins. A key differentiator is its strict adherence to web standards and its integration into a comprehensive Markdown/HTML processing pipeline. It ships with full TypeScript type definitions, providing a robust and type-safe development experience for Node.js (version 16+) and browser environments.
Common errors
-
ERR_REQUIRE_ESM
cause Attempting to use `require('hast-util-is-body-ok-link')` in a CommonJS environment, but the package is ESM-only.fixSwitch to ES module import syntax: `import { isBodyOkLink } from 'hast-util-is-body-ok-link';`. -
TypeError: isBodyOkLink is not a function
cause The `isBodyOkLink` function was imported incorrectly, likely as a default import (`import isBodyOkLink from '...'`) when it is a named export.fixUse a named import: `import { isBodyOkLink } from 'hast-util-is-body-ok-link';`. -
TypeError: Cannot read properties of undefined (reading 'type')
cause The `node` argument passed to `isBodyOkLink` is `undefined`, `null`, or an object that does not conform to the expected HAST `Node` structure, preventing access to its `type` property.fixEnsure the `node` argument is a properly constructed HAST `Node` object before passing it to `isBodyOkLink`.
Warnings
- breaking Version 3.0.0 and later of `hast-util-is-body-ok-link` (and the `unified` ecosystem it belongs to) require Node.js 16 or newer. Older Node.js versions are no longer supported.
- breaking `hast-util-is-body-ok-link` is an ESM-only package since version 3.0.0. Attempting to use CommonJS `require()` statements will result in an `ERR_REQUIRE_ESM` error.
- breaking Version 3.0.0 aligns with major updates across the `unified` and `hast` ecosystems (e.g., `unified` v11, `@types/hast` updates). This may necessitate updating other `hast` utilities and plugins to their latest compatible major versions.
- gotcha When processing untrusted HTML content, directly using `rehype` and its utilities like this without explicit sanitization can lead to Cross-Site Scripting (XSS) vulnerabilities.
Install
-
npm install hast-util-is-body-ok-link -
yarn add hast-util-is-body-ok-link -
pnpm add hast-util-is-body-ok-link
Imports
- isBodyOkLink
const isBodyOkLink = require('hast-util-is-body-ok-link')import { isBodyOkLink } from 'hast-util-is-body-ok-link' - isBodyOkLink
import isBodyOkLink from 'hast-util-is-body-ok-link'
import { isBodyOkLink } from 'hast-util-is-body-ok-link' - Node
import type { Node } from 'hast'
Quickstart
import { h } from 'hastscript';
import { isBodyOkLink } from 'hast-util-is-body-ok-link';
// Example 1: Link with itemProp (Body OK)
const link1 = h('link', { itemProp: 'foo' });
console.log('Link with itemProp:', isBodyOkLink(link1)); // Expected: true
// Example 2: Stylesheet link (Body OK)
const link2 = h('link', { rel: ['stylesheet'], href: 'index.css' });
console.log('Stylesheet link:', isBodyOkLink(link2)); // Expected: true
// Example 3: Author link (Not Body OK)
const link3 = h('link', { rel: ['author'], href: 'index.css' });
console.log('Author link:', isBodyOkLink(link3)); // Expected: false
// Example 4: Pingback link (Body OK)
const link4 = h('link', { rel: ['pingback'], href: 'ping.xml' });
console.log('Pingback link:', isBodyOkLink(link4)); // Expected: true
// Example 5: Non-link element
const div = h('div', 'Hello');
console.log('Non-link element:', isBodyOkLink(div)); // Expected: false (or handles as invalid HAST Node gracefully)