Skip to content

Commit 1a48cfe

Browse files
garthdwdanielroe
andauthored
fix: move readme click overriding to readme component (#681)
Co-authored-by: Daniel Roe <daniel@roe.dev>
1 parent b0a0bd4 commit 1a48cfe

File tree

2 files changed

+44
-37
lines changed

2 files changed

+44
-37
lines changed

app/components/Readme.vue

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,66 @@ defineProps<{
33
html: string
44
}>()
55
6+
const router = useRouter()
67
const { 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

app/pages/package/[...package].vue

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -385,24 +385,6 @@ defineOgImageComponent('Package', {
385385
stars: () => stars.value ?? 0,
386386
primaryColor: '#60a5fa',
387387
})
388-
389-
// We're using only @click because it catches touch events and enter hits
390-
function handleClick(event: MouseEvent) {
391-
const target = (event?.target as HTMLElement | undefined)?.closest('a')
392-
if (!target) return
393-
394-
const href = target.getAttribute('href')
395-
if (!href) return
396-
397-
const match = href.match(/^(?:https?:\/\/)?(?:www\.)?npmjs\.(?:com|org)(\/.+)$/)
398-
if (!match || !match[1]) return
399-
400-
const route = router.resolve(match[1])
401-
if (route) {
402-
event.preventDefault()
403-
router.push(route)
404-
}
405-
}
406388
</script>
407389

408390
<template>
@@ -979,7 +961,7 @@ function handleClick(event: MouseEvent) {
979961
</a>
980962
</h2>
981963
<!-- eslint-disable vue/no-v-html -- HTML is sanitized server-side -->
982-
<Readme v-if="readmeData?.html" :html="readmeData.html" @click="handleClick" />
964+
<Readme v-if="readmeData?.html" :html="readmeData.html" />
983965
<p v-else class="text-fg-subtle italic">
984966
{{ $t('package.readme.no_readme') }}
985967
<a

0 commit comments

Comments
 (0)