1+ a
12<script setup lang="ts">
23import docsearch from ' @docsearch/js'
34
@@ -15,7 +16,17 @@ function initialize() {
1516const options= Object .assign ({},userOptions , {
1617 navigator: {
1718 navigate({itemUrl }: { itemUrl: string }) {
18- router .push (itemUrl )
19+ const {pathname : hitPathname }= new URL (
20+ window .location .origin + itemUrl
21+ )
22+
23+ // router doesn't handle same-page navigation so we use the native
24+ // browser location API for anchor navigation
25+ if (route .path === hitPathname ) {
26+ window .location .assign (window .location .origin + itemUrl )
27+ }else {
28+ router .push (itemUrl )
29+ }
1930 },
2031 },
2132
@@ -35,6 +46,7 @@ function initialize() {
3546
3647 props: {
3748 href:hit .url ,
49+ children ,
3850
3951 onClick(event : MouseEvent ) {
4052if (isSpecialClick (event ))return
@@ -51,29 +63,41 @@ function initialize() {
5163// behavior to leverage the Router loading feature.
5264if (route .path !== hitPathname )event .preventDefault ()
5365
54- router .push (hit .url )
66+ // router doesn't handle same-page navigation so we use the native
67+ // browser location API for anchor navigation
68+ if (route .path === hitPathname ) {
69+ window .location .assign (window .location .origin + hit .url )
70+ }else {
71+ router .push (hit .url )
72+ }
5573 },
56-
57- children ,
5874 },
5975 }
6076 },
6177 })
6278
79+ // @ts-ignore: Unreachable code error
6380docsearch (options )
6481}
6582
83+ /**
84+ * Check if event is special click to avoid closing the DocSearch too soon.
85+ */
6686const isSpecialClick= (event : MouseEvent )=>
6787event .button === 1 ||
6888event .altKey ||
6989event .ctrlKey ||
7090event .metaKey ||
7191event .shiftKey
7292
93+ /**
94+ * Gets the relative path from an absolute URL provided by the DocSearch instance.
95+ */
7396const getRelativePath= (absoluteUrl : string )=> {
7497const { pathname, hash }= new URL (absoluteUrl )
75-
76- return pathname + hash
98+ const url= window .location .origin
99+ const relativeUrl= pathname .replace (url ,' /' )+ hash
100+ return relativeUrl .replace (/ \/ + $ / ,' ' )
77101}
78102
79103onMounted (()=> {