-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Build local search index DB from chrome service.
- Loading branch information
1 parent
e01656f
commit a87aee9
Showing
4 changed files
with
104 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { atom } from 'jotai'; | ||
import { Orama, create, insert } from '@orama/orama'; | ||
|
||
import { getChromeStaticPathname } from '../../utils/common'; | ||
import axios from 'axios'; | ||
|
||
type IndexEntry = { | ||
icon?: string; | ||
title: string[]; | ||
bundle: string[]; | ||
bundleTitle: string[]; | ||
id: string; | ||
uri: string; | ||
relative_uri: string; | ||
poc_description_t: string; | ||
alt_title: string[]; | ||
}; | ||
|
||
type SearchEntry = { | ||
title: string; | ||
uri: string; | ||
pathname: string; | ||
description: string; | ||
icon?: string; | ||
id: string; | ||
bundleTitle: string; | ||
altTitle?: string[]; | ||
}; | ||
|
||
const asyncSearchIndexAtom = atom(async () => { | ||
const staticPath = getChromeStaticPathname('search'); | ||
const { data: rawIndex } = await axios.get<IndexEntry[]>(`${staticPath}/search-index.json`); | ||
const searchIndex: SearchEntry[] = []; | ||
const idSet = new Set<string>(); | ||
rawIndex.forEach((entry) => { | ||
if (idSet.has(entry.id)) { | ||
console.warn('Duplicate id found in index', entry.id); | ||
return; | ||
} | ||
|
||
if (!entry.relative_uri.startsWith('/')) { | ||
console.warn('External ink found in the index. Ignoring: ', entry.relative_uri); | ||
return; | ||
} | ||
idSet.add(entry.id); | ||
searchIndex.push({ | ||
title: entry.title[0], | ||
uri: entry.uri, | ||
pathname: entry.relative_uri, | ||
description: entry.poc_description_t || entry.relative_uri, | ||
icon: entry.icon, | ||
id: entry.id, | ||
bundleTitle: entry.bundleTitle[0], | ||
altTitle: entry.alt_title, | ||
}); | ||
}); | ||
|
||
return searchIndex; | ||
}); | ||
|
||
const entrySchema = { | ||
title: 'string', | ||
description: 'string', | ||
altTitle: 'string[]', | ||
descriptionMatch: 'string', | ||
bundleTitle: 'string', | ||
pathname: 'string', | ||
} as const; | ||
|
||
async function insertEntry(db: Orama<typeof entrySchema>, entry: SearchEntry) { | ||
return insert(db, { | ||
id: entry.id, | ||
title: entry.title, | ||
description: entry.description, | ||
descriptionMatch: entry.description, | ||
altTitle: entry.altTitle ?? [], | ||
bundleTitle: entry.bundleTitle, | ||
pathname: entry.pathname, | ||
}); | ||
} | ||
|
||
export const asyncLocalOrama = atom(async (get) => { | ||
const db: Orama<typeof entrySchema> = await create({ | ||
schema: entrySchema, | ||
}); | ||
|
||
const insertCommands = (await get(asyncSearchIndexAtom)).map(async (entry) => { | ||
return insertEntry(db, entry); | ||
}); | ||
|
||
await Promise.all(insertCommands); | ||
return db; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters