Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork1
Rewrite element with rehype.
License
jaywcjlove/rehype-rewrite
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Rewrite element withrehype.
This package isESM only: Node 12+ is needed to use it and it must beimport instead ofrequire.
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';importrehypeRewritefrom'rehype-rewrite';importstringifyfrom'rehype-stringify';consthtml=`<h1>header</h1>`;consthtmlStr=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 */exportdeclareconstgetCodeString:(data?:ElementContent[],code?:string)=>string;exportdeclaretypeRehypeRewriteOptions={/** * 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;};declareconstremarkRewrite:Plugin<[RehypeRewriteOptions?],Root>;exportdefaultremarkRewrite;
Select an element to be wrapped. Expects a string selector that can be passed to hast-util-select (supported selectors). Ifselector is not set then wrap will check for a body all elements.
Rewrite element.
import{rehype}from'rehype';importrehypeRewritefrom'rehype-rewrite';importstringifyfrom'rehype-stringify';consthtml=`<h1>header</h1><h1>header</h1><h1>header</h1>`;consthtmlStr=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><h1class="title3">header</h1>
Output:<h1class="test">header</h1><h1class="test">header</h1><h1class="test">header</h1>
import{rehype}from'rehype';importrehypeRewritefrom'rehype-rewrite';importstringifyfrom'rehype-stringify';consthtml=`<h1>header</h1>`;consthtmlStr=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><bodystyle="color:red;"><h1>header</h1></body></html>
import{rehype}from'rehype';importrehypeRewritefrom'rehype-rewrite';importstringifyfrom'rehype-stringify';consthtml=`<h1>hello</h1>`;consthtmlStr=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';importremarkParsefrom'remark-parse';importrehypeRawfrom'rehype-raw';importremark2rehypefrom'remark-rehype';importrehypeRewritefrom'rehype-rewrite';importstringifyfrom'rehype-stringify';consthtml=`<h1>hello</h1>`;consthtmlStr=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:<h1style="color:red;">Hello World!</h1>
rehype-videoAdd improved video syntax: links to.mp4and.movturn into videos.rehype-attrNew syntax to add attributes to Markdown.rehype-ignoreIgnore content display via HTML comments, Shown in GitHub readme, excluded in HTML.remark-github-blockquote-alertRemark plugin to add support for GitHub Alert.
As always, thanks to our amazing contributors!
Made withaction-contributors.
MIT ©Kenny Wong
About
Rewrite element with rehype.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors3
Uh oh!
There was an error while loading.Please reload this page.