Integrated Pagefind for static site search with built-in Chinese word segmentation support. Changes: 1. **Installed Pagefind** (v1.4.0) as dev dependency 2. **Updated build script** to run Pagefind indexing after Next.js build - Indexes all 69 pages with 5,711 words - Automatic Chinese (zh-tw) language detection 3. **Created search modal component** (components/search-modal.tsx) - Dynamic Pagefind UI loading (lazy-loaded on demand) - Keyboard shortcuts (Cmd+K / Ctrl+K) - Chinese translations for UI elements - Dark mode compatible styling 4. **Added search button to header** (components/site-header.tsx) - Integrated SearchButton with keyboard shortcut display - Modal state management 5. **Custom Pagefind styles** (styles/globals.css) - Tailwind-based styling to match site design - Dark mode support - Highlight styling for search results Features: - ✅ Full-text search across all blog posts and pages - ✅ Built-in Chinese word segmentation (Unicode-based) - ✅ Mixed Chinese/English query support - ✅ Zero bundle impact (20KB lazy-loaded on search activation) - ✅ Keyboard shortcuts (⌘K / Ctrl+K) - ✅ Search result highlighting with excerpts - ✅ Dark mode compatible Technical Details: - Pagefind runs post-build to index .next directory - Search index stored in .next/pagefind/ - Chinese segmentation works automatically via Unicode boundaries - No third-party services or API keys required 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
92 lines
3.4 KiB
TypeScript
92 lines
3.4 KiB
TypeScript
'use client';
|
|
|
|
import Link from 'next/link';
|
|
import { useState } from 'react';
|
|
import { ThemeToggle } from './theme-toggle';
|
|
import { NavMenu, NavLinkItem, IconKey } from './nav-menu';
|
|
import { SearchButton, SearchModal } from './search-modal';
|
|
import { siteConfig } from '@/lib/config';
|
|
import { allPages } from 'contentlayer2/generated';
|
|
|
|
export function SiteHeader() {
|
|
const [isSearchOpen, setIsSearchOpen] = useState(false);
|
|
const pages = allPages
|
|
.slice()
|
|
.sort((a, b) => (a.title || '').localeCompare(b.title || ''));
|
|
|
|
const navItems: NavLinkItem[] = [
|
|
{ key: 'home', href: '/', label: '首頁', iconKey: 'home' },
|
|
{ key: 'blog', href: '/blog', label: 'Blog', iconKey: 'blog' },
|
|
...pages.map((page) => ({
|
|
key: page._id,
|
|
href: page.url,
|
|
label: page.title,
|
|
iconKey: getIconForPage(page.title, page.slug)
|
|
}))
|
|
];
|
|
|
|
return (
|
|
<header className="bg-white/80 backdrop-blur transition-colors duration-200 ease-snappy dark:bg-gray-950/80">
|
|
<div className="container mx-auto flex items-center justify-between px-4 py-3 text-slate-900 dark:text-slate-100">
|
|
<Link
|
|
href="/"
|
|
className="motion-link group relative type-title text-slate-900 hover:text-accent focus-visible:outline-none focus-visible:text-accent dark:text-slate-100"
|
|
>
|
|
<span className="absolute -bottom-0.5 left-0 h-[2px] w-0 bg-accent transition-all duration-180 ease-snappy group-hover:w-full" aria-hidden="true" />
|
|
{siteConfig.title}
|
|
</Link>
|
|
<div className="flex items-center gap-3">
|
|
<NavMenu items={navItems} />
|
|
<SearchButton onClick={() => setIsSearchOpen(true)} />
|
|
<ThemeToggle />
|
|
</div>
|
|
<SearchModal
|
|
isOpen={isSearchOpen}
|
|
onClose={() => setIsSearchOpen(false)}
|
|
/>
|
|
</div>
|
|
</header>
|
|
);
|
|
}
|
|
|
|
|
|
const titleOverrides = Object.fromEntries(
|
|
Object.entries(siteConfig.navIconOverrides?.titles ?? {}).map(([key, value]) => [
|
|
key.trim().toLowerCase(),
|
|
value as IconKey
|
|
])
|
|
);
|
|
|
|
const slugOverrides = Object.fromEntries(
|
|
Object.entries(siteConfig.navIconOverrides?.slugs ?? {}).map(([key, value]) => [
|
|
key.trim().toLowerCase(),
|
|
value as IconKey
|
|
])
|
|
);
|
|
|
|
function getIconForPage(title?: string, slug?: string): IconKey {
|
|
const normalizedTitle = title?.trim().toLowerCase();
|
|
if (normalizedTitle && titleOverrides[normalizedTitle]) {
|
|
return titleOverrides[normalizedTitle];
|
|
}
|
|
|
|
const normalizedSlug = slug?.trim().toLowerCase();
|
|
if (normalizedSlug && slugOverrides[normalizedSlug]) {
|
|
return slugOverrides[normalizedSlug];
|
|
}
|
|
|
|
if (!title) return 'file';
|
|
const lower = title.toLowerCase();
|
|
if (lower.includes('關於本站')) return 'menu';
|
|
if (lower.includes('關於') || lower.includes('about')) return 'user';
|
|
if (lower.includes('聯絡') || lower.includes('contact')) return 'contact';
|
|
if (lower.includes('位置') || lower.includes('map')) return 'location';
|
|
if (lower.includes('作品') || lower.includes('portfolio')) return 'pen';
|
|
if (lower.includes('標籤') || lower.includes('tags')) return 'tags';
|
|
if (lower.includes('homelab')) return 'server';
|
|
if (lower.includes('server') || lower.includes('伺服') || lower.includes('infrastructure')) return 'server';
|
|
if (lower.includes('開發工作環境')) return 'device';
|
|
if (lower.includes('device') || lower.includes('設備') || lower.includes('硬體') || lower.includes('hardware')) return 'device';
|
|
return 'file';
|
|
}
|