| @@ -1,8 +1,10 @@ |
| 1 | import type { Category } from '../types/Service'; |
1 | import type { Category } from '../types/Service'; |
| |
2 | import { slugify } from './fetch-data'; |
| 2 | |
3 | |
| 3 | export interface SearchItem { |
4 | export interface SearchItem { |
| 4 | type: 'Category' | 'Section' | 'Service'; |
5 | type: 'Category' | 'Section' | 'Service'; |
| 5 | category: string; |
6 | category: string; |
| |
7 | path: string; |
| 6 | itemCount?: number; |
8 | itemCount?: number; |
| 7 | sectionName?: string; |
9 | sectionName?: string; |
| 8 | description?: string; |
10 | description?: string; |
| @@ -12,15 +14,20 @@ export interface SearchItem { |
| 12 | codeberg?: string; |
14 | codeberg?: string; |
| 13 | git?: string; |
15 | git?: string; |
| 14 | logo?: string; |
16 | logo?: string; |
| |
17 | platforms?: string[]; |
| |
18 | traits?: string[]; |
| |
19 | alternativeTo?: string[]; |
| 15 | } |
20 | } |
| 16 | |
21 | |
| 17 | export const prepareSearchItems = (categories: Category[]): SearchItem[] => { |
22 | export const prepareSearchItems = (categories: Category[]): SearchItem[] => { |
| 18 | const items: SearchItem[] = []; |
23 | const items: SearchItem[] = []; |
| 19 | // Add each category |
24 | // Add each category |
| 20 | categories.forEach((category) => { |
25 | categories.forEach((category) => { |
| |
26 | const categoryPath = `/${slugify(category.name)}/`; |
| 21 | items.push({ |
27 | items.push({ |
| 22 | type: 'Category', |
28 | type: 'Category', |
| 23 | category: category.name, |
29 | category: category.name, |
| |
30 | path: categoryPath, |
| 24 | itemCount: (category.sections || []).reduce((acc, section) => { |
31 | itemCount: (category.sections || []).reduce((acc, section) => { |
| 25 | return acc + (section.services || []).length; |
32 | return acc + (section.services || []).length; |
| 26 | }, 0), |
33 | }, 0), |
| @@ -28,11 +35,13 @@ export const prepareSearchItems = (categories: Category[]): SearchItem[] => { |
| 28 | |
35 | |
| 29 | // Add section with category context |
36 | // Add section with category context |
| 30 | category.sections.forEach((section) => { |
37 | category.sections.forEach((section) => { |
| |
38 | const sectionPath = `${categoryPath}${slugify(section.name)}/`; |
| 31 | items.push({ |
39 | items.push({ |
| 32 | type: 'Section', |
40 | type: 'Section', |
| 33 | sectionName: section.name, |
41 | sectionName: section.name, |
| 34 | description: section.intro || '', |
42 | description: section.intro || '', |
| 35 | category: category.name, |
43 | category: category.name, |
| |
44 | path: sectionPath, |
| 36 | itemCount: (section.services || []).length, |
45 | itemCount: (section.services || []).length, |
| 37 | }); |
46 | }); |
| 38 | |
47 | |
| @@ -48,7 +57,21 @@ export const prepareSearchItems = (categories: Category[]): SearchItem[] => { |
| 48 | git: service.git || '', |
57 | git: service.git || '', |
| 49 | category: category.name, |
58 | category: category.name, |
| 50 | sectionName: section.name, |
59 | sectionName: section.name, |
| |
60 | path: `${sectionPath}${slugify(service.name)}/`, |
| 51 | logo: service.icon || '', |
61 | logo: service.icon || '', |
| |
62 | // Get the plafrorm from followWith field or android/ios app |
| |
63 | platforms: Object.keys(service) |
| |
64 | .filter((key) => key.endsWith('App')) |
| |
65 | .map((key) => key.replace(/App$/, '')) |
| |
66 | .concat(service.followWith || []), |
| |
67 | // Boolean fields, as words: `openSource` -> "open source" |
| |
68 | traits: Object.entries(service) |
| |
69 | .filter(([, value]) => value === true) |
| |
70 | .map(([key]) => |
| |
71 | key.replace(/[A-Z]/g, (c) => ` ${c.toLowerCase()}`), |
| |
72 | ), |
| |
73 | // The proprietary tools this section replaces, eg "whatsapp" |
| |
74 | alternativeTo: section.alternativeTo || [], |
| 52 | }); |
75 | }); |
| 53 | }); |
76 | }); |
| 54 | }); |
77 | }); |
| @@ -56,21 +79,27 @@ export const prepareSearchItems = (categories: Category[]): SearchItem[] => { |
| 56 | return items; |
79 | return items; |
| 57 | }; |
80 | }; |
| 58 | |
81 | |
| |
82 | // Set the fuzziness power. 0 = perfect match, and 1 = barley matched |
| |
83 | export const SEARCH_SCORE_CUTOFF = 0.85; |
| |
84 | |
| |
85 | export const isRelevant = (result: { score?: number }): boolean => |
| |
86 | (result.score ?? 0) <= SEARCH_SCORE_CUTOFF; |
| |
87 | |
| 59 | export const searchOptions = { |
88 | export const searchOptions = { |
| 60 | includeScore: true, |
89 | includeScore: true, |
| |
90 | ignoreLocation: true, |
| |
91 | threshold: 0.3, |
| 61 | keys: [ |
92 | keys: [ |
| 62 | { name: 'name', weight: 0.9 }, |
93 | { name: 'name', weight: 0.9 }, |
| 63 | { name: 'sectionName', weight: 0.8 }, |
94 | { name: 'sectionName', weight: 0.8 }, |
| |
95 | { name: 'alternativeTo', weight: 0.8 }, |
| 64 | { name: 'category', weight: 0.7 }, |
96 | { name: 'category', weight: 0.7 }, |
| 65 | { name: 'notableMentions', weight: 0.5 }, |
97 | { name: 'platforms', weight: 0.6 }, |
| 66 | { name: 'alternativeTo', weight: 0.5 }, |
98 | { name: 'traits', weight: 0.6 }, |
| 67 | { name: 'github', weight: 0.4 }, |
99 | { name: 'github', weight: 0.4 }, |
| 68 | { name: 'codeberg', weight: 0.4 }, |
100 | { name: 'codeberg', weight: 0.4 }, |
| 69 | { name: 'git', weight: 0.3 }, |
101 | { name: 'git', weight: 0.3 }, |
| 70 | { name: 'url', weight: 0.2 }, |
102 | { name: 'url', weight: 0.2 }, |
| 71 | { name: 'description', weight: 0.1 }, |
103 | { name: 'description', weight: 0.1 }, |
| 72 | { name: 'intro', weight: 0.1 }, |
| |
| 73 | { name: 'furtherInfo', weight: 0.1 }, |
| |
| 74 | { name: 'wordOfWarning', weight: 0.1 }, |
| |
| 75 | ], |
104 | ], |
| 76 | }; |
105 | }; |