mirror of
https://github.com/reonokiy/blog.nokiy.net.git
synced 2025-06-15 19:22:52 +02:00
23 lines
600 B
JavaScript
23 lines
600 B
JavaScript
import { visit } from 'unist-util-visit'
|
|
|
|
export function rehypeUnwrapImg() {
|
|
return (tree) => {
|
|
visit(tree, 'element', (node, index, parent) => {
|
|
if (
|
|
node.tagName === 'p'
|
|
&& node.children
|
|
&& parent
|
|
&& node.children.every(child =>
|
|
child.tagName === 'img'
|
|
|| (child.type === 'text' && child.value.trim() === ''),
|
|
)
|
|
) {
|
|
|
|
const imgNodes = node.children.filter(child => child.tagName === 'img')
|
|
if (imgNodes.length > 0) {
|
|
parent.children.splice(index, 1, ...imgNodes)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|