Skip to content

Commit

Permalink
feat: v2
Browse files Browse the repository at this point in the history
  • Loading branch information
naiyerasif committed Aug 27, 2023
0 parents commit 9431e31
Show file tree
Hide file tree
Showing 9 changed files with 1,938 additions and 0 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/regression.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: regression

on:
push:
branches:
- main

jobs:
regression:
runs-on: ubuntu-latest

steps:
- name: Checkout source
uses: actions/checkout@v2

- name: Setup PNPM
uses: pnpm/action-setup@v2
with:
version: 8.7.0

- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 20
cache: 'pnpm'

- name: Install dependencies
run: pnpm install --frozen-lockfile

- name: Run the tests
run: pnpm test
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
.DS_Store
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# MIT License

Copyright © 2023 — present Naiyer Asif

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
100 changes: 100 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# rehype-figure

[![npm](https://img.shields.io/npm/v/@microflash/rehype-figure)](https://www.npmjs.com/package/@microflash/rehype-figure)
[![regression](https://github.com/Microflash/rehype-figure/actions/workflows/regression.yml/badge.svg)](https://github.com/Microflash/rehype-figure/actions/workflows/regression.yml)
[![license](https://img.shields.io/npm/l/@microflash/rehype-figure)](./LICENSE.md)

[rehype](https://github.com/rehypejs/rehype) plugin to transform an image with alt text to a figure with caption

- [What's this?](#whats-this)
- [Install](#install)
- [Use](#use)
- [API](#api)
- [License](#license)

## What's this?

This package is a [unified](https://github.com/unifiedjs/unified) ([rehype](https://github.com/rehypejs/rehype)) plugin that takes an image node with alt text (e.g., `![Alt text](path-to-image.jpg)`) and converts it to a [figure](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/figure) element with caption.

```html
<figure>
<img src="path-to-image.jpg" alt="Alt text">
<figcaption>Alt Text</figcaption>
</figure>
```

## Install

This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c).

In Node.js (16.0+), install with [npm](https://docs.npmjs.com/cli/install):

```sh
npm install @microflash/rehype-figure
```

> For Node.js versions below 16.0, stick to 1.x.x versions of this plugin.
In Deno, with [esm.sh](https://esm.sh/):

```js
import rehypeFigure from "https://esm.sh/@microflash/rehype-figure";
```

In browsers, with [esm.sh](https://esm.sh/):

```html
<script type="module">
import rehypeFigure from "https://esm.sh/@microflash/rehype-figure?bundle";
</script>
```

## Use

Say we have the following module `example.js`:

```js
import { unified } from "unified";
import remarkParse from "remark-parse";
import remarkGfm from "remark-gfm";
import remarkRehype from "remark-rehype";
import rehypeFigure from "@microflash/rehype-figure";
import rehypeStringify from "rehype-stringify";

main()

async function main() {
const file = await unified()
.use(remarkParse)
.use(remarkGfm)
.use(remarkRehype)
.use(rehypeFigure)
.use(rehypeStringify)
.process("![Alt text](path-to-image.jpg)");

console.log(String(file));
}
```

Running that with `node example.js` yields:

```html
<figure>
<img src="path-to-image.jpg" alt="Alt Text">
<figcaption>Alt Text</figcaption>
</figure>
```

## API

The default export is `rehypeFigure`.

The following options are available. All of them are optional.

- `className`: class for the wrapped `figure` element

By default, no classes are added to the `figure` element.

## License

[MIT](./LICENSE.md)
58 changes: 58 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { visit } from "unist-util-visit";
import { whitespace } from "hast-util-whitespace";
import { remove } from "unist-util-remove";
import { h } from "hastscript";

export default function rehypeFigure(options = {}) {
return (tree) => {
// unwrap the images inside the paragraph
visit(tree, { tagName: "p" }, (node, index, parent) => {
if (!hasOnlyImages(node)) {
return;
}

remove(node, "text");

parent.children.splice(index, 1, ...node.children);

return index;
});

// wrap images in figure
visit(tree, (node) => isImageWithAlt(node), (node, index, parent) => {
if (isImageWithCaption(parent) || isImageLink(parent)) {
return;
}

const figure = createFigure(node, options);

node.tagName = figure.tagName;
node.children = figure.children;
node.properties = figure.properties;
});
};
}

function hasOnlyImages({ children }) {
return children.every((child) => child.tagName === "img" || whitespace(child));
}

function isImageWithAlt({ tagName, properties }) {
return tagName === "img" && Boolean(properties.alt) && Boolean(properties.src);
}

function isImageWithCaption({ tagName, children }) {
return tagName === "figure" && children.some((child) => child.tagName === "figcaption");
}

function isImageLink({ tagName }) {
return tagName === "a";
}

function createFigure({ properties }, options) {
const props = options.className ? { class: options.className } : {};
return h("figure", props, [
h("img", { ...properties }),
h("figcaption", properties.alt)
]);
}
45 changes: 45 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"name": "@microflash/rehype-figure",
"version": "2.0.0",
"description": "rehype plugin to transform an image with alt text to a figure with caption",
"license": "MIT",
"keywords": [
"unified",
"rehype",
"rehype-plugin",
"plugin",
"figure",
"caption",
"image"
],
"repository": "https://github.com/Microflash/rehype-figure.git",
"bugs": "https://github.com/Microflash/rehype-figure/issues",
"homepage": "https://github.com/Microflash/rehype-figure#readme",
"author": "Naiyer Asif (https://www.naiyerasif.com)",
"type": "module",
"main": "index.js",
"files": [
"index.js"
],
"scripts": {
"test": "ava"
},
"dependencies": {
"hast-util-whitespace": "^3.0.0",
"hastscript": "^8.0.0",
"unist-util-remove": "^4.0.0",
"unist-util-visit": "^5.0.0"
},
"devDependencies": {
"ava": "^5.3.1",
"rehype-stringify": "^10.0.0",
"remark-parse": "^10.0.2",
"remark-rehype": "^10.1.0",
"unified": "^11.0.2"
},
"ava": {
"files": [
"test/**/*.test.js"
]
}
}

0 comments on commit 9431e31

Please sign in to comment.