
Research
/Security News
Weaponizing Discord for Command and Control Across npm, PyPI, and RubyGems.org
Socket researchers uncover how threat actors weaponize Discord across the npm, PyPI, and RubyGems ecosystems to exfiltrate sensitive data.
rehype-rewrite
Advanced tools
Rewrite element with rehype.
This package is ESM only: Node 12+ is needed to use it and it must be import
instead of require
.
npm install rehype-rewrite
🚧 Migrate from rehype-rewrite
v2.xtov3.x
.
rehype() - .use(rehypeRewrite, (node, index, parent) => {}) + .use(rehypeRewrite, { + rewrite: (node, index, parent) => {} + })
import { rehype } from 'rehype';
import rehypeRewrite from 'rehype-rewrite';
import stringify from 'rehype-stringify';
const html = `<h1>header</h1>`;
const htmlStr = rehype()
.data('settings', { fragment: true })
.use(rehypeRewrite, {
rewrite: (node, index, parent) => {
if(node.type == 'text' && node.value == 'header') {
node.value = ''
}
}
})
.use(stringify)
.processSync(html)
.toString()
<h1>header</h1>
Output:
<h1></h1>
import { Plugin } from 'unified';
import { Root, Element, ElementContent, RootContent } from 'hast';
/** Get the node tree source code string */
export declare const getCodeString: (data?: ElementContent[], code?: string) => string;
export declare type RehypeRewriteOptions = {
/**
* Select an element to be wrapped. Expects a string selector that can be passed to hast-util-select ([supported selectors](https://github.com/syntax-tree/hast-util-select/blob/master/readme.md#support)).
* If `selector` is not set then wrap will check for a body all elements.
*/
selector?: string;
/** Rewrite Element. */
rewrite(node: Root | RootContent, index: number | null, parent: Root | Element | null): void;
};
declare const remarkRewrite: Plugin<[RehypeRewriteOptions?], Root>;
export default remarkRewrite;
selector?: string;
Select an element to be wrapped. Expects a string selector that can be passed to hast-util-select (supported selectors). If selector
is not set then wrap will check for a body all elements.
rewrite(node, index, parent): void;
Rewrite element.
import { rehype } from 'rehype';
import rehypeRewrite from 'rehype-rewrite';
import stringify from 'rehype-stringify';
const html = `<h1>header</h1><h1>header</h1><h1 class="title3">header</h1>`;
const htmlStr = rehype()
.data('settings', { fragment: true })
.use(rehypeRewrite, {
selector: 'h1',
rewrite: (node) => {
if (node.type === 'element') {
node.properties.className = 'test';
}
}
})
.use(stringify)
.processSync(html)
.toString()
<h1>header</h1> <h1>header</h1> <h1 class="title3">header</h1>
Output:
<h1 class="test">header</h1> <h1 class="test">header</h1> <h1 class="test">header</h1>
import { rehype } from 'rehype';
import rehypeRewrite from 'rehype-rewrite';
import stringify from 'rehype-stringify';
const html = `<h1>header</h1>`;
const htmlStr = rehype()
.use(rehypeRewrite, {
rewrite: (node) => {
if (node.type == 'element' && node.tagName == 'body') {
node.properties = { ...node.properties, style: 'color:red;'}
}
}
})
.use(stringify)
.processSync(html)
.toString()
<h1>header</h1>
Output:
<html><head></head><body style="color:red;"><h1>header</h1></body></html>
import { rehype } from 'rehype';
import rehypeRewrite from 'rehype-rewrite';
import stringify from 'rehype-stringify';
const html = `<h1>hello</h1>`;
const htmlStr = rehype()
.data('settings', { fragment: true })
.use(rehypeRewrite, {
rewrite: (node) => {
if (node.type == 'element' && node.tagName == 'h1') {
node.children = [ ...node.children, {
type: 'element',
tagName: 'span',
properties: {},
children: [
{type: 'text', value: ' world'}
]
}]
}
}
})
.use(stringify)
.processSync(html)
.toString()
<h1>hello</h1>
Output:
<h1>hello<span> world</span></h1>
import { unified } from 'unified';
import remarkParse from 'remark-parse';
import rehypeRaw from 'rehype-raw';
import remark2rehype from 'remark-rehype';
import rehypeRewrite from 'rehype-rewrite';
import stringify from 'rehype-stringify';
const html = `<h1>hello</h1>`;
const htmlStr = unified()
.use(remarkParse)
.use(remark2rehype, { allowDangerousHtml: true })
.use(rehypeRaw)
.use(rehypeRewrite, {
rewrite: (node) => {
if (node.type == 'element' && node.tagName == 'h1') {
node.properties = { ...node.properties, style: 'color:red;' }
}
}
})
.use(stringify)
.processSync(html)
.toString()
<h1>hello</h1>
Output:
<h1 style="color:red;">Hello World!</h1>
rehype-video
Add improved video syntax: links to .mp4
and .mov
turn into videos.rehype-attr
New syntax to add attributes to Markdown.rehype-ignore
Ignore content display via HTML comments, Shown in GitHub readme, excluded in HTML.rehypejs
HTML processor powered by plugins part of the @unifiedjs collectiveremark-parse
remark plugin to parse Markdownremark-rehype
remark plugin to transform to rehyperehype-raw
rehype plugin to reparse the tree (and raw nodes)rehype-stringify
rehype plugin to serialize HTMLAs always, thanks to our amazing contributors!
Made with action-contributors.
MIT © Kenny Wong
rehype-parse is a plugin for parsing HTML into a syntax tree. While it does not provide rewriting capabilities, it is often used in conjunction with other rehype plugins to manipulate HTML.
rehype-stringify is a plugin for serializing a syntax tree back into HTML. It is typically used after parsing and transforming the HTML with other rehype plugins.
hast-util-select is a utility for selecting nodes in a HAST (Hypertext Abstract Syntax Tree). It can be used to find and manipulate specific nodes, similar to how rehype-rewrite allows for node transformations.
FAQs
Rewrite element with rehype.
The npm package rehype-rewrite receives a total of 216,947 weekly downloads. As such, rehype-rewrite popularity was classified as popular.
We found that rehype-rewrite demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Research
/Security News
Socket researchers uncover how threat actors weaponize Discord across the npm, PyPI, and RubyGems ecosystems to exfiltrate sensitive data.
Security News
Socket now integrates with Bun 1.3’s Security Scanner API to block risky packages at install time and enforce your organization’s policies in local dev and CI.
Research
The Socket Threat Research Team is tracking weekly intrusions into the npm registry that follow a repeatable adversarial playbook used by North Korean state-sponsored actors.