-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
37 lines (32 loc) · 1013 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { visit } from "unist-util-visit";
import { h } from "hastscript";
export default function rehypeFigureTitle(option) {
const className = (option && option.className) || "rehype-figure-title";
function buildFigure({ properties }) {
const figure = h("figure", { class: className }, [
h("img", { ...properties, title: null }),
properties.title && properties.title.trim().length > 0
? h("figcaption", properties.title)
: "",
]);
return figure;
}
return function (tree) {
visit(tree, { tagName: "p" }, (node, index) => {
const images = node.children
.filter((n) => n.tagName === "img")
.map((img) => buildFigure(img));
if (images.length === 0) return;
tree.children[index] =
images.length === 1
? images[0]
: (tree.children[index] = h(
"div",
{
class: `${className}-container`,
},
images,
));
});
};
}