hast-util-is-javascript
`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
-
ERR_REQUIRE_ESM
cause Attempting to import `hast-util-is-javascript` using CommonJS `require()` syntax in an environment that enforces ESM.fixChange your import statement from `const isJavaScript = require('hast-util-is-javascript')` to `import { isJavaScript } from 'hast-util-is-javascript'`. -
TypeError: Cannot read properties of undefined (reading 'isJavaScript') or TypeError: isJavaScript is not a function
cause Attempting to use `import isJavaScript from 'hast-util-is-javascript'` which assumes a default export, but `isJavaScript` is a named export.fixEnsure you are using a named import: `import { isJavaScript } from 'hast-util-is-javascript'`.
Warnings
- breaking This package is now pure ES module (ESM) and no longer provides CommonJS (CJS) exports. Direct `require()` calls will fail.
- breaking Node.js 16 or greater is now required for this package. Older Node.js versions are no longer supported.
- gotcha Working with HTML ASTs like `hast` can expose applications to Cross-Site Scripting (XSS) vulnerabilities if not handled carefully, especially when injecting user-supplied content.
Install
-
npm install hast-util-is-javascript -
yarn add hast-util-is-javascript -
pnpm add hast-util-is-javascript
Imports
- isJavaScript
const isJavaScript = require('hast-util-is-javascript')import { isJavaScript } from 'hast-util-is-javascript'
Quickstart
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