dependabot/npm_and_yarn/astrojs/mdx-7.0.8
dependabot/npm_and_yarn/astro-7.4.0
dependabot/npm_and_yarn/astrojs/mdx-7.0.8
dependabot/npm_and_yarn/astrojs/sitemap-3.7.4
dependabot/npm_and_yarn/katex-0.19.0
dependabot/npm_and_yarn/prettier-3.9.10
dependabot/npm_and_yarn/sharp-0.35.5
dependabot/npm_and_yarn/tsx-4.23.15
dependabot/npm_and_yarn/typescript-eslint-8.71.2
dependabot/npm_and_yarn/typescript-eslint/parser-8.71.2
development
main
| 1 | import { visit } from 'unist-util-visit' |
| 2 | |
| 3 | /** |
| 4 | * Rehype plugin to cleanup and extract raw figure elements from paragraph nodes |
| 5 | */ |
| 6 | export default function rehypeCleanup() { |
| 7 | return (tree) => { |
| 8 | visit(tree, 'element', (node, index, parent) => { |
| 9 | if (node.tagName !== 'p') { |
| 10 | return |
| 11 | } |
| 12 | if (!node.children?.length) { |
| 13 | return |
| 14 | } |
| 15 | if (!parent) { |
| 16 | return |
| 17 | } |
| 18 | |
| 19 | const rawFigureNodes = [] |
| 20 | |
| 21 | for (const child of node.children) { |
| 22 | if (child.type === 'raw' && child.value && child.value.trim().startsWith('<figure')) { |
| 23 | rawFigureNodes.push(child) |
| 24 | } else if (child.type !== 'text' || child.value.trim() !== '') { |
| 25 | return |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | if (rawFigureNodes.length > 0) { |
| 30 | parent.children.splice(index, 1, ...rawFigureNodes) |
| 31 | return index |
| 32 | } |
| 33 | }) |
| 34 | } |
| 35 | } |