/[sudobot]/trunk/docs/app/search/route.ts
ViewVC logotype

Contents of /trunk/docs/app/search/route.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 626 - (show annotations)
Sat Sep 7 09:38:45 2024 UTC (6 months, 3 weeks ago) by rakinar2
File MIME type: application/typescript
File size: 1869 byte(s)
chore: sync with git

1 import { getIndex } from "@/utils/pages";
2 import { NextRequest, NextResponse } from "next/server";
3
4 export const dynamic = "force-dynamic";
5
6 export async function GET(request: NextRequest) {
7 const query = new URL(request.url).searchParams.get("q")?.toLowerCase();
8
9 if (!query) {
10 return NextResponse.json(
11 {
12 error: "Invalid Request Payload",
13 },
14 {
15 status: 400,
16 },
17 );
18 }
19
20 const index = getIndex();
21 const lowercasedIndex = getIndex(true);
22
23 const results = [];
24
25 if (lowercasedIndex) {
26 for (const i in lowercasedIndex) {
27 if (results.length >= 15) {
28 break;
29 }
30
31 const titleIncludes = lowercasedIndex[i].title?.includes(query);
32 const descriptionIncludes =
33 lowercasedIndex[i].description?.includes(query);
34 const dataIncludes = lowercasedIndex[i].data.includes(query);
35
36 if (titleIncludes || descriptionIncludes || dataIncludes) {
37 results.push({
38 ...index?.[i],
39 match: titleIncludes
40 ? ("title" as const)
41 : descriptionIncludes
42 ? ("description" as const)
43 : ("data" as const),
44 });
45 }
46 }
47 }
48
49 results.sort((a, b) => {
50 if (a.match === "title" && b.match === "title") {
51 return a.title!.localeCompare(b.title!);
52 }
53
54 if (a.match === "title") {
55 return -1;
56 }
57
58 if (b.match === "title") {
59 return 1;
60 }
61
62 return a[a.match]!.substring(0, 10).localeCompare(
63 b[b.match]!.substring(0, 10),
64 );
65 });
66
67 return NextResponse.json({
68 results,
69 });
70 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26