| @@ -1,8 +1,10 @@ |
| 1 | 1 | import type { Category } from '../types/Service'; |
| 2 | import { slugify } from './fetch-data'; |
| 2 | 3 | |
| 3 | 4 | export interface SearchItem { |
| 4 | 5 | type: 'Category' | 'Section' | 'Service'; |
| 5 | 6 | category: string; |
| 7 | path: string; |
| 6 | 8 | itemCount?: number; |
| 7 | 9 | sectionName?: string; |
| 8 | 10 | description?: string; |
| @@ -12,15 +14,20 @@ export interface SearchItem { |
| 12 | 14 | codeberg?: string; |
| 13 | 15 | git?: string; |
| 14 | 16 | logo?: string; |
| 17 | platforms?: string[]; |
| 18 | traits?: string[]; |
| 19 | alternativeTo?: string[]; |
| 15 | 20 | } |
| 16 | 21 | |
| 17 | 22 | export const prepareSearchItems = (categories: Category[]): SearchItem[] => { |
| 18 | 23 | const items: SearchItem[] = []; |
| 19 | 24 | // Add each category |
| 20 | 25 | categories.forEach((category) => { |
| 26 | const categoryPath = `/${slugify(category.name)}/`; |
| 21 | 27 | items.push({ |
| 22 | 28 | type: 'Category', |
| 23 | 29 | category: category.name, |
| 30 | path: categoryPath, |
| 24 | 31 | itemCount: (category.sections || []).reduce((acc, section) => { |
| 25 | 32 | return acc + (section.services || []).length; |
| 26 | 33 | }, 0), |
| @@ -28,11 +35,13 @@ export const prepareSearchItems = (categories: Category[]): SearchItem[] => { |
| 28 | 35 | |
| 29 | 36 | // Add section with category context |
| 30 | 37 | category.sections.forEach((section) => { |
| 38 | const sectionPath = `${categoryPath}${slugify(section.name)}/`; |
| 31 | 39 | items.push({ |
| 32 | 40 | type: 'Section', |
| 33 | 41 | sectionName: section.name, |
| 34 | 42 | description: section.intro || '', |
| 35 | 43 | category: category.name, |
| 44 | path: sectionPath, |
| 36 | 45 | itemCount: (section.services || []).length, |
| 37 | 46 | }); |
| 38 | 47 | |
| @@ -48,7 +57,21 @@ export const prepareSearchItems = (categories: Category[]): SearchItem[] => { |
| 48 | 57 | git: service.git || '', |
| 49 | 58 | category: category.name, |
| 50 | 59 | sectionName: section.name, |
| 60 | path: `${sectionPath}${slugify(service.name)}/`, |
| 51 | 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 | 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 | 88 | export const searchOptions = { |
| 60 | 89 | includeScore: true, |
| 90 | ignoreLocation: true, |
| 91 | threshold: 0.3, |
| 61 | 92 | keys: [ |
| 62 | 93 | { name: 'name', weight: 0.9 }, |
| 63 | 94 | { name: 'sectionName', weight: 0.8 }, |
| 95 | { name: 'alternativeTo', weight: 0.8 }, |
| 64 | 96 | { name: 'category', weight: 0.7 }, |
| 65 | | { name: 'notableMentions', weight: 0.5 }, |
| 66 | | { name: 'alternativeTo', weight: 0.5 }, |
| 97 | { name: 'platforms', weight: 0.6 }, |
| 98 | { name: 'traits', weight: 0.6 }, |
| 67 | 99 | { name: 'github', weight: 0.4 }, |
| 68 | 100 | { name: 'codeberg', weight: 0.4 }, |
| 69 | 101 | { name: 'git', weight: 0.3 }, |
| 70 | 102 | { name: 'url', weight: 0.2 }, |
| 71 | 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 | }; |