Rehype plugin to manipulate the DOM
rehype plugin that supports tree manipulation to group a document's tags to enforce a certain formation, such as header / section / footer.
unified().use(rehypeAbsorbSiblings[, options])](#unifieduserehypeabsorbsiblings-options)Optionsunified is a project that transforms content with abstract syntax trees (ASTs). rehype adds support for HTML to unified. hast is the HTML AST that rehype uses. This is a rehype plugin uses hast.
With the default options, a document with a tree such as:
``yaml`
- header
- h1: This is a header
- hr
- p: This is section content
- hr
- p: This is another section's content
- footer
- p: This is footer content
will be reformed into:
`yaml`
- header:
- h1: This is a header
- section:
- p: This is section content
- hr
- section:
- p: This is another section's content
- footer:
- p: This is footer content
This is designed to work with markdown editors which stick to the original markdwn rule of not parsing markdown syntax inside of html tags.
.Install
This package is ESM only. In Node.js (version 16+), install with npm:
`
npm install rehype-absorb-siblings
`API
This package exports no identifiers. The default export is
rehypeAbosrbSiblings.$3
Rewrite the document's nodes according to
options or the default options,##### Parameters
*
options (Options, optional)
- configuration##### Returns
Transformer)$3
Configuration.
##### Fields
-
default: (string, default: section) -- the tag to use when there is not a current allowed tag
- allowed: (Array, default: ['header', 'section', 'hr', 'footer']) -- tags permitted at the root level of the node
- extraAllowed: (Array, optional) -- concatenated onto the allowed value, allows one to provide additional tags to the default ones.
- greedy: (Record, optional) -- keys are tag names which will absorb other non-greedy siblings. See example.
- stingy: (Array, default: ['hr']) -- tags that do not absorb their subsequent siblings
- aliases: (Record, optional) -- tag names that, when found, are converted another tag. See example.$3
#### Example: using hr tags as section breaks
providing these options:
`javascript
unified()
.use(remarkParse)
.use(remarkRehype)
.use(rehypeAbsorbSiblings, {
alases: { hr: 'section' }
})
.use(rehypeStringify)
`will transform this markdown:
`markdown
This is section one---
This is section two
`into this html:
`html
This is section one
This is section two
`#### Example: footers with sections
providing these options:
`javascript
unified()
.use(remarkRehype)
.use(rehypeAbsorbSiblings, {
greedy: { footer: true }
})
.use(rehypeStringify)
`Will transform this html:
`html
This is a top-level section
This is the first footer section
This is the second footer section
`into this:
`html
This is a top-level section
`The greedy option will continue to absorb subsequent siblings until another greedy sibling is found, in this case a second footer.
$3
This project is compatible with NodeJS version 20 and forward.This plugin works with
unified version 11+. It may work with versions 6+ but I have not tested it with those.$3
Use of rehype-absorb-siblings` is safe by default.