@bradgarropy/rehype-image-links
TypeScript icon, indicating that this package has built-in type declarations

1.1.1Β β€’Β PublicΒ β€’Β Published

πŸ”— rehype image links

version downloads size github actions coverage typescript contributing contributors discord

Rehype plugin to wrap images in links.

πŸ“¦ Installation

This package is hosted on npm.

npm install @bradgarropy/rehype-image-links

πŸ₯‘ Usage

This is a rehype plugin for use in a unified chain that modifies HTML syntax trees. It wraps all img tags in a tags with a link to their original source.

import rehypeParse from "rehype-parse"
import rehypeStringify from "rehype-stringify"
import {unified} from "unified"

const processor = unified()
    .use(rehypeParse, {fragment: true})
    .use(rehypeImageLinks)
    .use(rehypeStringify)

const html = await processor.process(
    '<img src="https://example.com/photo.jpg" alt="alt text">',
)

console.log(file)

// output
// <a href="https://example.com/photo.jpg">
//     <img src="https://example.com/photo.jpg" alt="alt text" />
// </a>

πŸ“– API Reference

use(rehypeImageLinks, options)

The options object is optional. Here are all the available options.

Name Required Default Example Description
classes false [] ["link", "fancy"] List of classes to add to the a tag.
srcTransform false (url: string) => url (url: string) => url.toLowerCase() Function that modifies the src attribute on the img tag.
hrefTransform false (url: string) => url (url: string) => url.toLowerCase() Function that modifies the href attribute on the a tag.

If you provide the classes option, those classes will be combined into a string and added to the a tag. For example, if you provided {classes: ["link", "fancy"]} for the options, the result will look like this.

<!-- input -->
<img src="https://example.com/photo.jpg" alt="alt text" />

<!-- output -->
<a href="https://example.com/photo.jpg" class="link fancy">
    <img src="https://example.com/photo.jpg" alt="alt text" />
</a>

❔ Questions

πŸ› report bugs by filing issues
πŸ“’ provide feedback with issues or on twitter
πŸ™‹πŸΌβ€β™‚οΈ use my ama or twitter to ask any other questions

✨ contributors


Brad Garropy

πŸ’» πŸ“– ⚠️ πŸš‡

Package Sidebar

Install

npm i @bradgarropy/rehype-image-links

Weekly Downloads

8

Version

1.1.1

License

MIT

Unpacked Size

20.7 kB

Total Files

15

Last publish

Collaborators

  • bradgarropy