HAST Utility: Is CSS Link
hast-util-is-css-link is a utility function within the unified (specifically rehype) ecosystem designed to determine if a given HAST (HTML Abstract Syntax Tree) node represents a `<link>` element that references a CSS stylesheet. It checks for a `rel` attribute containing 'stylesheet' and a compatible `type` attribute (no type, empty string, or 'text/css'). The package is currently at version 3.0.1 and is part of the actively maintained unified collective, which typically releases updates aligned with Node.js LTS versions and major ecosystem changes. It differentiates itself by providing a focused, robust, and tree-agnostic check adhering to WHATWG HTML standards, making it reliable for transformations and analysis of HTML in JavaScript environments.
Common errors
-
Error [ERR_REQUIRE_ESM]: require() of ES Module .../node_modules/hast-util-is-css-link/index.js from ... not supported.
cause Attempting to use `require()` to import `hast-util-is-css-link`, which is an ES Module.fixChange `const { isCssLink } = require('hast-util-is-css-link')` to `import { isCssLink } from 'hast-util-is-css-link'`. Ensure your project's `package.json` has `"type": "module"` or you are using a bundler configured for ESM. -
TypeError: isCssLink is not a function (or similar undefined export error)
cause Incorrectly importing a named export as a default export, or attempting to import a non-existent export.fixEnsure you are using named imports: `import { isCssLink } from 'hast-util-is-css-link'`. There is no default export for this package.
Warnings
- breaking The unified ecosystem, which this package is part of, updated to require Node.js 16 and changed to ESM-only exports in its major version 7.0.0 (though this package is at 3.0.1, it's tied to that ecosystem versioning).
- breaking The unified ecosystem now strictly uses `exports` in `package.json`, which changes module resolution. Using direct deep imports ('private APIs') is no longer supported or stable.
- gotcha Improper handling of HTML with rehype (the parent project for HAST utilities) can lead to Cross-Site Scripting (XSS) vulnerabilities if user-controlled HTML is processed without sanitization.
Install
-
npm install hast-util-is-css-link -
yarn add hast-util-is-css-link -
pnpm add hast-util-is-css-link
Imports
- isCssLink
const { isCssLink } = require('hast-util-is-css-link')import { isCssLink } from 'hast-util-is-css-link' - isCssLink (Type)
import type { Node } from 'hast' import { isCssLink } from 'hast-util-is-css-link'
Quickstart
import { h } from 'hastscript';
import { isCssLink } from 'hast-util-is-css-link';
const cssLinkNode = h('link', { rel: ['stylesheet', 'author'], href: 'styles.css' });
const alternateCssLinkNode = h('link', { rel: ['stylesheet'], type: 'text/css', href: 'another.css' });
const nonCssLinkNode = h('link', { rel: ['preload'], href: 'image.png' });
const scriptNode = h('script', { src: 'app.js' });
console.log('Is cssLinkNode a CSS link?', isCssLink(cssLinkNode));
console.log('Is alternateCssLinkNode a CSS link?', isCssLink(alternateCssLinkNode));
console.log('Is nonCssLinkNode a CSS link?', isCssLink(nonCssLinkNode));
console.log('Is scriptNode a CSS link?', isCssLink(scriptNode));