Rehype Plugin to Remove HTTP-Equiv Meta Elements
`rehype-remove-meta-http-equiv` is a rehype plugin designed to optimize HTML output by removing `<meta>` elements that possess the `http-equiv` attribute. Specifically, it targets `content-type` and `content-language` meta tags, which can often be set more efficiently through other means (e.g., `charset` attribute on `<meta>` or `lang` attribute on `<html>`). This package is part of the broader unified ecosystem, focusing on transforming HTML syntax trees (hast). Currently at version 4.0.1, this package, along with other rehype plugins, aligns with the modern JavaScript module system, being ESM-only and requiring Node.js 16 or newer for its operation. Its primary purpose is to help reduce the transfer size of HTML documents, contributing to faster page loads without altering the semantic meaning of the document when alternatives for setting charset or language are present.
Common errors
-
ERR_REQUIRE_ESM: require() of ES Module ... not supported. Instead change the require of index.js in ... to a dynamic import() call.
cause Attempting to import the ESM-only `rehype-remove-meta-http-equiv` package using CommonJS `require()` syntax.fixChange your import statement from `const plugin = require('rehype-remove-meta-http-equiv')` to `import plugin from 'rehype-remove-meta-http-equiv'` and ensure your project is configured for ESM (e.g., by adding `"type": "module"` to `package.json`). -
TypeError: unified is not a function
cause The `unified` package was not imported correctly, or an incompatible version is being used (e.g., an older CommonJS `require` of a new ESM `unified`).fixEnsure `unified` is imported using `import { unified } from 'unified'` and that you are using a compatible version (unified@^13 for rehype v4.x plugins). -
SyntaxError: Cannot use import statement outside a module
cause You are attempting to use an `import` statement for `rehype-remove-meta-http-equiv` in a file that is being treated as a CommonJS module.fixAdd `"type": "module"` to your `package.json` file or use the `.mjs` file extension for your module file to explicitly enable ESM.
Warnings
- breaking This package is ESM-only since version 4. Attempting to use 'require()' will result in an 'ERR_REQUIRE_ESM' error.
- breaking Version 4.0.0 and above require Node.js 16 or newer. Older Node.js versions are not supported, leading to potential runtime errors or unexpected behavior.
- gotcha As rehype processes HTML, untrusted input combined with improper use can lead to Cross-Site Scripting (XSS) vulnerabilities. This plugin removes meta tags but does not sanitize other HTML content.
- breaking The `rehype` ecosystem, including this plugin's compatibility, relies on `unified` v13+ and `hast` v3+. Incompatible versions can cause type errors or runtime failures.
Install
-
npm install rehype-remove-meta-http-equiv -
yarn add rehype-remove-meta-http-equiv -
pnpm add rehype-remove-meta-http-equiv
Imports
- rehypeRemoveMetaHttpEquiv
const rehypeRemoveMetaHttpEquiv = require('rehype-remove-meta-http-equiv')import rehypeRemoveMetaHttpEquiv from 'rehype-remove-meta-http-equiv'
- rehypeRemoveMetaHttpEquiv (CLI config)
"plugins": ["rehype-remove-meta-http-equiv"]
- rehypeRemoveMetaHttpEquiv (Type)
import type { VFile } from 'vfile'
Quickstart
import rehypeParse from 'rehype-parse';
import rehypeRemoveMetaHttpEquiv from 'rehype-remove-meta-http-equiv';
import rehypeStringify from 'rehype-stringify';
import {read} from 'to-vfile';
import {unified} from 'unified';
import {join} from 'node:path';
import {tmpdir} from 'node:os';
import {writeFile} from 'node:fs/promises';
async function processHtml() {
const inputHtml = `<!doctype html>
<html lang="en-GB">
<head>
<meta charset="utf8">
<meta http-equiv="content-type" content="text/html; charset=chinese">
<meta http-equiv="content-language" content="en-US">
</head>
<body>
<h1>Hello World</h1>
<p>This is a test document to show meta http-equiv removal.</p>
</body>
</html>`;
// Create a temporary file to simulate reading from a file system
const tempFilePath = join(tmpdir(), 'input.html');
await writeFile(tempFilePath, inputHtml, 'utf8');
const file = await unified()
.use(rehypeParse, {fragment: false}) // Ensure full document parsing
.use(rehypeRemoveMetaHttpEquiv)
.use(rehypeStringify)
.process(await read(tempFilePath));
console.log('Original HTML:\n', inputHtml);
console.log('\nProcessed HTML:\n', String(file));
// Expected output will have the meta http-equiv tags removed.
}
processHtml().catch(console.error);