diff --git a/package-lock.json b/package-lock.json
index 8ac2dfb..2e72a29 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -642,6 +642,11 @@
"integrity": "sha1-miyr0bno4K6ZOkv31YdcOcQujqw=",
"dev": true
},
+ "lunr": {
+ "version": "2.3.9",
+ "resolved": "https://registry.npmjs.org/lunr/-/lunr-2.3.9.tgz",
+ "integrity": "sha512-zTU3DaZaF3Rt9rhN3uBMGQD3dD2/vFQqnvZCDv4dl5iOzq2IZQqTxu90r4E5J+nP70J3ilqVCrbho2eWaeW8Ow=="
+ },
"magic-string": {
"version": "0.25.7",
"resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.25.7.tgz",
diff --git a/package.json b/package.json
index 932b4f5..204138d 100644
--- a/package.json
+++ b/package.json
@@ -9,6 +9,7 @@
"compression": "^1.7.1",
"dayjs": "^1.9.7",
"highlight.js": "^10.4.1",
+ "lunr": "^2.3.9",
"polka": "next",
"sirv": "^1.0.9",
"svelte-cloudinary": "^0.2"
diff --git a/src/components/Nav.svelte b/src/components/Nav.svelte
index d309fb7..342f150 100644
--- a/src/components/Nav.svelte
+++ b/src/components/Nav.svelte
@@ -101,6 +101,11 @@
NB
+ -
+
+
+
+
{#each routes as { name, href }}
-
diff --git a/src/components/SearchResult.svelte b/src/components/SearchResult.svelte
new file mode 100644
index 0000000..8a4cdd3
--- /dev/null
+++ b/src/components/SearchResult.svelte
@@ -0,0 +1,50 @@
+
+
+
+
+
-
+
+
{slug.replace(/-/g, ' ')}
+ {type}
+ Score: {result.score.toFixed(1)}
+
+
diff --git a/src/routes/search.json.js b/src/routes/search.json.js
new file mode 100644
index 0000000..0e94a02
--- /dev/null
+++ b/src/routes/search.json.js
@@ -0,0 +1,36 @@
+import lunr from 'lunr'
+
+import { getAll } from '../lib/wp'
+
+function removeHTML(s) {
+ return s.replace(/<.*?>|\s+|\d+;/g, ' ').trim()
+}
+
+async function convertForIdx(type, fields = []) {
+ const items = await getAll(type)
+ const defaults = ['title', 'content', 'slug']
+ return items.map((item) => ({
+ url: `${item.type}/${item.slug}`,
+ data: [...defaults, ...fields].map((field) => removeHTML(item[field])).join(' '),
+ }))
+}
+
+export async function get(req, res) {
+ const all = await Promise.all([
+ convertForIdx('projects', ['description']),
+ convertForIdx('pages'),
+ convertForIdx('posts'),
+ convertForIdx('works', ['role']),
+ ])
+
+ const idx = lunr(function () {
+ this.ref('url')
+ this.field('data')
+
+ all.flat().forEach((doc) => {
+ this.add(doc)
+ })
+ })
+ res.setHeader('Content-Type', 'application/json')
+ res.end(JSON.stringify(idx))
+}
diff --git a/src/routes/search.svelte b/src/routes/search.svelte
new file mode 100644
index 0000000..e00002a
--- /dev/null
+++ b/src/routes/search.svelte
@@ -0,0 +1,58 @@
+
+
+
+
+
+
+
+
+
+ {#each results as result (result.ref)}
+
+ {/each}
+
+