deno.land / x / lume@v2.1.4 / plugins / pagefind.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import { merge } from "../core/utils/object.ts";import { posix } from "../deps/path.ts";import { Page } from "../core/file.ts";import { pagefind } from "../deps/pagefind.ts";
import type { CustomRecord, TranslationsOptions } from "../deps/pagefind.ts";import type Site from "../core/site.ts";
export interface UIOptions { /** * The container id to insert the search * @default "search" */ containerId?: string;
/** * The number of search results to load at once, before a “Load more” button is shown. * @default 5 */ pageSize?: number;
/** * Include results from page subsections (based on headings with IDs). */ showSubResults?: boolean;
/** Whether to show an image alongside each search result. */ showImages?: boolean;
/** * The maximum number of characters to show in the excerpt. * `0` means no limit */ excerptLength?: number;
/** * A function that Pagefind UI calls before performing a search. * This can be used to normalize search terms to match your content. */ processTerm?: (term: string) => string;
/** * A function that Pagefind UI calls before displaying each result. * This can be used to fix relative URLs, rewrite titles, * or any other modifications you might like to make to the raw result object * returned by Pagefind */ processResult?: (result: unknown) => unknown;
/** * By default, Pagefind UI shows filters with no results alongside the count (0). * Pass false to hide filters that have no remaining results. */ showEmptyFilters?: boolean;
/** * The default behavior of the filter display is to show values only when there is one filter with six or fewer values. When you include a filter name in openFilters it will open by default, regardless of the number of filters or values present. */ openFilters?: string[];
/** * By default, Pagefind UI applies a CSS reset to itself. * Pass false to omit this and inherit from your site styles. */ resetStyles?: boolean;
/** * The number of milliseconds to wait after a user stops typing before performing a search. * If you wish to disable this, set to 0. * @default 300 */ debounceTimeoutMs?: number;
/** * A set of custom ui strings to use instead of the automatically detected language strings. * See https://github.com/CloudCannon/pagefind/blob/main/pagefind_ui/translations/en.json for all available keys and initial values. * The items in square brackets such as SEARCH_TERM will be substituted dynamically when the text is used. */ translations?: TranslationsOptions;
/** * Enabling autofocus automatically directs attention to the search input field for * enhanced user convenience, particularly beneficial when the UI is loaded within a modal dialog. * However, exercise caution, as using autofocus indiscriminately may pose potential * accessibility challenges. */ autofocus?: boolean;
/** * Passes sort options to Pagefind for ranking. * Note that using a sort will override all ranking by relevance. */ sort?: Record<string, "asc" | "desc">;
/** * Enable the ability to highlight search terms on the result pages. * Configure the query parameter name. */ highlightParam?: string;}
export interface Options { /** The path to the pagefind bundle directory */ outputPath?: string;
/** Options for the UI interface or false to disable it */ ui?: UIOptions | false;
/** Options for the indexing process */ indexing?: pagefind.PagefindServiceConfig;
/** Other custom records */ customRecords?: CustomRecord[];}
export const defaults: Options = { outputPath: "/pagefind", ui: { containerId: "search", showImages: false, excerptLength: 0, showEmptyFilters: true, showSubResults: false, resetStyles: true, }, indexing: { rootSelector: "html", verbose: false, excludeSelectors: [], },};
/** A plugin to generate a static full text search engine */export default function (userOptions?: Options) { const options = merge(defaults, userOptions);
return (site: Site) => { site.process([".html"], async (pages, allPages) => { const { index } = await pagefind.createIndex(options.indexing);
if (!index) { throw new Error("Pagefind index not created"); }
// Page indexing for (const page of pages) { const { errors } = await index.addHTMLFile({ url: page.data.url, content: page.content as string, });
if (errors.length > 0) { throw new Error( `Pagefind index errors for ${page.src.path}:\n${errors.join("\n")}`, ); } }
if (options.customRecords) { for (const record of options.customRecords) { const { errors } = await index.addCustomRecord(record);
if (errors.length > 0) { throw new Error( `Pagefind index errors for custom record:\n${errors.join("\n")}`, ); } } }
// Output indexing const { files } = await index.getFiles(); const textDecoder = new TextDecoder(); const textExtensions = [".js", ".css", ".json"];
for (const file of files) { const { path } = file; const content = textExtensions.includes(posix.extname(path)) ? textDecoder.decode(file.content) : file.content;
allPages.push( Page.create({ url: posix.join("/", options.outputPath, path), content, }), ); }
// Cleanup await index.deleteIndex(); await pagefind.close(); });
if (options.ui) { const { containerId, ...ui } = options.ui;
site.process([".html"], (pages) => { for (const page of pages) { const { document } = page; if (!document) { continue; } const container = document.getElementById(containerId!);
// Insert UI styles and scripts if (container) { const styles = document.createElement("link"); styles.setAttribute("rel", "stylesheet"); styles.setAttribute( "href", site.url( `${posix.join(options.outputPath, "pagefind-ui.css")}`, ), );
// Insert before other styles to allow overriding const first = document.head.querySelector( "link[rel=stylesheet],style", ); if (first) { document.head.insertBefore(styles, first); } else { document.head.append(styles); }
const script = document.createElement("script"); script.setAttribute("type", "text/javascript"); script.setAttribute( "src", site.url( `${posix.join(options.outputPath, "pagefind-ui.js")}`, ), ); document.head.append(script);
const uiSettings = { element: `#${containerId}`, ...ui, bundlePath: site.url(posix.join(options.outputPath, "/")), baseUrl: site.url("/"), processTerm: ui.processTerm ? ui.processTerm.toString() : undefined, processResult: ui.processResult ? ui.processResult.toString() : undefined, }; const init = document.createElement("script"); init.setAttribute("type", "text/javascript"); init.innerHTML = `window.addEventListener('DOMContentLoaded',()=>{new PagefindUI(${ JSON.stringify(uiSettings) });});`; document.head.append(init);
if (ui.highlightParam) { const highlightScript = document.createElement("script"); highlightScript.setAttribute("type", "module"); highlightScript.innerHTML = ` import "${ site.url( `${posix.join(options.outputPath, "pagefind-highlight.js")}`, ) }"; new PagefindHighlight({ highlightParam: ${ JSON.stringify(ui.highlightParam) } }); `; document.head.append(highlightScript); } } } }); } };}
lume

Version Info

Tagged at
7 months ago