New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

rehype-rewrite

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rehype-rewrite

Rewrite element with rehype.

4.0.2
latest
Source
npm
Version published
Maintainers
1
Created

What is rehype-rewrite?

The rehype-rewrite package is a plugin for the rehype ecosystem that allows you to rewrite and transform HTML nodes. It provides a way to manipulate the structure and content of HTML documents programmatically.

What are rehype-rewrite's main functionalities?

Rewrite HTML nodes

This feature allows you to rewrite HTML nodes. In this example, all <a> tags are transformed into <span> tags.

const rehype = require('rehype');
const rehypeRewrite = require('rehype-rewrite');

rehype()
  .use(rehypeRewrite, {
    rewrite: (node, index, parent) => {
      if (node.tagName === 'a') {
        node.tagName = 'span';
      }
    }
  })
  .process('<a href="#">Link</a>', (err, file) => {
    if (err) throw err;
    console.log(String(file));
  });

Modify text content

This feature allows you to modify the text content within HTML nodes. In this example, any text containing 'Hello' is replaced with 'Hi'.

const rehype = require('rehype');
const rehypeRewrite = require('rehype-rewrite');

rehype()
  .use(rehypeRewrite, {
    rewrite: (node, index, parent) => {
      if (node.type === 'text' && node.value.includes('Hello')) {
        node.value = node.value.replace('Hello', 'Hi');
      }
    }
  })
  .process('<p>Hello World</p>', (err, file) => {
    if (err) throw err;
    console.log(String(file));
  });

Add attributes to elements

This feature allows you to add attributes to HTML elements. In this example, an 'alt' attribute is added to all <img> tags.

const rehype = require('rehype');
const rehypeRewrite = require('rehype-rewrite');

rehype()
  .use(rehypeRewrite, {
    rewrite: (node, index, parent) => {
      if (node.tagName === 'img') {
        node.properties = node.properties || {};
        node.properties.alt = 'An image';
      }
    }
  })
  .process('<img src="image.png" />', (err, file) => {
    if (err) throw err;
    console.log(String(file));
  });

Other packages similar to rehype-rewrite

Keywords

rehype

FAQs

Package last updated on 26 Nov 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts