aboutsummaryrefslogtreecommitdiffstats
path: root/src/pages/api/search-index.json.ts
blob: 340c26ab5f40b8ee6a8582c123fa2cd737cd3d5f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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',
      },
    })
  }
}