blob: 369677190c34690dff790cd225873155c1afc33b [file] [log] [blame]
import lunr from 'lunr';
import { searchIndexUrl } from '../../utils/proxiedGenerated';
export async function fetchIndexes(baseUrl) {
if (process.env.NODE_ENV === 'production') {
// const json = await (await fetch(`${baseUrl}${searchIndexUrl}`)).json();
const json = await (await fetch(`https://cdn.selectdb.com${baseUrl}${searchIndexUrl}`)).json();
const wrappedIndexes = json.map(({ documents, index }, type) => ({
type: type,
documents,
index: lunr.Index.load(index),
}));
const zhDictionary = json.reduce((acc, item) => {
for (const tuple of item.index.invertedIndex) {
if (/\p{Unified_Ideograph}/u.test(tuple[0][0])) {
acc.add(tuple[0]);
}
}
return acc;
}, new Set());
return {
wrappedIndexes,
zhDictionary: Array.from(zhDictionary),
};
}
// The index does not exist in development, therefore load a dummy index here.
return {
wrappedIndexes: [],
zhDictionary: [],
};
}