https://github.com/jamesgeorgewilliams/rehype-wrap-sibling
A rehype plugin to wrap a selected element and its sibling in a container element.
https://github.com/jamesgeorgewilliams/rehype-wrap-sibling
rehype rehype-plugin sibling wrap
Last synced: 3 months ago
JSON representation
A rehype plugin to wrap a selected element and its sibling in a container element.
- Host: GitHub
- URL: https://github.com/jamesgeorgewilliams/rehype-wrap-sibling
- Owner: jamesgeorgewilliams
- License: mit
- Created: 2024-12-11T14:50:45.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-05-21T04:34:26.000Z (about 1 year ago)
- Last Synced: 2025-10-05T17:49:38.803Z (8 months ago)
- Topics: rehype, rehype-plugin, sibling, wrap
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/rehype-wrap-sibling
- Size: 255 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# rehype-wrap-sibling

[](https://badge.fury.io/js/rehype-wrap-sibling)
A [rehype](https://github.com/rehypejs/rehype?tab=readme-ov-file#plugins) plugin to wrap a selected element and its
sibling in a container element.
- The plugin will wrap all occurrences of the provided selector and its sibling if one exists.
- The selected element(s) and next sibling are wrapped by default.
- HTML comments between the selected element(s) and sibling element will not be preserved.
- The plugin doesn't prettify output of the wrapper: whitespace, line endings, etc.
## Installation
This package is [ESM only][esm]. In Node.js (version 18+), install with [npm][]:
```sh
npm i rehype-wrap-sibling
```
## Usage
```js
/* example.js */
import * as fs from 'node:fs/promises';
import { rehype } from 'rehype';
import rehypeWrapSibling from 'rehype-wrap-sibling';
const document = await fs.readFile('./input.html', 'utf8');
const file = await rehype()
.data('settings', { fragment: true })
.use(rehypeWrapSibling, {
selector: 'h1',
wrapper: 'hgroup#doc-title',
})
.process(document);
await fs.writeFile('./output.html', String(file));
```
```html
HTML: Living Standard
Last Edited: 7 July 2022
```
```html
HTML: Living Standard
Last Edited: 7 July 2022
```
### Options
Name | Type | Description
---|---|---
`selector` | `string` | CSS selector to select an element(s).
`wrapper` | `string?` | Element to wrap the selected element and its sibling.
`wrapPreviousSibling` | `boolean?` | If `true`, the selected element(s), and its previous sibling are wrapped.
- If no `wrapper` option is provided by the user, the default container element will be `
`.
- The `selector` option can be a CSS selector supported
via [hast-util-select](https://github.com/syntax-tree/hast-util-select?tab=readme-ov-file#support).
- The `wrapper` option can be a selector supported
via [hast-util-parse-selector](https://github.com/syntax-tree/hast-util-parse-selector/blob/main/readme.md#parameters).
## License
[MIT](./LICENSE) :copyright: [James Williams][author]
[author]: https://jameswilliams.dev
[npm]: https://docs.npmjs.com/cli/install
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c