Skip to content

Commit f6a4414

Browse files
authored
Merge pull request #19602 from dvdksn/fuse-search
hugo: use fuse.js search instead of algolia
2 parents 4504868 + 6ccbfa3 commit f6a4414

File tree

18 files changed

+181
-919
lines changed

18 files changed

+181
-919
lines changed

assets/css/search.css

Lines changed: 0 additions & 635 deletions
This file was deleted.

assets/css/styles.css

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
@import "tailwindcss/base";
44
@import "/assets/css/global";
55
@import "/assets/css/typography";
6-
@import "/assets/css/search";
76
@import "/assets/css/hack";
87

98
@import "tailwindcss/components";

assets/js/src/alpine.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import Alpine from 'alpinejs'
22
import collapse from '@alpinejs/collapse'
33
import persist from '@alpinejs/persist'
4+
import focus from '@alpinejs/focus'
45

56
window.Alpine = Alpine
67

78
Alpine.plugin(collapse)
8-
Alpine.store("showSidebar", false)
9-
109
Alpine.plugin(persist)
10+
Alpine.plugin(focus)
11+
Alpine.store("showSidebar", false)
1112

1213
Alpine.start()

assets/js/src/search.js

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,54 @@
1-
import docsearch from "@docsearch/js";
2-
import * as params from "@params"; // hugo dict
3-
4-
const { appid, apikey, indexname } = params;
5-
6-
docsearch({
7-
container: "#docsearch",
8-
appId: appid,
9-
apiKey: apikey,
10-
indexName: indexname,
11-
transformItems(items) {
12-
return items.map((item) => ({
13-
...item,
14-
url: item.url.replace("https://docs.docker.com", ""),
15-
}));
16-
},
17-
});
1+
import Fuse from "fuse.js";
2+
3+
let indexed = false;
4+
let handler = null;
5+
6+
const modalSearchInput = document.querySelector("#modal-search-input");
7+
const modalSearchResults = document.querySelector("#modal-search-results");
8+
9+
async function initializeIndex() {
10+
const index = await fetch("/metadata.json").then((response) =>
11+
response.json(),
12+
);
13+
14+
const options = {
15+
keys: [
16+
{ name: "title", weight: 2 },
17+
{ name: "description", weight: 1 },
18+
{ name: "keywords", weight: 1 },
19+
{ name: "tags", weight: 1 },
20+
],
21+
minMatchCharLength: 2,
22+
threshold: 0.2,
23+
};
24+
25+
handler = new Fuse(index, options);
26+
indexed = true;
27+
}
28+
29+
async function executeSearch(query) {
30+
!indexed && (await initializeIndex());
31+
const results = handler.search(query).map(({ item }) => item);
32+
return results
33+
}
34+
35+
async function modalSearch(e) {
36+
const query = e.target.value;
37+
results = await executeSearch(query)
38+
39+
let resultsHTML = `<div>${results.length} results</div>`;
40+
resultsHTML += results
41+
.map((item) => {
42+
return `<div class="bg-gray-light-100 dark:bg-gray-dark-200 rounded p-4">
43+
<div class="flex flex-col">
44+
<a class="link" href="${item.url}">${item.title}</a>
45+
<p>${item.description}</p>
46+
</div>
47+
</div>`;
48+
})
49+
.join("");
50+
51+
modalSearchResults.innerHTML = resultsHTML;
52+
}
53+
54+
modalSearchInput.addEventListener("input", modalSearch);

