/
search.astro
372 lines (334 loc) · 9.9 KB
/
search.astro
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
---
import PageLayout from "@layouts/PageLayout.astro";
import {
getArticles,
getGardens,
getNotes,
normalizeArt,
normalizeGardens,
normalizeNotes,
} from "src/utils";
import artData from "../content/_art";
type SearchableContentItem = {
body?: string;
date: string;
description: string;
tags: string[];
title: string;
type: "article" | "note" | "garden" | "art";
url: string;
year: number;
};
const searchableContent: Array<SearchableContentItem> = [];
const years = new Set<number>();
const tags = new Set<string>();
function addToSearchableContent(array: Array<any>) {
array.forEach((item) => {
const date = new Date(item.data.pubDate);
const year = date.getFullYear();
years.add(year);
item.data.tags?.forEach((tag) => tags.add(tag));
const type =
item.collection.slice(-1) === "s"
? item.collection.slice(0, -1)
: item.collection;
searchableContent.push({
body: item.body,
date: item.data.pubDate,
description: item.data.description || "",
tags: item.data.tags || [],
title: item.data.title,
type,
url: `/${item.collection}/${item.slug}`,
year: year,
});
});
}
addToSearchableContent(await getArticles());
addToSearchableContent(normalizeNotes(await getNotes()));
addToSearchableContent(normalizeGardens(await getGardens(), "FIRST"));
addToSearchableContent(normalizeArt(artData));
const sortedYears = Array.from(years).sort((a, b) => b - a);
const sortedTags = Array.from(tags).sort((a, b) =>
a.toLowerCase().localeCompare(b.toLowerCase())
);
---
<PageLayout documentTitle="Search" title="Search">
<form action="." id="search" method="get">
<div class="line(xs)" style="align-items:initial;">
<label class="line(xs)">
<svg
aria-label="Search for content"
xmlns="http://www.w3.org/2000/svg"
width="24"
height="24"
fill="#000000"
viewBox="0 0 256 256"
><rect width="256" height="256" fill="none"></rect><circle
cx="116"
cy="116"
r="84"
fill="none"
stroke="currentColor"
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="16"></circle><line
x1="175.4"
y1="175.4"
x2="224"
y2="224"
fill="none"
stroke="currentColor"
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="16"></line>
</svg>
<input
autofocus
name="q"
placeholder="Search content title, description, or body"
type="search"
/>
</label>
<button>Search</button>
</div>
<fieldset>
<legend class="hidden()">Filters</legend>
<div class="line(m)">
<label class="line(xs)">
Type
<select name="type">
<option value="">All</option>
{
["Articles", "Notes", "Gardens", "Art"].map((type) => {
let value = type.toLowerCase();
if (value.slice(-1) === "s") {
value = value.slice(0, -1);
}
return <option value={value}>{type}</option>;
})
}
</select>
</label>
<label class="line(xs)">
Year
<select name="year">
<option value="">All</option>
{sortedYears.map((year) => <option value={year}>{year}</option>)}
</select>
</label>
<label class="line(xs)">
Tag
<select name="tag">
<option value="">All</option>
{
sortedTags.map((tag) => {
return <option value={tag}>{tag}</option>;
})
}
</select>
</label>
</div>
</fieldset>
<output id="search-output"></output>
<template id="search-result">
<li class="line(xs)">
<span>%TYPE%</span>
<div>
<a href="%URL%">%TITLE%</a>
<time datetime="%DATE%">(%DISPLAY_DATE%)</time>
</div>
</li>
</template>
<script is:inline src="https://cdn.jsdelivr.net/npm/fuse.js@7.0.0"></script>
<script is:inline define:vars={{ searchableContent }}>
// Sync form with URL
const searchParams = new URLSearchParams(window.location.search);
const query = searchParams.get("q");
if (query) {
document.querySelector("input[name=q]").value = query;
}
const type = searchParams.get("type");
if (type) {
document.querySelector("select[name=type]").value = type;
}
const year = searchParams.get("year");
if (year) {
document.querySelector("select[name=year]").value = year;
}
const tag = searchParams.get("tag");
if (tag) {
document.querySelector("select[name=tag]").value = tag;
}
const output = document.getElementById("search-output");
const liTemplate = document.querySelector("template#search-result")
.content.firstElementChild;
function searchAndRender(query, type, year, tag) {
if (!query && !type && !year && !tag) {
output.textContent = "";
return;
}
// Filter content based on search parameters
const filteredList = searchableContent.filter((item) => {
let include = true;
if (type && item.type != type) {
include = false;
}
if (year && item.year != year) {
include = false;
}
if (tag && !item.tags.includes(tag)) {
include = false;
}
return include;
});
let listToRender = filteredList;
const fuse = new Fuse(filteredList, {
keys: ["body", "description", "tags", "title", "url"],
threshold: 0.2,
});
if (query) {
listToRender = fuse.search(query).map((result) => result.item);
}
// Render search results
const heading = query ? `Results for "${query}"` : "Results";
if (listToRender.length === 0) {
output.innerHTML = `
<h2>${heading}</h2>
<p>No results found</p>
`;
return;
}
output.innerHTML = `
<h2>${heading}</h2>
<p>Found ${listToRender.length} result${
listToRender.length === 1 ? "" : "s"
}</p>
<ol>
${listToRender
.map((item) => {
const displayDate = new Date(item.date).toLocaleDateString();
const html = liTemplate
.cloneNode(true)
.outerHTML.replace(/%TYPE%/g, item.type)
.replace(/%URL%/g, item.url)
.replace(/%TITLE%/g, item.title)
.replace(/%DATE%/g, item.date)
.replace(/%DISPLAY_DATE%/g, displayDate);
return html;
})
.join("")}
</ol>
`;
}
searchAndRender(query, type, year, tag);
const form = document.querySelector("main form");
function handleFormChange() {
const searchParams = new URLSearchParams();
const formData = new FormData(form);
for (const [key, value] of formData) {
searchParams.set(key, value);
}
window.history.pushState(
{},
"",
`${window.location.pathname}?${searchParams}`
);
searchAndRender(
formData.get("q"),
formData.get("type"),
formData.get("year"),
formData.get("tag")
);
}
form.addEventListener("change", (e) => {
if (e.target.tagName === "SELECT") {
if (e.target.value) {
e.target.setAttribute("value", e.target.value);
} else {
e.target.removeAttribute("value");
}
}
handleFormChange();
});
form.addEventListener("input", handleFormChange);
</script>
</form>
<style>
form :is(input, select) {
color: var(--text-color);
}
label:has(input) {
border: var(--stroke-thickness) solid var(--off-background);
flex-grow: 1;
padding: var(--space-2xs);
}
label:has(input):is(:focus-within) {
color: var(--anchor-color);
outline: var(--stroke-thickness) solid var(--anchor-color);
outline-offset: var(--stroke-thickness);
}
label svg {
width: 20px;
}
label input {
background-color: transparent;
border: 0;
flex-grow: 1;
}
label input:is(:focus, :hover) {
outline: transparent;
}
form input::placeholder {
color: var(--lighter-text-color);
}
form fieldset {
border: 0;
font-size: small;
padding: var(--space-xs) 0;
margin: 0;
}
form button {
background-color: var(--off-background);
border: var(--stroke-thickness) solid var(--off-background);
color: var(--text);
}
form button:is(:focus, :hover) {
background-color: var(--primary);
border-color: var(--primary);
color: var(--background);
}
form legend {
font-size: small;
color: var(--lighter-text-color);
text-transform: uppercase;
padding-inline: 1ch;
}
form select {
background-color: var(--background);
border: var(--stroke-thickness) solid var(--off-background);
}
form select[value] {
color: var(--anchor-color);
}
form select option:is(:checked,:focus,:hover) {
background-color: var(--off-background);
}
:global(#search-output ol) {
list-style-type: none;
padding: 0;
}
li {
margin-block-start: var(--space-2xs);
}
li span {
background-color: var(--off-background);
font-size: x-small;
text-transform: uppercase;
padding: 0.25em 0.5em;
}
li time {
color: var(--lighter-text-color);
}
</style>
</PageLayout>