Rehype Minify JavaScript URL
rehype-minify-javascript-url is a rehype plugin designed to optimize HTML documents by minifying `javascript:` URL attributes found within various HTML tags. It's an integral part of the unified collective's rehype ecosystem, which focuses on transforming HTML with a plugin-based architecture. The current stable version is 5.0.1. This package specifically targets and reduces the size of JavaScript code embedded within URL attributes by applying minification techniques (e.g., transforming `alert(true)` to `alert(!0)`). Following the unified ecosystem's release cadence, it ensures compatibility with maintained Node.js versions, currently requiring Node.js 16 or newer. Its primary differentiator is its specialized focus on JavaScript URL minification, contributing to overall HTML document size reduction within the robust rehype processing pipeline.
Common errors
-
Error [ERR_REQUIRE_ESM]: require() of ES Module .../node_modules/rehype-minify-javascript-url/index.js from ... not supported.
cause Attempting to import an ESM-only package using CommonJS `require()` syntax.fixUpdate your import statements to use ES module syntax: `import rehypeMinifyJavaScriptUrl from 'rehype-minify-javascript-url'` and ensure your project is configured for ESM. -
TypeError: rehypeMinifyJavaScriptUrl is not a function
cause This typically occurs if you try to destructure a named export, but the package only provides a default export, or if the import path is incorrect, leading to an undefined module.fixVerify that you are importing the default export correctly: `import rehypeMinifyJavaScriptUrl from 'rehype-minify-javascript-url'`. Do not use `{ rehypeMinifyJavaScriptUrl }`.
Warnings
- breaking This package is ESM-only. CommonJS `require()` is no longer supported.
- breaking rehype-minify-javascript-url requires Node.js version 16 or newer.
- gotcha Improper use of rehype plugins, especially when handling user-generated or untrusted HTML, can lead to Cross-Site Scripting (XSS) vulnerabilities.
- gotcha The `rehype-minify-javascript-url` package exports only a default function. Attempting to use named imports will result in errors.
Install
-
npm install rehype-minify-javascript-url -
yarn add rehype-minify-javascript-url -
pnpm add rehype-minify-javascript-url
Imports
- rehypeMinifyJavaScriptUrl
const rehypeMinifyJavaScriptUrl = require('rehype-minify-javascript-url')import rehypeMinifyJavaScriptUrl from 'rehype-minify-javascript-url'
Quickstart
import rehypeMinifyJavaScriptUrl from 'rehype-minify-javascript-url'
import rehypeParse from 'rehype-parse'
import rehypeStringify from 'rehype-stringify'
import {read} from 'to-vfile'
import {unified} from 'unified'
async function processHtml() {
const file = await unified()
.use(rehypeParse)
.use(rehypeMinifyJavaScriptUrl)
.use(rehypeStringify)
.process(await read('index.html'))
console.log(String(file))
}
// Assuming 'index.html' exists in the current directory with content like:
// <a href="javascript:alert(true)">Click me</a>
// For a runnable example, you might create a dummy file:
// import { write } from 'to-vfile';
// await write({ path: 'index.html', value: '<a href="javascript:alert(true)">Click me</a>' });
processHtml().catch(console.error);