Rehype Plugin to Remove HTTP-Equiv Meta Elements

4.0.1 · active · verified Wed Apr 22

`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

Warnings

Install

Imports

Quickstart

This example demonstrates how to use `rehype-remove-meta-http-equiv` within a unified processing pipeline to strip `http-equiv` meta tags from an HTML document, showing the transformation from input to optimized output.

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

view raw JSON →