refactor: update tag search URLs to use query parameters

This commit is contained in:
面条
2026-04-27 19:34:13 +08:00
parent f15d0bd8e2
commit 7d79eba756
5 changed files with 37 additions and 12 deletions

View File

@@ -48,7 +48,7 @@
- [Abner's memos](https://memos.abnerz6.top/)
- [小众软件的发现](https://talk.appinn.net/)
- [小报童优惠与排行榜](https://youhui.xiaobaoto.com/)
- [热干面拌 10 号土豆泥](https://memo.moran.im/)
- [热干面拌 10 号土豆泥](https://memo.moran.im/)
### 平台

View File

@@ -39,6 +39,7 @@ const tagStandaloneClass = 'pt-[30px]'
const tagLinkClass =
'inline-block rounded-[4px] border border-line px-[10px] py-[2px] text-muted no-underline hover:border-accent hover:text-accent hover:no-underline'
const commentsClass = 'ml-[3px] border-l-2 border-line pb-6 pl-[15px] pt-[6px] sm:pl-[30px]'
const getTagHref = (tag: string) => `/search/result?q=${encodeURIComponent(`#${tag}`)}`
---
<article class={articleClass} style={{ 'view-transition-name': `post-${post.id}` }}>
@@ -84,8 +85,8 @@ const commentsClass = 'ml-[3px] border-l-2 border-line pb-6 pl-[15px] pt-[6px] s
hasTags && (
<footer class:list={[tagBoxClass, tagPaddingClass, !hasContent && tagStandaloneClass]}>
<span class="tag-icon" aria-hidden="true" />
{post.tags.map((tag) => (
<a href={`/search/%23${tag}`} title={tag} class={tagLinkClass}>
{post.tags.map((tag: string) => (
<a href={getTagHref(tag)} title={tag} class={tagLinkClass}>
{tag}
</a>
))}

View File

@@ -484,7 +484,7 @@ async function extractPost($: CheerioAPI, item: AnyNode | null, options: Extract
const tagLink = $(tagNode)
const tagText = tagLink.text()
tagLink.attr('href', `/search/${encodeURIComponent(tagText)}`)
tagLink.attr('href', `/search/result?q=${encodeURIComponent(tagText)}`)
const normalizedTag = tagText.replace('#', '')
if (normalizedTag) {

View File

@@ -1,17 +1,37 @@
import { defineMiddleware } from 'astro:middleware'
function getEncodedTagSearchQuery(pathname: string): string {
if (!pathname.startsWith('/search/%23')) {
return ''
}
try {
return decodeURIComponent(pathname.slice('/search/'.length))
}
catch {
return ''
}
}
export const onRequest = defineMiddleware(async (context, next) => {
context.locals.SITE_URL = `${import.meta.env.SITE ?? ''}${import.meta.env.BASE_URL}`
context.locals.RSS_URL = `${context.locals.SITE_URL}rss.xml`
context.locals.RSS_PREFIX = ''
if (context.url.pathname.startsWith('/search') && context.params.q?.startsWith('#')) {
const tag = context.params.q.replace('#', '')
context.locals.RSS_URL = `${context.locals.SITE_URL}rss.xml?tag=${tag}`
const querySearch = context.url.searchParams.get('q') || ''
const legacyTagSearch = getEncodedTagSearchQuery(context.url.pathname)
const pathSearch = context.params.q || ''
const searchQuery = querySearch || legacyTagSearch || pathSearch
if (context.url.pathname.startsWith('/search') && searchQuery.startsWith('#')) {
const tag = searchQuery.replace('#', '')
context.locals.RSS_URL = `${context.locals.SITE_URL}rss.xml?tag=${encodeURIComponent(tag)}`
context.locals.RSS_PREFIX = `${tag} | `
}
const response = await next()
const response = legacyTagSearch
? await context.rewrite(`/search/result?q=${encodeURIComponent(legacyTagSearch)}`)
: await next()
if (!response.bodyUsed) {
if (response.headers.get('Content-type') === 'text/html') {

View File

@@ -11,10 +11,14 @@ channel.seo = {
title: 'Tags',
}
const items = (getEnv(import.meta.env, Astro, 'TAGS') || '').split(',').map((tag) => ({
href: `/search/%23${tag}`,
label: tag,
}))
const items = (getEnv(import.meta.env, Astro, 'TAGS') || '')
.split(',')
.map((tag) => tag.trim())
.filter(Boolean)
.map((tag) => ({
href: `/search/result?q=${encodeURIComponent(`#${tag}`)}`,
label: tag,
}))
---
<Layout channel={channel}>