rehype plugin to remove the wrapping paragraph (<p>
) for
images (<img>
).
- What is this?
- When should I use this?
- Install
- Use
- API
- Types
- Compatibility
- Security
- Contribute
- License
This package is a unified (rehype) plugin that searches for paragraphs which contain only images (possibly in links) and nothing else, and then removes those surrounding paragraphs.
When working with markdown, this project can make it simpler to style images with CSS, for example displaying them at the full available width, because paragraph styles no longer interfere with them.
This package is ESM only. In Node.js (version 16+), install with npm:
npm install rehype-unwrap-images
In Deno with esm.sh
:
import rehypeUnwrapImages from 'https://esm.sh/rehype-unwrap-images@1'
In browsers with esm.sh
:
<script type="module">
import rehypeUnwrapImages from 'https://esm.sh/rehype-unwrap-images@1?bundle'
</script>
Say we have the following file example.html
.
<h1>Saturn</h1>
<p>Saturn is the sixth planet from the Sun and the second-largest in the Solar
System, after Jupiter.</p>
<p><img alt="Saturn" src="//upload.wikimedia.org/wikipedia/commons/thumb/c/c7/Saturn_during_Equinox.jpg/300px-Saturn_during_Equinox.jpg"></p>
…and a module example.js
:
import rehypeParse from 'rehype-parse'
import rehypeStringify from 'rehype-stringify'
import rehypeUnwrapImages from 'rehype-unwrap-images'
import {read} from 'to-vfile'
import {unified} from 'unified'
const file = await read('example.html')
await unified()
.use(rehypeParse, {fragment: true})
.use(rehypeUnwrapImages)
.use(rehypeStringify)
.process(file)
console.log(String(file))
…then running node example.js
yields:
<h1>Saturn</h1>
<p>Saturn is the sixth planet from the Sun and the second-largest in the Solar
System, after Jupiter.</p>
<img alt="Saturn" src="//upload.wikimedia.org/wikipedia/commons/thumb/c/c7/Saturn_during_Equinox.jpg/300px-Saturn_during_Equinox.jpg">
This package exports no identifiers.
The default export is rehypeUnwrapImages
.
Remove the wrapping paragraph for images.
There are no parameters.
Transform (Transformer
).
This package is fully typed with TypeScript. It exports no additional types.
Projects maintained by the unified collective are compatible with maintained versions of Node.js.
When we cut a new major release, we drop support for unmaintained versions of
Node.
This means we try to keep the current release line, rehype-unwrap-images@1
,
compatible with Node.js 16.
This plugin works with unified
version 6+ and rehype
version 1+.
Use of rehype-unwrap-images
does not change the tree other than sometimes
removing <p>
.
Use of this plugin does not open you up for a
cross-site scripting (XSS) attack.
When in doubt,
use rehype-sanitize
.
See contributing.md
in rehypejs/.github
for ways to get started.
See support.md
for ways to get help.
This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.