rehype-unwrap-images
v1.0.0
Published
rehype plugin to remove the wrapping paragraph (`<p>`) for images (`<img>`)
Downloads
4,877
Readme
rehype-unwrap-images
rehype plugin to remove the wrapping paragraph (<p>
) for
images (<img>
).
Contents
What is this?
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 should I use this?
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.
Install
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>
Use
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">
API
This package exports no identifiers.
The default export is rehypeUnwrapImages
.
unified().use(rehypeUnwrapImages)
Remove the wrapping paragraph for images.
Parameters
There are no parameters.
Returns
Transform (Transformer
).
Types
This package is fully typed with TypeScript. It exports no additional types.
Compatibility
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+.
Security
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
.
Contribute
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.