Skip to content

Commit df4b722

Browse files
authored
Merge pull request #51 from ty-coud/fix-pagefind-links
Ensure pagefind links conform to trailingSlashes setting
2 parents f57fcc5 + 7ef5daf commit df4b722

7 files changed

Lines changed: 37 additions & 5 deletions

File tree

astro.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import rehypePixelated from './src/plugins/rehype-pixelated' /* Custom plugin to
2525
// https://astro.build/config
2626
export default defineConfig({
2727
site: siteConfig.site,
28-
trailingSlash: 'never',
28+
trailingSlash: siteConfig.trailingSlashes ? 'always' : 'never',
2929
prefetch: true,
3030
markdown: {
3131
remarkPlugins: [

src/components/Header.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const selectTheme =
2020
{siteConfig.title}
2121
</a>
2222
<div class="flex items-center gap-3 sm:mr-3">
23-
<Search />
23+
<Search trailingSlashes={siteConfig.trailingSlashes} />
2424
{lightDarkAutoTheme && <LightDarkAutoButton />}
2525
{selectTheme && <SelectTheme />}
2626
<nav id="nav-mobile" aria-label="Menu" class="p-0 text-accent sm:hidden">

src/components/Search.astro

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
import '@pagefind/default-ui/css/ui.css'
33
import IconCircleX from '~/icons/circle-x.svg'
44
import IconSearch from '~/icons/search.svg'
5+
6+
const { trailingSlashes = false } = Astro.props
57
---
68

7-
<site-search class="ms-auto" id="search">
9+
<site-search class="ms-auto" id="search" data-trailing-slashes={trailingSlashes}>
810
<button
911
class="hover:text-accent flex cursor-pointer items-center justify-center rounded-md"
1012
aria-keyshortcuts="Control+K Meta+K"
@@ -85,6 +87,7 @@ import IconSearch from '~/icons/search.svg'
8587
#dialogFrame: HTMLDivElement | null
8688
#openBtn: HTMLButtonElement | null
8789
#controller: AbortController
90+
trailingSlashes: boolean
8891

8992
constructor() {
9093
super()
@@ -94,6 +97,12 @@ import IconSearch from '~/icons/search.svg'
9497
this.#dialogFrame = this.querySelector('.dialog-frame')
9598
this.#controller = new AbortController()
9699

100+
this.trailingSlashes = this.dataset.trailingSlashes === 'true' ? true : false
101+
this.stripTrailingSlash = (path: string) => path.replace(/(.)\/(#.*)?$/, '$1$2')
102+
this.formatURL = !this.trailingSlashes
103+
? this.stripTrailingSlash
104+
: (path: string) => path
105+
97106
// Set up events
98107
if (this.#openBtn) {
99108
this.#openBtn.addEventListener('click', this.openModal)
@@ -121,12 +130,27 @@ import IconSearch from '~/icons/search.svg'
121130
const onIdle = window.requestIdleCallback || ((cb) => setTimeout(cb, 1))
122131
onIdle(async () => {
123132
const { PagefindUI } = await import('@pagefind/default-ui')
133+
124134
new PagefindUI({
125135
baseUrl: import.meta.env.BASE_URL,
126136
bundlePath: import.meta.env.BASE_URL.replace(/\/$/, '') + '/pagefind/',
127137
element: '#pagefind-search',
128138
showImages: false,
129139
showSubResults: true,
140+
processResult: (result: {
141+
url: string
142+
sub_results: Array<{ url: string }>
143+
}) => {
144+
// Ensure links in search results match the trailing slashes setting
145+
result.url = this.formatURL(result.url)
146+
147+
result.sub_results = result.sub_results.map((res) => {
148+
res.url = this.formatURL(res.url)
149+
return res
150+
})
151+
152+
return result
153+
},
130154
})
131155
})
132156
}

src/pages/about.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ layout: '~/layouts/MarkdownLayout.astro'
33
title: About Me
44
---
55

6-
The laughing kookaburra (Dacelo novaeguineae) is a bird in the kingfisher subfamily Halcyoninae. It is a large robust kingfisher with a whitish head and a brown eye-stripe. The upperparts are mostly dark brown but there is a mottled light-blue patch on the wing coverts. The underparts are cream-white and the tail is barred with rufous and black. The plumage of the male and female birds is similar. The territorial call is a distinctive laugh that is often delivered by several birds at the same time, and is widely used as a stock sound effect in situations that involve a jungle setting.
6+
The laughing kookaburra (Dacelo novaeguineae) is a bird in the kingfisher subfamily Halcyoninae. It is a large robust kingfisher with a whitish head and a brown eye-stripe. The upperparts are mostly dark brown but there is a mottled light-blue patch on the wing coverts. The underparts are cream-white and the tail is barred with rufous and black. The plumage of the male and female birds is similar. The territorial call is a distinctive laugh that is often delivered by several birds at the same time, and is widely used as a stock sound effect in situations that involve a jungle setting.

src/pages/social-cards/[slug].png.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ const defaultTheme =
3131
? siteConfig.themes.include[0]
3232
: siteConfig.themes.default
3333

34-
const themeStyles = await resolveThemeColorStyles([defaultTheme], siteConfig.themes.overrides)
34+
const themeStyles = await resolveThemeColorStyles(
35+
[defaultTheme],
36+
siteConfig.themes.overrides,
37+
)
3538
const bg = themeStyles[defaultTheme]?.background
3639
const fg = themeStyles[defaultTheme]?.foreground
3740
const accent = themeStyles[defaultTheme]?.accent

src/site.config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ const config: SiteConfig = {
2222
// For pagination, the number of posts to display per page.
2323
// The homepage will display half this number in the "Latest Posts" section.
2424
pageSize: 6,
25+
// Whether Astro should resolve trailing slashes in URLs or not.
26+
// This value is used in the astro.config.mjs file and in the "Search" component to make sure pagefind links match this setting.
27+
// It is not recommended to change this, since most links existing in the site currently do not have trailing slashes.
28+
trailingSlashes: false,
2529
// The navigation links to display in the header.
2630
navLinks: [
2731
{

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ export interface SiteConfig {
173173
socialCardAvatarImage: string
174174
tags: string[]
175175
pageSize: number
176+
trailingSlashes: boolean
176177
themes: ThemesConfig
177178
socialLinks: SocialLinks
178179
navLinks: NavLink[]

0 commit comments

Comments
 (0)