mirror of
https://github.com/reonokiy/blog.nokiy.net.git
synced 2025-06-17 12:01:33 +02:00
feat: add code copy cutton
This commit is contained in:
parent
9c87c1bf03
commit
3312b30dbf
7 changed files with 132 additions and 5 deletions
37
src/plugins/rehype-code-copy-button.mjs
Normal file
37
src/plugins/rehype-code-copy-button.mjs
Normal file
|
@ -0,0 +1,37 @@
|
|||
import { SKIP, visit } from 'unist-util-visit'
|
||||
|
||||
export function rehypeCodeCopyButton() {
|
||||
return (tree) => {
|
||||
visit(tree, 'element', (node, index, parent) => {
|
||||
if (
|
||||
node.tagName === 'pre'
|
||||
&& node.children?.[0]?.tagName === 'code'
|
||||
&& parent
|
||||
&& !node.properties?.['data-copy-button-added']
|
||||
) {
|
||||
node.properties = node.properties || {}
|
||||
node.properties['data-copy-button-added'] = 'true'
|
||||
|
||||
parent.children[index] = {
|
||||
type: 'element',
|
||||
tagName: 'div',
|
||||
properties: { className: ['code-block-wrapper'] },
|
||||
children: [
|
||||
{
|
||||
type: 'element',
|
||||
tagName: 'button',
|
||||
properties: {
|
||||
'className': ['code-copy-button'],
|
||||
'type': 'button',
|
||||
'aria-label': 'Copy code',
|
||||
},
|
||||
children: [],
|
||||
},
|
||||
node,
|
||||
],
|
||||
}
|
||||
return SKIP
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue