-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
164 lines (158 loc) · 8.63 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import fs from "fs";
import * as cheerio from "cheerio";
import { fileURLToPath } from "node:url";
import type { AstroIntegration, RouteData } from "astro";
// for a simple integration you could use as reference, check out ref: https://github.com/Kendy205/astro-og-image/blob/main/src/index.ts
interface LunrIndexItem {
title: string;
loc: string;
content?: string;
adf_section?: string | null;
}
interface AstroLunrConfig {
routeFilter?(value: RouteData, index?: number, array?: RouteData[]): RouteData[];
headingFilter?(idx: number, el: cheerio.Element): boolean;
contentFilter?(el: cheerio.Element): boolean;
resultFilter?(obj: LunrIndexItem, idx?: number, arr?: LunrIndexItem[]): boolean;
}
export default function AstroLunr(config: AstroLunrConfig): AstroIntegration {
const isHeading = (chr: cheerio.Element): boolean => {
return ["h1", "h2", "h3", "h4", "h5", "h6"].indexOf(chr.name) >= 0;
};
return {
name: "@jackcarey/astro-lunr",
hooks: {
"astro:build:done": async ({ dir, routes }) => {
if (typeof config?.routeFilter === "function") {
console.log("Building search index with custom route filter");
}
if (typeof config?.headingFilter === "function") {
console.log("Building search index with custom heading filter");
}
if (typeof config?.resultFilter === "function") {
console.log("Building search index with custom result filter");
}
let index_path = fileURLToPath(`${dir}search_index.json`);
let index_json: LunrIndexItem[] = [];
if (typeof config?.routeFilter === "function") {
routes = routes.filter(config.routeFilter);
}
for (let route of routes) {
if (route.type == "page") {
//ignore endpoints
let path = route?.distURL?.pathname;
if (path && path[0] == "/") {
path = path.substring(1); //remove preceding slash / if it exists
}
if (path) {
const content = fs.readFileSync(path, "utf8");
const $ = cheerio.load(content);
let headings = $("h1,h2,h3,h4,h5,h6");
if (typeof config?.headingFilter === "function") {
headings = headings.filter(config.headingFilter);
} else {
// a sensible default filter that ignores headings inside the header and nav elements
headings = headings.filter(
(idx, hd) =>
$(hd).parents("header").length +
$(hd).parents("nav").length ==
0
);
}
headings.each((idx: number, el: cheerio.Element) => {
let cheery = $(el);
let headingID: string = el.attribs?.id || "";
let loc: string = `${route.route}${
headingID ? "#" : ""
}${headingID}`;
let title: string = cheery.text() || loc;
let result: LunrIndexItem = { loc, title };
let indexExists = index_json.some(
(x) => x.loc == result.loc && x.title == result.title
);
if (!indexExists) {
// To enable advanced filtering with @jackcarey/astro-filter-content,
// the astro docs filter (adf) filter is added, if it exists
let adf_section = null;
for (let parent of cheery.parents()) {
let el = $(parent);
let sec = el.data("adf-section");
if (sec) {
adf_section = sec;
break;
}
}
if (adf_section) {
result.adf_section = "" + adf_section;
}
// add text content following each heading to the result
// by looping through every 'nextSibling' element and children from
// cheerio until another heading is found
let content = "";
for (let sib of cheery.nextAll()) {
if (isHeading(sib)) {
break;
}
// this function is called recursively on tags
const nestedText = (cheer) => {
const ignore = [
"script",
"style",
"astro-island",
"nav",
"video",
"svg>*:not(title,text)",
];
// only ignore elements if the user hasn't specified their own filter
let ignored =
!config?.contentFilter &&
ignore.indexOf(el.name) != -1;
//otherwise, don't ignore any elements and only use the provided filter
let excluded =
config?.contentFilter &&
!config.contentFilter(el);
if (ignored || excluded) {
return "";
}
let res = "";
if (cheer.children.length) {
for (let idx in cheer.children) {
let child = cheer.children[idx];
if (child.type == "text") {
res += child.data.trim();
} else {
res += nestedText($(child));
}
}
}
return res.trim();
};
content += nestedText(sib).trim();
}
if (content) {
result.content = content;
}
index_json.push(result);
}
});
}
}
}
if (typeof config?.resultFilter === "function") {
index_json = index_json.filter(config.resultFilter);
}
fs.writeFileSync(index_path, JSON.stringify(index_json), {
encoding: "utf8",
flag: "w",
});
console.log(
"Finished building search_index.json for Lunr. It has",
index_json.length,
"entries,",
index_json.filter((x) => Object.keys(x).indexOf("content") != -1).length,
"with 'content'."
);
},
},
};
}