File tree Expand file tree Collapse file tree 2 files changed +14
-3
lines changed
packages/astro-theme/components/search Expand file tree Collapse file tree 2 files changed +14
-3
lines changed Original file line number Diff line number Diff line change 7575 {:else }
7676 <ol class =" flex flex-col gap-2" >
7777 {#each results as result (result .id )}
78- <Result {result } />
78+ <Result {result } { baseUrl } />
7979 {/each }
8080 </ol >
8181 {/if }
Original file line number Diff line number Diff line change 11<script lang =" ts" >
22 export let result: PagefindResult ;
3+ export let baseUrl: string = " /" ;
34
45 const showExcerpt = false ; // Makes the UI quite busy.
56
7+ // Add base path (e.g., /docs/) to search result URLs for browser routing.
8+ // Only does so if base isnt root. If it is docs would be a subdir and be already in URL
9+ // Also checks if url already starts with base for extra defense against /docs/docs
10+ function prependBase(url : string ): string {
11+ if (baseUrl === " /" || url .startsWith (baseUrl )) {
12+ return url ;
13+ }
14+ return baseUrl + url ;
15+ }
16+
617 // Based on thin_sub_results from https://github.com/CloudCannon/pagefind/blob/main/pagefind_ui/default/svelte/result_with_subs.svelte
718 function thinSubResults(
819 pageUrl : string ,
3849{:then document }
3950 <li class =" rounded-md bg-stone-900" >
4051 <a
41- href ={document .url }
52+ href ={prependBase ( document .url ) }
4253 class =" rounded-t-md block border border-transparent hover:border-stone-500 py-3 px-4 text-lg"
4354 class:rounded-b-md ={document .sub_results .length <= 1 }
4455 >
5768 {#each document .sub_results as subResult , i }
5869 <li >
5970 <a
60- href ={subResult .url }
71+ href ={prependBase ( subResult .url ) }
6172 class =" block border border-transparent hover:border-stone-500 text-stone-300 py-2 px-4 whitespace-nowrap text-ellipsis overflow-hidden"
6273 class:rounded-b-md ={i === document .sub_results .length - 1 }
6374 >
You can’t perform that action at this time.
0 commit comments