diff options
| author | Nolawz <Nolawz333@protonmail.com> | 2026-03-03 11:52:14 +0530 |
|---|---|---|
| committer | Nolawz <Nolawz333@protonmail.com> | 2026-03-03 11:52:14 +0530 |
| commit | 13bca5d95de10c76e95b05beddea20de8cabda61 (patch) | |
| tree | 14cefd995ecd35dc258973ecc883ba72715067b7 /src/pages | |
| parent | 58b0433ceeab26ac6b29b85119671eb5fc9027aa (diff) | |
Added search functionality
Diffstat (limited to 'src/pages')
| -rw-r--r-- | src/pages/api/search-index.json.ts | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/src/pages/api/search-index.json.ts b/src/pages/api/search-index.json.ts new file mode 100644 index 0000000..340c26a --- /dev/null +++ b/src/pages/api/search-index.json.ts @@ -0,0 +1,74 @@ +import type { APIRoute } from 'astro' +import { getAllEvents, getAllPosts } from '@/lib/data-utils' + +export const prerender = true + +export const GET: APIRoute = async () => { + try { + const posts = await getAllPosts() + const events = await getAllEvents() + + const searchIndex = { + "posts": posts.map((post) => { + // Extract text content from HTML body (remove tags) + // The body property contains the raw HTML content + const htmlContent = (post as { body?: string }).body || '' + const textContent = htmlContent + .replace(/<[^>]+>/g, ' ') + .replace(/\s+/g, ' ') + .trim() + + return { + id: post.id || '', + title: post.data.title || '', + description: post.data.description || '', + date: post.data.date?.toISOString() || new Date().toISOString(), + tags: post.data.tags || [], + authors: post.data.authors || [], + url: `/blog/${post.id}`, + // Include full content for better search results + content: textContent, // Full content for indexing + } + }), + "events": events.map((event) => { + // Extract text content from HTML body (remove tags) + // The body property contains the raw HTML content + const htmlContent = (event as { body?: string }).body || '' + const textContent = htmlContent + .replace(/<[^>]+>/g, ' ') + .replace(/\s+/g, ' ') + .trim() + + return { + id: event.id || '', + title: event.data.title || '', + description: event.data.description || '', + date: event.data.date?.toISOString() || new Date().toISOString(), + type: event.data.type, + url: `/events/${event.id}`, + // Include full content for better search results + content: textContent, // Full content for indexing + } + }), + } + + + return new Response(JSON.stringify(searchIndex), { + status: 200, + headers: { + 'Content-Type': 'application/json', + 'Cache-Control': 'public, max-age=3600', + }, + }) + } catch (error) { + console.error('Error generating search index:', error) + // Return empty array on error instead of failing + return new Response(JSON.stringify([]), { + status: 200, + headers: { + 'Content-Type': 'application/json', + 'Cache-Control': 'public, max-age=3600', + }, + }) + } +} |
