Lunr integration for Astro; client-side search for statically hosted pages.
0 | import lunr from 'lunr'; | ||
1 | |||
2 | - | const LUNR_DIR = import.meta.env.PUBLIC_LUNR_DIR || ""; | |
3 | |||
4 | - | function join(...path){ | |
5 | + | function joinPath(...path){ | |
6 | return path.filter(Boolean).join("/").replace(/\/+/g, "/"); | ||
7 | } | ||
8 | |||
9 | - | let idxMap = new Map(); | |
10 | - | export async function getIndex(index = ""){ | |
11 | - | if(!idxMap.has(index)){ | |
12 | - | let response = await fetch(join(LUNR_DIR, index, "idx.json")); | |
13 | - | if(response.status !== 200){ | |
14 | - | throw new Error(`Astro-lunr: idx.json not found for index=${index}`); | |
15 | + | export default function initializeLunr({lunrDir}){ | |
16 | + | let idxMap = new Map(); | |
17 | + | async function getIndex(index = ""){ | |
18 | + | if(!idxMap.has(index)){ | |
19 | + | let response = await fetch(joinPath(lunrDir, index, "idx.json")); | |
20 | + | if(response.status !== 200){ | |
21 | + | throw new Error(`Astro-lunr: idx.json not found for index=${index}`); | |
22 | + | } | |
23 | + | let idxJson = await response.text(); | |
24 | + | idxMap.set(index, lunr.Index.load(JSON.parse(idxJson))); | |
25 | } | ||
26 | - | let idxJson = await response.text(); | |
27 | - | idxMap.set(index, lunr.Index.load(JSON.parse(idxJson))); | |
28 | + | return idxMap.get(index); | |
29 | } | ||
30 | - | return idxMap.get(index); | |
31 | - | } | |
32 | |||
33 | - | let docMap = new Map(); | |
34 | - | export async function getDocs(index = ""){ | |
35 | - | if(!docMap.has(index)){ | |
36 | - | let response = await fetch(join(LUNR_DIR, index, "docs.json")); | |
37 | - | if(response.status !== 200){ | |
38 | - | throw new Error(`Astro-lunr: docs.json not found for index=${index}`); | |
39 | + | let docMap = new Map(); | |
40 | + | async function getDocs(index = ""){ | |
41 | + | if(!docMap.has(index)){ | |
42 | + | let response = await fetch(joinPath(lunrDir, index, "docs.json")); | |
43 | + | if(response.status !== 200){ | |
44 | + | throw new Error(`Astro-lunr: docs.json not found for index=${index}`); | |
45 | + | } | |
46 | + | let docsJson = await response.text(); | |
47 | + | docMap.set(index, JSON.parse(docsJson).reduce((map, doc) => map.set(doc.id, doc), new Map())); | |
48 | } | ||
49 | - | let docsJson = await response.text(); | |
50 | - | docMap.set(index, JSON.parse(docsJson).reduce((map, doc) => map.set(doc.id, doc), new Map())); | |
51 | + | return docMap.get(index); | |
52 | } | ||
53 | - | return docMap.get(index); | |
54 | - | } | |
55 | |||
56 | - | export async function search(query, index) { | |
57 | - | if(!query) { | |
58 | - | return []; | |
59 | + | async function search(query, index) { | |
60 | + | if(!query) { | |
61 | + | return []; | |
62 | + | } | |
63 | + | let idx = await getIndex(index); | |
64 | + | return idx.search(query); | |
65 | } | ||
66 | - | let idx = await getIndex(index); | |
67 | - | return idx.search(query); | |
68 | - | } | |
69 | |||
70 | - | export async function enrich(hits, index){ | |
71 | - | const docs = await getDocs(index) | |
72 | - | return hits.map(hit => { | |
73 | - | return { | |
74 | - | doc: docs.get(hit.ref), | |
75 | - | hit | |
76 | - | } | |
77 | - | }) | |
78 | + | async function enrich(hits, index){ | |
79 | + | const docs = await getDocs(index) | |
80 | + | return hits.map(hit => { | |
81 | + | return { | |
82 | + | doc: docs.get(hit.ref), | |
83 | + | hit | |
84 | + | } | |
85 | + | }) | |
86 | + | } | |
87 | + | ||
88 | + | return { | |
89 | + | getIndex, | |
90 | + | getDocs, | |
91 | + | search, | |
92 | + | enrich, | |
93 | + | } |
0 | { | ||
1 | "name": "@siverv/astro-lunr", | ||
2 | - | "version": "0.0.1", | |
3 | + | "version": "0.0.2", | |
4 | "license" : "GPL-3.0", | ||
5 | "description": "Lunr integration for Astro", | ||
... | |||
26 | "rehype": "^12.0.1" | ||
27 | } |
0 | |||
1 | |||
... | |||
93 | |||
94 | ```js | ||
95 | - | import {search, enrich} from 'astro-lunr/client/lunr.js'; | |
96 | + | import initializeLunr from 'astro-lunr/client/lunr.js'; | |
97 | |||
98 | + | const {search, enrich} = initializeLunr({lunrDir: "./lunr"}) | |
99 | + | ||
100 | search("query", "index") | ||
101 | .then(enrich) | ||
... | |||
112 | To properly search files that are not usually genereated in the build-step, you would need to have a separate build-step that includes all pages that might be generated. | ||
113 |