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 | + | const BASE_URL = import.meta.env.PUBLIC_BASE_URL || "/"; | |
| 4 | |||
| 5 | function join(...path){ | ||
| 6 | - | return path.filter(Boolean).join("/"); | |
| 7 | + | return path.filter(Boolean).join("/").replace(/\/+/g, "/"); | |
| 8 | } | ||
| 9 | |||
| 10 | let idxMap = new Map(); | ||
| 11 | export async function getIndex(index = ""){ | ||
| 12 | if(!idxMap.has(index)){ | ||
| 13 | - | let response = await fetch(join(LUNR_DIR, index, "idx.json")); | |
| 14 | + | let response = await fetch(join(BASE_URL, LUNR_DIR, index, "idx.json")); | |
| 15 | if(response.status !== 200){ | ||
| 16 | throw new Error(`Astro-lunr: idx.json not found for index=${index}`); | ||
| ... | |||
| 25 | export async function getDocs(index = ""){ | ||
| 26 | if(!docMap.has(index)){ | ||
| 27 | - | let response = await fetch(join(LUNR_DIR, index, "docs.json")); | |
| 28 | + | let response = await fetch(join(BASE_URL, LUNR_DIR, index, "docs.json")); | |
| 29 | if(response.status !== 200){ | ||
| 30 | throw new Error(`Astro-lunr: docs.json not found for index=${index}`); | ||
| ... | |||
| 52 | } | ||
| 53 | }) |