rehype plugin to enhance images.
- What is this?
- When should I use this?
- Install
- Use
- API
- Bugs
- Authoring
- HTML
- CSS
- Syntax
- Types
- Compatibility
- Security
- Related
- Contribute
- Notice
- License
This plugin enhances images by dropping them if they are invalid, creating links around them, and optionally passing images through an image proxy.
An image proxy requires a dedicated server, which could become costly if you have tons of user content, but it prevents leaking the readers information to external servers, and it solves CORS errors.
This plugin is part of a monorepo rehype-github
.
See its readme for more info.
You can use this plugin when you want to match how github.com works or when you
want to build similar pipelines that have user content.
You should likely use this in combination with an image proxy,
such as
camomile
(Node.js)
or
go-camo
(Go).
This package is ESM only. In Node.js (version 16+), install with npm:
npm install rehype-github-image
In Deno with esm.sh
:
import rehypeGithubImage from 'https://esm.sh/rehype-github-image@1'
In browsers with esm.sh
:
<script type="module">
import rehypeGithubImage from 'https://esm.sh/rehype-github-image@1?bundle'
</script>
Say our module example.js
looks as follows:
import rehypeGithubImage from 'rehype-github-image'
import rehypeParse from 'rehype-parse'
import rehypeStringify from 'rehype-stringify'
import {unified} from 'unified'
const file = await unified()
.use(rehypeParse, {fragment: true})
.use(rehypeGithubImage)
.use(rehypeStringify)
.process('<img src="https://example.com/index.png">')
console.log(String(file))
…now running node example.js
yields:
<p><a target="_blank" rel="noopener noreferrer" href="https://example.com/index.png"><img src="https://example.com/index.png" style="max-width: 100%;"></a></p>
This package exports the identifier camo
.
The default export is
rehypeGithubImage
.
Create a toProxyUrl
for a camo server.
See for example:
path
(string
, required) — where the camo server runs (such ashttps://camo.githubusercontent.com
)secret
(string
, required) — shared secret with your camo server (such asmyVerySecretSecret
)
Function to create a URL to a proxy from an external URL
(ToProxyUrl
).
Plugin to enhance images.
options
(Options
, optional) — configuration
Configuration (TypeScript type).
toProxyUrl
(ToProxyUrl
, optional) — change external URLs to go through an image proxyinternal
(Array<string>
orstring
, optional) — hostname or hostnames to not mark as external; URLs to these hostnames will not be passed through the image proxyrel
(Array<string>
orstring
, default:['noopener', 'noreferrer']
) — relationship(s) of your site to external content, used inrel
ona
s wrapping the images; norel
field is set on URLs that go to your image proxytargetBlank
(boolean
, default:true
) — whether to open images in a new window
These options are safe by default,
but you should change them.
You should likely include 'nofollow'
and 'ugc'
in rel
.
If you have targetBlank: true
(default),
make sure to include 'noopener'
and 'noreferrer'
(default).
👉 Note: to summarize, with
targetBlank: false
, userel: ['nofollow', 'ugc']
. WithtargetBlank: true
(default), userel: ['nofollow', 'noopener', 'noreferrer', 'ugc']
.
Create a URL to a proxy from an external URL (TypeScript type).
url
(string
) — URL to hash
URL to proxy (Promise<string>
or string
).
There are no bugs with how GitHub does this,
but they drop the target
and use ['nofollow', 'ugc']
in the rel
.
There are no additional recommendations on how to author links in markdown.
The markup that github.com uses for invalid URLs is:
<img src="" alt="" style="max-width: 100%;">
For valid URLs,
they keep the value in src
:
<img src="../image.jpg" alt="" style="max-width: 100%;">
If the image is not in an a
element,
they add one:
<a target="_blank" rel="noopener noreferrer" href="image.jpg"><img src="image.jpg" alt="alt" style="max-width: 100%;"></a>
If the image goes to some domain,
that isn’t http://github.com
(or https:
),
they pass the image through a camo image proxy:
<a target="_blank" href="https://camo.githubusercontent.com/559e4923433749bd3cd9c1e4ddb7317442c7ca8e836e2a843189d13e264c9ff2/68747470733a2f2f6578616d706c652e636f6d"><img src="https://camo.githubusercontent.com/559e4923433749bd3cd9c1e4ddb7317442c7ca8e836e2a843189d13e264c9ff2/68747470733a2f2f6578616d706c652e636f6d" data-canonical-src="https://example.com" style="max-width: 100%;"></a>
These urls have the following format:
<base>/<digest>/<hex>
…where hex
is the hex encoded original URL,
digest
is the hex encoded HMAC digest generated with a shared secret key and
the original URL,
and base
is the path where camo is running
(such as https://camo.githubusercontent.com
).
No CSS is needed.
No syntax is applicable.
This package is fully typed with TypeScript.
It exports the additional type Options
and
ToProxyUrl
.
Projects maintained by the unified collective are compatible with all maintained versions of Node.js. As of now, that is Node.js 16+. Our projects sometimes work with older versions, but this is not guaranteed.
This plugin works with rehype-parse
version 3+,
rehype-stringify
version 3+,
rehype
version 5+,
and unified
version 6+.
This package is safe.
remark-gfm
— support GFM in remarkrehype-external-links
— similar plugin
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.
This project is not affiliated with GitHub.