hugo_stats.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,15 @@
112112
"Without-packages",
113113
"Without-systemd",
114114
"absolute",
115+
"appearance-none",
115116
"aspect-video",
116117
"bake-action",
117118
"basis-1/3",
118119
"basis-2/3",
119120
"bg-accent-light",
120121
"bg-amber-light",
121122
"bg-amber-light-200",
123+
"bg-background-dark/70",
122124
"bg-background-light",
123125
"bg-black/70",
124126
"bg-blue-light",
@@ -135,6 +137,7 @@
135137
"bg-pattern-purple",
136138
"bg-pattern-verde",
137139
"bg-red-light",
140+
"bg-transparent",
138141
"bg-violet-light",
139142
"bg-white",
140143
"block",
@@ -160,6 +163,7 @@
160163
"dark:bg-blue-dark",
161164
"dark:bg-blue-dark-400",
162165
"dark:bg-gray-dark-100",
166+
"dark:bg-gray-dark-100/70",
163167
"dark:bg-gray-dark-200",
164168
"dark:bg-gray-dark-300",
165169
"dark:bg-gray-dark-300/50",
@@ -174,6 +178,7 @@
174178
"dark:border-gray-dark-400",
175179
"dark:decoration-blue-dark",
176180
"dark:fill-blue-dark",
181+
"dark:focus:outline-blue-dark",
177182
"dark:from-accent-dark",
178183
"dark:from-background-dark",
179184
"dark:from-blue-dark-400",
@@ -182,6 +187,7 @@
182187
"dark:hover:bg-blue-dark-500",
183188
"dark:hover:bg-gray-dark-500",
184189
"dark:hover:text-white",
190+
"dark:outline-gray-dark",
185191
"dark:prose-invert",
186192
"dark:syntax-dark",
187193
"dark:text-blue-dark",
@@ -207,9 +213,11 @@
207213
"fixed",
208214
"flex",
209215
"flex-1",
216+
"flex-auto",
210217
"flex-col",
211218
"flex-col-reverse",
212219
"flex-wrap",
220+
"focus:outline-blue-light",
213221
"font-medium",
214222
"footnote-backref",
215223
"footnote-ref",
@@ -230,6 +238,7 @@
230238
"grid-cols-2",
231239
"group",
232240
"group-hover:block'",
241+
"h-12",
233242
"h-16",
234243
"h-2",
235244
"h-32",
@@ -279,11 +288,17 @@
279288
"lg:pb-2",
280289
"lg:scale-100",
281290
"lg:text-base",
291+
"lg:w-75ch",
282292
"lg:w-[1200px]",
293+
"lg:w-[600px]",
294+
"lg:w-[840px]",
283295
"link",
284296
"lntable",
285297
"lntd",
298+
"m-2",
286299
"m-auto",
300+
"max-h-[77vh]",
301+
"max-h-[80vh]",
287302
"max-h-full",
288303
"max-w-56",
289304
"max-w-[1400px]",
@@ -304,13 +319,17 @@
304319
"md:hidden",
305320
"md:max-w-[66%]",
306321
"md:scale-100",
322+
"md:w-full",
323+
"min-h-0",
307324
"min-h-screen",
308325
"min-w-0",
309326
"ml-3",
310327
"ml-auto",
311328
"mt-1",
312329
"mt-20",
313330
"mt-auto",
331+
"mx-1",
332+
"mx-8",
314333
"mx-auto",
315334
"my-0",
316335
"my-2",
@@ -321,9 +340,14 @@
321340
"object-cover",
322341
"openSUSE-and-SLES",
323342
"origin-bottom-right",
343+
"outline",
344+
"outline-2",
345+
"outline-gray-light",
346+
"overflow-auto",
324347
"overflow-clip",
325348
"overflow-hidden",
326349
"overflow-x-hidden",
350+
"overflow-y-auto",
327351
"overflow-y-scroll",
328352
"p-1",
329353
"p-2",
@@ -350,6 +374,7 @@
350374
"px-3",
351375
"px-4",
352376
"py-1",
377+
"py-12",
353378
"py-2",
354379
"py-20",
355380
"py-4",
@@ -366,6 +391,7 @@
366391
"rounded-[6px]",
367392
"rounded-b-lg",
368393
"rounded-full",
394+
"rounded-lg",
369395
"rounded-sm",
370396
"scale-50",
371397
"scale-75",
@@ -415,13 +441,16 @@
415441
"w-8",
416442
"w-[1200px]",
417443
"w-[32px]",
444+
"w-[840px]",
418445
"w-full",
419446
"w-lvw",
420447
"warning",
421448
"xl:grid-cols-3",
422449
"xl:grid-cols-4",
423450
"xl:grid-cols-main-xl",
451+
"xl:w-100ch",
424452
"xl:w-[1200px]",
453+
"xl:w-[800px]",
425454
"youtube-video",
426455
"z-10",
427456
"z-20",

layouts/_default/cli.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
{{ .Scratch.Set "headings" slice }}
1313
{{ .Scratch.Set "subheadings" slice }}
1414
{{ partial "breadcrumbs.html" . }}
15-
<article class="DocSearch-content prose max-w-none dark:prose-invert">
15+
<article class="prose max-w-none dark:prose-invert">
1616
<h1 class="scroll-mt-36">{{ .Title }}</h1>
1717
<table>
1818
<tbody>

layouts/_default/glossary.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
{{ define "main" }}
66
{{ partial "breadcrumbs.html" . }}
7-
<article class="DocSearch-content prose max-w-none dark:prose-invert">
7+
<article class="prose max-w-none dark:prose-invert">
88
{{ with .Title }}
99
<h1 class="scroll-mt-36">{{ . }}</h1>
1010
{{ end }}

layouts/_default/list.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
{{ define "main" }}
66
{{ partial "breadcrumbs.html" . }}
7-
<article class="DocSearch-content prose max-w-none dark:prose-invert">
7+
<article class="prose max-w-none dark:prose-invert">
88
{{ with .Title }}
99
<h1 class="scroll-mt-36">{{ . }}</h1>
1010
{{ end }}

layouts/_default/single.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
{{ define "main" }}
66
{{ partial "breadcrumbs.html" . }}
7-
<article class="DocSearch-content prose max-w-none dark:prose-invert">
7+
<article class="prose max-w-none dark:prose-invert">
88
{{ with .Title }}
99
<h1 class="scroll-mt-36">{{ . }}</h1>
1010
{{ end }}

layouts/index.metadata.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
{{- $title := partialCached "utils/title.html" . . -}}
44
{{- $desc := partialCached "utils/description.html" . . -}}
55
{{- $kwd := partialCached "utils/keywords.html" . . -}}
6-
{{- jsonify (dict "url" .Permalink "title" $title "description" $desc "keywords" $kwd) -}},
6+
{{- $tags := slice -}}
7+
{{- range (.GetTerms "tags") -}}
8+
{{ $tags = $tags | append .LinkTitle }}
9+
{{- end -}}
10+
{{- jsonify (dict "url" .Permalink "title" $title "description" $desc "keywords" $kwd "tags" $tags) -}},
711
{{- end -}}
812
{}]

0 commit comments

Comments
 (0)