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){ | ||
| ... | |||
| 10 | export async function getIndex(index = ""){ | ||
| 11 | if(!idxMap.has(index)){ | ||
| 12 | - | let response = await fetch(join(BASE_URL, LUNR_DIR, index, "idx.json")); | |
| 13 | + | let response = await fetch(join(LUNR_DIR, index, "idx.json")); | |
| 14 | if(response.status !== 200){ | ||
| 15 | throw new Error(`Astro-lunr: idx.json not found for index=${index}`); | ||
| ... | |||
| 24 | export async function getDocs(index = ""){ | ||
| 25 | if(!docMap.has(index)){ | ||
| 26 | - | let response = await fetch(join(BASE_URL, LUNR_DIR, index, "docs.json")); | |
| 27 | + | let response = await fetch(join(LUNR_DIR, index, "docs.json")); | |
| 28 | if(response.status !== 200){ | ||
| 29 | throw new Error(`Astro-lunr: docs.json not found for index=${index}`); | ||
| ... | |||
| 51 | } | ||
| 52 | }) |
| 0 | { | ||
| 1 | - | "name": "astro-lunr", | |
| 2 | + | "name": "@siverv/astro-lunr", | |
| 3 | "version": "0.0.1", | ||
| 4 | "private": true, | ||
| 5 | "license" : "GPL-3.0", | ||
| 6 | "description": "Lunr integration for Astro", | ||
| 7 | "type": "module", | ||
| 8 | + | "homepage": "https://siverv.github.io/astro-git-view/astro-lunr/", | |
| 9 | + | "contributors": [ | |
| 10 | + | { | |
| 11 | + | "name": "Siver K. Volle", | |
| 12 | + | "email": "package-json.astro-lunr@siverv.no", | |
| 13 | + | "url": "https://www.siverv.no" | |
| 14 | + | } | |
| 15 | + | ], | |
| 16 | + | "repository": { | |
| 17 | + | "type": "git", | |
| 18 | + | "url": "https://github.com/siverv/astro-lunr.git" | |
| 19 | + | }, | |
| 20 | "exports": { | ||
| 21 | "./plugin.mjs": "./plugin.mjs", | ||
| ... | |||
| 27 | "rehype": "^12.0.1" | ||
| 28 | } |
| 0 | import fs from 'node:fs'; | ||
| 1 | import path from 'node:path'; | ||
| ... | |||
| 57 | export function createVitePlugin({ config }) { | ||
| 58 | return { | ||
| 59 | - | name: 'astro-lunr:dev-server', | |
| 60 | + | name: '@siverv/astro-lunr:dev-server', | |
| 61 | configureServer(viteServer) { | ||
| 62 | viteServer.middlewares.use((req, res, next) => { | ||
| ... | |||
| 97 | let pathsToIndex = [] | ||
| 98 | return { | ||
| 99 | - | name: 'lunr-filenames', | |
| 100 | + | name: '@siverv/astro-lunr:plugin', | |
| 101 | hooks: { | ||
| 102 | 'astro:config:setup': (options) => { | ||
| 103 | if(options.command === "dev"){ | ||
| 104 | options.addRenderer({ | ||
| 105 | - | name: 'lunr-renderer', | |
| 106 | - | serverEntrypoint: 'astro-lunr/server/renderer.js', | |
| 107 | + | name: '@siverv/astro-lunr:renderer', | |
| 108 | + | serverEntrypoint: '@siverv/astro-lunr/server/renderer.js', | |
| 109 | }); | ||
| 110 | options.updateConfig({ vite: getViteConfiguration(options) }); | ||
| ... | |||
| 160 | } | ||
| 161 | } |
| 0 | |||
| 1 | |||
| ... | |||
| 31 | | `verbose` | boolean | Debug log | | ||
| 32 | |||
| 33 | + | If using `subDir` or the astro-config `base`, set the env-variable `PUBLIC_LUNR_DIR` to be `/base/subDir/` | |
| 34 | |||
| 35 | ### Example from astro-git-view | ||
| ... | |||
| 107 | 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. | ||
| 108 |