提交正式官网
This commit is contained in:
@@ -0,0 +1,271 @@
|
||||
import { siteSearchIndex, type SiteSearchCategory, type SiteSearchEntry } from '~/data/site-search-index'
|
||||
|
||||
import { doctorZh } from '~/data/locale/zh/doctors.zh'
|
||||
|
||||
import { hospitalIntroZh } from '~/data/locale/zh/hospitals-intros.zh'
|
||||
|
||||
import { hospitalNameZh } from '~/data/locale/zh/hospitals-names.zh'
|
||||
|
||||
import { joinSearchText } from '~/utils/collect-text'
|
||||
|
||||
import type { SiteLocale } from '~/types/locale'
|
||||
|
||||
|
||||
|
||||
export type SiteSearchResult = SiteSearchEntry & {
|
||||
|
||||
score: number
|
||||
|
||||
excerpt: string
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
function localizeSearchEntry(entry: SiteSearchEntry, locale: SiteLocale): SiteSearchEntry {
|
||||
|
||||
if (locale !== 'zh') return entry
|
||||
|
||||
|
||||
|
||||
if (entry.id.startsWith('hospital:')) {
|
||||
|
||||
const hospitalId = entry.id.slice('hospital:'.length)
|
||||
|
||||
const name = hospitalNameZh[hospitalId] ?? entry.title
|
||||
|
||||
const intro = hospitalIntroZh[hospitalId] ?? ''
|
||||
|
||||
return {
|
||||
|
||||
...entry,
|
||||
|
||||
title: name,
|
||||
|
||||
body: joinSearchText(name, intro)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (entry.id.startsWith('doctor:')) {
|
||||
|
||||
const doctorId = entry.id.slice('doctor:'.length)
|
||||
|
||||
const zh = doctorZh[doctorId]
|
||||
|
||||
if (!zh) return entry
|
||||
|
||||
return {
|
||||
|
||||
...entry,
|
||||
|
||||
title: zh.name,
|
||||
|
||||
body: joinSearchText(zh.name, zh.department, zh.hospitalName, zh.expertLabel, zh.expertInfo)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
return entry
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
function getSearchIndex(locale: SiteLocale): SiteSearchEntry[] {
|
||||
|
||||
if (locale === 'en') return siteSearchIndex
|
||||
|
||||
return siteSearchIndex.map((entry) => localizeSearchEntry(entry, locale))
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
function normalizeQuery(query: string): string {
|
||||
|
||||
return query.trim().toLowerCase()
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
function tokenize(query: string): string[] {
|
||||
|
||||
return normalizeQuery(query)
|
||||
|
||||
.split(/\s+/)
|
||||
|
||||
.filter((token) => token.length > 0)
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
function buildExcerpt(body: string, tokens: string[], maxLen = 140): string {
|
||||
|
||||
const lower = body.toLowerCase()
|
||||
|
||||
let idx = -1
|
||||
|
||||
for (const token of tokens) {
|
||||
|
||||
const found = lower.indexOf(token)
|
||||
|
||||
if (found !== -1 && (idx === -1 || found < idx)) idx = found
|
||||
|
||||
}
|
||||
|
||||
if (idx === -1) {
|
||||
|
||||
return body.length <= maxLen ? body : `${body.slice(0, maxLen).trim()}…`
|
||||
|
||||
}
|
||||
|
||||
const start = Math.max(0, idx - 40)
|
||||
|
||||
const slice = body.slice(start, start + maxLen).trim()
|
||||
|
||||
const prefix = start > 0 ? '…' : ''
|
||||
|
||||
const suffix = start + maxLen < body.length ? '…' : ''
|
||||
|
||||
return `${prefix}${slice}${suffix}`
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
function scoreEntry(entry: SiteSearchEntry, tokens: string[], rawQuery: string): number {
|
||||
|
||||
if (!tokens.length) return 0
|
||||
|
||||
|
||||
|
||||
const title = entry.title.toLowerCase()
|
||||
|
||||
const body = entry.body.toLowerCase()
|
||||
|
||||
const q = normalizeQuery(rawQuery)
|
||||
|
||||
let score = 0
|
||||
|
||||
|
||||
|
||||
if (title === q) score += 120
|
||||
|
||||
else if (title.includes(q)) score += 70
|
||||
|
||||
|
||||
|
||||
if (body.includes(q)) score += 25
|
||||
|
||||
|
||||
|
||||
for (const token of tokens) {
|
||||
|
||||
if (title.includes(token)) score += 24
|
||||
|
||||
if (body.includes(token)) score += 8
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
return score
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
export function searchSite(query: string, limit = 24, locale: SiteLocale = 'en'): SiteSearchResult[] {
|
||||
|
||||
const tokens = tokenize(query)
|
||||
|
||||
if (!tokens.length) return []
|
||||
|
||||
|
||||
|
||||
const index = getSearchIndex(locale)
|
||||
|
||||
const scored = index
|
||||
|
||||
.map((entry) => {
|
||||
|
||||
const score = scoreEntry(entry, tokens, query)
|
||||
|
||||
if (score <= 0) return null
|
||||
|
||||
return {
|
||||
|
||||
...entry,
|
||||
|
||||
score,
|
||||
|
||||
excerpt: buildExcerpt(entry.body, tokens)
|
||||
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
.filter((item): item is SiteSearchResult => item !== null)
|
||||
|
||||
.sort((a, b) => b.score - a.score || a.title.localeCompare(b.title))
|
||||
|
||||
|
||||
|
||||
return scored.slice(0, limit)
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
export function useSiteSearch() {
|
||||
|
||||
const { locale } = useSiteLocale()
|
||||
|
||||
|
||||
|
||||
function search(query: string, limit = 24) {
|
||||
|
||||
return searchSite(query, limit, locale.value)
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
return {
|
||||
|
||||
searchSite: search,
|
||||
|
||||
categories: [
|
||||
|
||||
'Page',
|
||||
|
||||
'Patient Guide',
|
||||
|
||||
'Hospital',
|
||||
|
||||
'Doctor',
|
||||
|
||||
'FAQ',
|
||||
|
||||
'Success Story',
|
||||
|
||||
'Health Screening',
|
||||
|
||||
'Legal'
|
||||
|
||||
] as const satisfies readonly SiteSearchCategory[]
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user