UNPKG

rehype-figure

Version:

[![Build Status](https://travis-ci.com/josestg/rehype-figure.svg?token=1ZtvVXXQrZXVL8domfez&branch=master)](https://travis-ci.com/josestg/rehype-figure)

38 lines (31 loc) 959 B
const visit = require("unist-util-visit") const h = require("hastscript") function rehypeFigure(option) { const className = (option && option.className) || "rehype-figure" function buildFigure({ properties }) { const figure = h("figure", { class: className }, [ h("img", { ...properties }), properties.alt && properties.alt.trim().length > 0 ? h("figcaption", properties.alt) : "", ]) 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 )) }) } } module.exports = rehypeFigure