hast-util-is-javascript

3.0.1 · active · verified Sun Apr 19

`hast-util-is-javascript` is a utility package within the unified/rehype ecosystem designed to determine if a given `hast` (HTML Abstract Syntax Tree) node represents a JavaScript script. It specifically checks `<script>` elements, evaluating their `type` and `language` attributes to ascertain if they contain or reference JavaScript, or if these attributes are absent, implying JavaScript by default. The current stable version is `3.0.1`. As part of the `rehypejs` project, it follows a coordinated release schedule, often releasing minor or patch updates across multiple packages, with major version increments for significant breaking changes, such as the recent `7.0.0` ecosystem release which introduced ESM-only modules and a Node.js 16+ requirement. Its primary differentiator is its seamless integration with other `hast` manipulation tools and its strict adherence to WHATWG HTML parsing standards.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to use `isJavaScript` to check various `hast` script nodes for JavaScript content based on their `type` or `language` attributes, or when these attributes are implicitly JavaScript.

import { h } from 'hastscript';
import { isJavaScript } from 'hast-util-is-javascript';

// Create a script element without type/language, which defaults to JS
const jsScript = h('script');
console.log("Is plain script JS?", isJavaScript(jsScript));
// Expected output: Is plain script JS? true

// Create a script element with explicit JS type
const jsScriptWithType = h('script', { type: 'text/ecmascript' });
console.log("Is script with text/ecmascript type JS?", isJavaScript(jsScriptWithType));
// Expected output: Is script with text/ecmascript type JS? true

// Create a script element with explicit JS language
const jsScriptWithLanguage = h('script', { language: 'ecmascript' });
console.log("Is script with ecmascript language JS?", isJavaScript(jsScriptWithLanguage));
// Expected output: Is script with ecmascript language JS? true

// Create a script element with a non-JS type
const nonJsScript = h('script', { type: 'text/fooscript' });
console.log("Is script with text/fooscript type JS?", isJavaScript(nonJsScript));
// Expected output: Is script with text/fooscript type JS? false

// Create a div element (not a script)
const divElement = h('div');
console.log("Is a div element JS?", isJavaScript(divElement));
// Expected output: Is a div element JS? false

view raw JSON →