Fork of Chiri for Astro for my blog
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

at a1e6ead60fb44c152fefa3a86179bcf2b84d6249 67 lines 2.0 kB view raw
1import { visit } from 'unist-util-visit' 2 3/** 4 * Rehype plugin to cleanup and extract raw figure elements from paragraph nodes 5 */ 6export default function rehypeCleanup() { 7 return (tree) => { 8 visit(tree, 'element', (node, index, parent) => { 9 // 1. Task List Item Fix: Remove leading space after checkbox 10 if (node.tagName === 'li' && node.properties?.className?.includes('task-list-item')) { 11 const children = node.children 12 let inputIndex = -1 13 14 // Find the checkbox input 15 for (let i = 0; i < children.length; i++) { 16 const child = children[i] 17 if (child.type === 'element' && child.tagName === 'input' && child.properties?.type === 'checkbox') { 18 inputIndex = i 19 break 20 } 21 } 22 23 if (inputIndex !== -1) { 24 // Check key siblings after the checkbox for the text node 25 for (let i = inputIndex + 1; i < children.length; i++) { 26 const child = children[i] 27 if (child.type === 'comment') continue 28 if (child.type === 'text') { 29 if (child.value.startsWith(' ')) { 30 child.value = child.value.replace(/^\s+/, '') 31 } 32 break 33 } else { 34 break 35 } 36 } 37 } 38 } 39 40 // 2. Figure Cleanup (Original Logic) 41 if (node.tagName !== 'p') { 42 return 43 } 44 if (!node.children?.length) { 45 return 46 } 47 if (!parent) { 48 return 49 } 50 51 const rawFigureNodes = [] 52 53 for (const child of node.children) { 54 if (child.type === 'raw' && child.value && child.value.trim().startsWith('<figure')) { 55 rawFigureNodes.push(child) 56 } else if (child.type !== 'text' || child.value.trim() !== '') { 57 return 58 } 59 } 60 61 if (rawFigureNodes.length > 0) { 62 parent.children.splice(index, 1, ...rawFigureNodes) 63 return index 64 } 65 }) 66 } 67}