HAST Utility: Is CSS Link

3.0.1 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to import and use `isCssLink` to check various HAST nodes, including valid CSS links and non-CSS elements.

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));

view raw JSON →