All repositories

astro-lunr

License

Lunr integration for Astro; client-side search for statically hosted pages.

2022-05-14 17:06 #1: fixed base path replacement problem
Siver K. Volle 3f44311
2022-05-14 17:06 #1: fixed base path replacement problem master Siver K. Volle 3f44311
2022-05-01 13:57 Fixed problem with import.meta.env when proper npm-module Siver K. Volle 00a915f
2022-04-18 20:31 add demo-link to readme Siver K. Volle d930b81
2022-04-18 19:51 initial steps to turn astro-lunr into its own repo Siver K. Volle 9983ab3
2022-04-18 18:39 better lunr in dev-mode; better file diffs Siver K. Volle 182546c
2022-04-17 19:29 support for github pages; max-lines in file view Siver K. Volle 1612826
2022-04-16 00:25 Included readme and licenses; made the plugins into npm-modules; removed more dead-links and ui/ux-bugs Siver K. Volle 077a354
2022-04-15 17:21 Multi-repo support; refactored astro-git and astro-lunr into independent plugins Siver K. Volle adfedbe
2022-04-14 10:00 lunr-based indexing and search integration; assorted minor ux/ui improvements Siver K. Volle 146ebb5

Differences for commit 00a915fc838d1213904d4e69b02c025a7ceff64a

client/lunr.js | +44L -36L 84% changed
to file
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 + }
package.json | +1L -1L 7% changed
to file
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 }
README.md | +3L -1L 3% changed
to file
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