mirror of
https://github.com/reonokiy/blog.nokiy.net.git
synced 2025-06-16 19:51:07 +02:00
chore: add rehypeUnwrapImg plugin for image processing optimization, update katex resource path
This commit is contained in:
parent
110fad06ab
commit
55923f9fcc
8 changed files with 93 additions and 74 deletions
23
src/plugins/rehype-unwrap-img.mjs
Normal file
23
src/plugins/rehype-unwrap-img.mjs
Normal file
|
@ -0,0 +1,23 @@
|
|||
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)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue