HAST Utility: Is Conditional Comment
This package, `hast-util-is-conditional-comment`, is a specialized utility within the unified (specifically rehype) ecosystem designed to determine if a given `hast` (Hypertext Abstract Syntax Tree) node represents an HTML conditional comment. Conditional comments are a legacy feature primarily used by older versions of Internet Explorer (discontinued after IE 9) and are generally recommended for removal in modern web development. The current stable version is 3.0.1. As part of the `unified` collective, it adheres to a release cadence tied to major `unified` ecosystem updates, ensuring compatibility with other `hast` and `unist` utilities. Key differentiators include its tight integration with the `hast` AST format, its ESM-only distribution, and its strict compatibility with maintained Node.js versions (currently Node.js 16+). It does not perform transformations but provides a focused predicate function for identifying this specific, non-standard HTML comment type.
Common errors
-
ERR_REQUIRE_ESM
cause Attempting to use `require()` to import `hast-util-is-conditional-comment` in a CommonJS module.fixChange your import statement to `import { isConditionalComment } from 'hast-util-is-conditional-comment';` and ensure your environment supports ESM. -
TypeError: isConditionalComment is not a function
cause Incorrectly importing `isConditionalComment` as a default import, or attempting to destructure from a module that has no named export.fixThe package has no default export. Always use named import syntax: `import { isConditionalComment } from 'hast-util-is-conditional-comment';`.
Warnings
- breaking Version 3.x of `hast-util-is-conditional-comment` is ESM-only and requires Node.js 16 or later. Attempting to use it in a CommonJS environment or older Node.js versions will result in import errors or runtime failures.
- gotcha While this utility itself only checks node types and does not modify the AST, working with `hast` trees in general (especially with other `rehype` plugins) can expose applications to Cross-Site Scripting (XSS) vulnerabilities if not handled carefully.
- gotcha This package specifically identifies legacy Internet Explorer conditional comments. It will not identify other types of special comments or directives, only the specific patterns recognized as IE conditional comments.
Install
-
npm install hast-util-is-conditional-comment -
yarn add hast-util-is-conditional-comment -
pnpm add hast-util-is-conditional-comment
Imports
- isConditionalComment
const isConditionalComment = require('hast-util-is-conditional-comment')import { isConditionalComment } from 'hast-util-is-conditional-comment' - Node
import { Node } from 'hast'import type { Node } from 'hast' - u
import u from 'unist-builder'
import { u } from 'unist-builder'
Quickstart
import { isConditionalComment } from 'hast-util-is-conditional-comment';
import { u } from 'unist-builder'; // u for creating unist/hast nodes
// A valid IE conditional comment
const ieConditionalComment = u({ type: 'comment', value: '[if IE]>...<![endif]' });
// An IE conditional end comment
const ieConditionalEndComment = u({ type: 'comment', value: '<![endif]' });
// A regular HTML comment
const regularComment = u({ type: 'comment', value: 'This is a regular comment' });
console.log('Is IE Conditional Comment (full):', isConditionalComment(ieConditionalComment));
// => true
console.log('Is IE Conditional Comment (end):', isConditionalComment(ieConditionalEndComment));
// => true
console.log('Is Regular Comment:', isConditionalComment(regularComment));
// => false