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 e99799aa3072bcee1528d71fef722dc197e4d67a 38 lines 1.0 kB view raw
1import { visit } from 'unist-util-visit' 2 3/** 4 * Rehype plugin that adds copy button to code blocks for easy code copying functionality 5 */ 6export default function rehypeCopyCode() { 7 return (tree) => { 8 visit(tree, 'element', (node) => { 9 if (node.tagName === 'pre') { 10 if (!node.children || node.children.length === 0) { 11 return 12 } 13 14 const codeElement = node.children.find((child) => child.tagName === 'code') 15 if (!codeElement) { 16 return 17 } 18 19 node.properties = node.properties || {} 20 node.properties.className = node.properties.className || [] 21 node.properties.className.push('copy-code-block') 22 23 const copyButton = { 24 type: 'element', 25 tagName: 'button', 26 properties: { 27 className: ['copy-button'], 28 type: 'button', 29 'aria-label': 'Copy code' 30 }, 31 children: [] 32 } 33 34 node.children.unshift(copyButton) 35 } 36 }) 37 } 38}