From 13bca5d95de10c76e95b05beddea20de8cabda61 Mon Sep 17 00:00:00 2001 From: Nolawz Date: Tue, 3 Mar 2026 11:52:14 +0530 Subject: Added search functionality --- src/pages/api/search-index.json.ts | 74 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/pages/api/search-index.json.ts (limited to 'src/pages/api') 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', + }, + }) + } +} -- cgit v1.2.3