@@ -3,41 +3,66 @@ defineProps<{
33 html: string
44}>()
55
6+ const router = useRouter ()
67const { copy } = useClipboard ()
78
8- const handleCopy = async (e : MouseEvent ) => {
9- const target = (e .target as HTMLElement ).closest (' [data-copy]' )
9+ // Combined click handler for:
10+ // 1. Intercepting npmjs.com links to route internally
11+ // 2. Copy button functionality for code blocks
12+ function handleClick(event : MouseEvent ) {
13+ const target = event .target as HTMLElement | undefined
1014 if (! target ) return
1115
12- const wrapper = target .closest (' .readme-code-block' )
13- if (! wrapper ) return
16+ // Handle copy button clicks
17+ const copyTarget = target .closest (' [data-copy]' )
18+ if (copyTarget ) {
19+ const wrapper = copyTarget .closest (' .readme-code-block' )
20+ if (! wrapper ) return
1421
15- const pre = wrapper .querySelector (' pre' )
16- if (! pre ?.textContent ) return
22+ const pre = wrapper .querySelector (' pre' )
23+ if (! pre ?.textContent ) return
1724
18- await copy (pre .textContent )
25+ copy (pre .textContent )
1926
20- const icon = target .querySelector (' span' )
21- if (! icon ) return
27+ const icon = copyTarget .querySelector (' span' )
28+ if (! icon ) return
2229
23- const originalIcon = ' i-carbon:copy'
24- const successIcon = ' i-carbon:checkmark'
30+ const originalIcon = ' i-carbon:copy'
31+ const successIcon = ' i-carbon:checkmark'
2532
26- icon .classList .remove (originalIcon )
27- icon .classList .add (successIcon )
33+ icon .classList .remove (originalIcon )
34+ icon .classList .add (successIcon )
2835
29- setTimeout (() => {
30- icon .classList .remove (successIcon )
31- icon .classList .add (originalIcon )
32- }, 2000 )
36+ setTimeout (() => {
37+ icon .classList .remove (successIcon )
38+ icon .classList .add (originalIcon )
39+ }, 2000 )
40+ return
41+ }
42+
43+ // Handle npmjs.com link clicks - route internally
44+ const anchor = target .closest (' a' )
45+ if (! anchor ) return
46+
47+ const href = anchor .getAttribute (' href' )
48+ if (! href ) return
49+
50+ const match = href .match (/ ^ (?:https? :\/\/ )? (?:www\. )? npmjs\. (?:com| org)(\/ . + )$ / )
51+ if (! match || ! match [1 ]) return
52+
53+ const route = router .resolve (match [1 ])
54+ if (route ) {
55+ event .preventDefault ()
56+ router .push (route )
57+ }
3358}
3459 </script >
3560
3661<template >
3762 <article
3863 class =" readme prose prose-invert max-w-[70ch] lg:max-w-none"
3964 v-html =" html"
40- @click =" handleCopy "
65+ @click =" handleClick "
4166 />
4267</template >
4368
0 commit comments