mirror of
https://github.com/reonokiy/blog.nokiy.net.git
synced 2025-06-16 19:51:07 +02:00
style: add image to figure plugin
- Improve styles for images, blockquotes, tables and more - Fix unexpected line breaks
This commit is contained in:
parent
018f1c9b6c
commit
4ff44f9ac4
14 changed files with 126 additions and 59 deletions
29
src/plugins/rehype-img-to-figure.mjs
Normal file
29
src/plugins/rehype-img-to-figure.mjs
Normal 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 }],
|
||||
},
|
||||
]
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue