Rehype Minify JavaScript URL

5.0.1 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

Demonstrates how to use rehype-minify-javascript-url within a unified pipeline to parse HTML, minify JavaScript URLs, and then stringify the result.

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

view raw JSON →