/
eleventy-edge.js
57 lines (47 loc) · 1.73 KB
/
eleventy-edge.js
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
import {
EleventyEdge,
precompiledAppData,
} from "./_generated/eleventy-edge-app.js";
import notes from "../../src/site/_data/social_archive.json" assert {type: 'json'};
import dateDisplay from "../../src/site/_filters/edgeFilters/dates.js";
import socialLink from "../../src/site/_filters/edgeFilters/socialLink.js";
export default async (request, context) => {
try {
let edge = new EleventyEdge("edge", {
request,
context,
precompiled: precompiledAppData,
// default is [], add more keys to opt-in e.g. ["appearance", "username"]
cookies: [],
});
const url = new URL(request.url);
let filteredNotes = {
filter: "",
results: notes
};
const searchStr = url.searchParams.get("str");
// if there is an empty str param, ignore the search and go to the notes page
for (const [key, value] of url.searchParams.entries()) {
if(key == "str" && value == "") {
return Response.redirect(`${url.origin}/notes`, 302);
}
}
// if there is not str at all, we can ignore
if(!searchStr) {
return context.next();
}
filteredNotes.filter = searchStr;
filteredNotes.results = notes.filter(e => e.full_text.toLowerCase().indexOf(searchStr.toLocaleLowerCase()) !== -1);
edge.config((eleventyConfig) => {
// Add some custom Edge-specific configuration
eleventyConfig.addFilter("dateDisplay", dateDisplay);
eleventyConfig.addShortcode("socialLink", socialLink);
eleventyConfig.addGlobalData("filteredNotes", filteredNotes);
eleventyConfig.addGlobalData("notes", notes);
});
return await edge.handleResponse();
} catch (e) {
console.log("ERROR", { e });
return context.next(e);
}
};