Skip to content

Commit d61b472

Browse files
bluwydanielroe
andauthored
feat: use router for import links in package code (#1307)
Co-authored-by: Daniel Roe <daniel@roe.dev>
1 parent f4832f7 commit d61b472

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

app/components/Code/Viewer.vue

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,31 @@ watch(
5757
},
5858
{ immediate: true },
5959
)
60+
61+
// Use Nuxt's `navigateTo` for the rendered import links
62+
function handleImportLinkNavigate() {
63+
if (!codeRef.value) return
64+
65+
const anchors = codeRef.value.querySelectorAll('a.import-link')
66+
anchors.forEach(anchor => {
67+
// NOTE: We do not need to remove previous listeners because we re-create the entire HTML content on each html update
68+
anchor.addEventListener('click', event => {
69+
const href = anchor.getAttribute('href')
70+
if (href) {
71+
event.preventDefault()
72+
navigateTo(href)
73+
}
74+
})
75+
})
76+
}
77+
78+
watch(
79+
() => props.html,
80+
() => {
81+
nextTick(handleImportLinkNavigate)
82+
},
83+
{ immediate: true },
84+
)
6085
</script>
6186

6287
<template>

0 commit comments

Comments
 (0)