dependabot/npm_and_yarn/prettier-3.7.4
copilot/add-i18n-support-for-english
dependabot/npm_and_yarn/astro-5.16.4
dependabot/npm_and_yarn/katex-0.16.27
dependabot/npm_and_yarn/prettier-3.7.4
dependabot/npm_and_yarn/tailwindcss-4.1.16
dependabot/npm_and_yarn/tailwindcss/vite-4.1.16
dependabot/npm_and_yarn/typescript-eslint-8.46.2
dependabot/npm_and_yarn/typescript-eslint/parser-8.46.2
development
main
| 1 | import { visit } from 'unist-util-visit' |
| 2 | |
| 3 | /** |
| 4 | * Rehype plugin that adds copy button to code blocks for easy code copying functionality |
| 5 | */ |
| 6 | export 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 | } |