style: add image to figure plugin

- Improve styles for images, blockquotes, tables and more
- Fix unexpected line breaks
This commit is contained in:
radishzzz 2025-04-16 06:25:04 +01:00
parent 018f1c9b6c
commit 4ff44f9ac4
14 changed files with 126 additions and 59 deletions

View file

@ -0,0 +1,29 @@
import { visit } from 'unist-util-visit'
export function rehypeImgToFigure() {
return (tree) => {
visit(tree, 'element', (node) => {
if (
node.tagName === 'p'
&& node.children
&& node.children.length === 1
&& node.children[0].tagName === 'img'
&& node.children[0].properties.alt
) {
const child = node.children[0]
const altText = child.properties.alt
node.tagName = 'figure'
node.children = [
child,
{
type: 'element',
tagName: 'figcaption',
properties: {},
children: [{ type: 'text', value: altText }],
},
]
}
})
}
}