Skip to content

Commit

Permalink
search: combine indices
Browse files Browse the repository at this point in the history
Signed-off-by: Sumner Evans <me@sumnerevans.com>
  • Loading branch information
sumnerevans committed Feb 28, 2025
1 parent cf08935 commit 0562797
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 28 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 14 additions & 15 deletions generate_index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
var lunr = require('lunr'),
stdin = process.stdin,
stdout = process.stdout,
buffer = []
const lunr = require('lunr');

stdin.resume()
stdin.setEncoding('utf8')
process.stdin.resume();
process.stdin.setEncoding('utf8');

stdin.on('data', function(data) {
buffer.push(data)
})
const buffer = [];
process.stdin.on('data', (d) => buffer.push(d));

stdin.on('end', function() {
var idx = lunr(function() {
process.stdin.on('end', () => {
const data = JSON.parse(buffer.join(''));
var lunrIndex = lunr(function() {
this.ref("permalink");
["title", "contents", "tags", "categories"].forEach(f => this.field(f));
// This is so that we can highlight stuff.
this.metadataWhitelist = ["position"];

JSON.parse(buffer.join('')).forEach(article => {
data.forEach(article => {
this.add({
permalink: article.permalink,
title: article.title,
Expand All @@ -28,5 +23,9 @@ stdin.on('end', function() {
});
});

stdout.write(JSON.stringify(idx))
const index = {};
for (const article of data) {
index[article.permalink] = article;
}
process.stdout.write(JSON.stringify({ index, lunrIndex }));
})
25 changes: 12 additions & 13 deletions themes/smol/assets/js/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ interface RawRecord {
}

document.addEventListener("DOMContentLoaded", async () => {
const version = "v2";
let rawIndex: Record<string, RawRecord> = {};
let lunrSearchIndex: lunr.Index | undefined;
const searchInput = document.getElementById(
Expand All @@ -33,7 +32,9 @@ document.addEventListener("DOMContentLoaded", async () => {
const hls = Object.keys(searchResult.matchData.metadata);
const hlQuery = hls.map((h) => `hl=${encodeURIComponent(h)}`).join("&");
const [categoriesList, tagsList] = ["categories", "tags"].map((t) =>
article[t]?.map((c) => `<a href="/${t}/${c.toLowerCase()}">${c}</a>`)
article[t]?.map(
(c) => `<a href="/${t}/${c.toLowerCase().replace(" ", "-")}">${c}</a>`
)
);
const searchResultDiv = document.createElement("div");
searchResultDiv.classList.add("search-result");
Expand Down Expand Up @@ -90,23 +91,21 @@ document.addEventListener("DOMContentLoaded", async () => {
console.log("Got content hash", hash);

const loadSearchIndex = async () => {
const version = "v3";
if (lunrSearchIndex) {
searchInput.focus();
return;
}
console.time("fetch indexes");
const [indexResp, searchIndexResp] = await Promise.all(
["", "search-"].map((name) =>
fetch(`/${name}index.json?v=${version}&cb=${hash}`, { method: "GET" })
)
);
console.timeEnd("fetch indexes");
console.time("fetch index");
const searchIndexUrl = `/search-index.json?v=${version}&ch=${hash}`;
const searchIndex = await (
await fetch(searchIndexUrl, { method: "GET" })
).json();
console.timeEnd("fetch index");

console.time("initialize the search index");
for (const result of await indexResp.json()) {
rawIndex[result.permalink] = result;
}
lunrSearchIndex = lunr.Index.load(await searchIndexResp.json());
rawIndex = searchIndex.index;
lunrSearchIndex = lunr.Index.load(searchIndex.lunrIndex);
console.timeEnd("initialize the search index");

searchInput.removeAttribute("disabled");
Expand Down

0 comments on commit 0562797

Please sign in to comment.