Debian Codegraph @

www/src/lib/shiki-transformers.js

raw symbols SCIP
1 /**
2 * Shiki transformer that adds a macOS-style header with dots and a copy button to code blocks
3 */
4 export const codeBlockTransformer = {
5 pre(node) {
6 // Add 'group' class to pre for hover effects
7 const existingClasses = node.properties.class || '';
8 node.properties.class = `${existingClasses} group`.trim();
9
10 // Add macOS-style header with dots
11 /** @type {import('hast').Element} */
12 const header = {
13 type: 'element',
14 tagName: 'div',
15 properties: {
16 class: 'sticky left-0 z-10 flex gap-1.5 px-3 py-2 bg-neutral-50 dark:bg-neutral-920 border-b border-neutral-100 dark:border-neutral-900 rounded-t-[3px]'
17 },
18 children: [
19 {
20 type: 'element',
21 tagName: 'div',
22 properties: {class: 'size-2.25 rounded-full bg-neutral-200 dark:bg-neutral-800'},
23 children: []
24 },
25 {
26 type: 'element',
27 tagName: 'div',
28 properties: {class: 'size-2.25 rounded-full bg-neutral-200 dark:bg-neutral-800'},
29 children: []
30 },
31 {
32 type: 'element',
33 tagName: 'div',
34 properties: {class: 'size-2.25 rounded-full bg-neutral-200 dark:bg-neutral-800'},
35 children: []
36 }
37 ]
38 };
39
40 // Add copy button (visible on mobile, hover on desktop)
41 /** @type {import('hast').Element} */
42 const button = {
43 type: 'element',
44 tagName: 'button',
45 properties: {
46 class: 'z-20 absolute top-9 right-2 size-8 flex items-center justify-center select-none bg-white dark:bg-neutral-910 border border-neutral-200 dark:border-neutral-800 rounded-[0.5rem] opacity-100 lg:opacity-0 data-copied:opacity-100 group-interact:opacity-100 focus-visible:opacity-100 transition text-neutral-600 dark:text-neutral-400 interact:bg-neutral-75 dark:interact:bg-neutral-875 interact:border-neutral-400 dark:interact:border-neutral-600 interact:text-neutral-900 dark:interact:text-white outline-hidden focus-visible:ring-1 focus-visible:ring-offset-1 focus-visible:ring-neutral-500 dark:focus-visible:ring-neutral-400 cursor-pointer before:content-["Copied!"] before:absolute before:right-full before:top-1/2 before:-translate-y-1/2 before:mr-2 before:px-2 before:py-1 before:text-xs before:font-semibold before:text-white before:bg-neutral-900 dark:before:bg-neutral-100 dark:before:text-neutral-900 before:rounded before:whitespace-nowrap before:opacity-0 before:pointer-events-none data-copied:before:opacity-100 before:transition',
47 _: `on click
48 -- Note: iOS Safari requires HTTPS for clipboard API (won't work on localhost)
49 writeText(textContent of last <code/> in closest <pre/>) into navigator.clipboard
50 add .icon-\\[radix-icons--check\\] to first <i/> in me
51 remove .icon-\\[radix-icons--copy\\] from first <i/> in me
52 set @title of me to 'Copied!'
53 add @data-copied
54 wait 2s
55 add .icon-\\[radix-icons--copy\\] to first <i/> in me
56 remove .icon-\\[radix-icons--check\\] from first <i/> in me
57 set @title of me to 'Copy to clipboard'
58 remove @data-copied`,
59 title: 'Copy to clipboard'
60 },
61 children: [
62 {
63 type: 'element',
64 tagName: 'i',
65 properties: {
66 class: 'size-4 icon-[radix-icons--copy]',
67 },
68 children: []
69 }
70 ]
71 };
72
73 // Make pre unfocusable (code inside is the scrollable element)
74 node.properties.tabindex = '-1';
75
76 // Add header and button as first children
77 // @ts-ignore - type inference issue with hast nodes
78 node.children.unshift(button, header);
79 }
80 };