Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions scripts/smoke.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,13 @@ check('snippet falls back to the start of the text when nothing matches', () =>
assert.ok(s.startsWith('Gratitude'));
});

check('search matches an author name', () => {
const idx = new SearchIndex().build([
{ title: 'On stillness', themes: ['Silence'], author: 'Nirodbaran', summary: 'x', body: 'y' },
]);
const hits = idx.search('Nirodbaran');
assert.equal(hits.length, 1);
assert.ok(hits[0].matched.includes('nirodbaran'));
});

console.log(`\n${passed} checks passed.`);
4 changes: 4 additions & 0 deletions src/reader/reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,10 @@ function renderList(items) {
/** Render search hits with a highlighted contextual snippet (issue #3). */
function renderSearchResults(hits) {
els.results.replaceChildren();
if (!hits.length) {
els.results.append(tag('li', 'card', 'No articles match that search. Try a broader word or a theme.'));
return;
}
for (const { article, matched } of hits) {
const li = document.createElement('li');
li.className = 'card';
Expand Down
3 changes: 2 additions & 1 deletion src/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* search.js — small, dependency-free full-text search for an edition.
*
* Builds an in-memory inverted index over each article's title, themes,
* summary, and body, then ranks matches with TF-IDF plus light field
* author, summary, and body, then ranks matches with TF-IDF plus light field
* weighting (a hit in the title counts for more than one in the body). It is
* meant for a single edition's worth of articles — hundreds, not millions —
* which is exactly the scale of a monthly magazine, and it keeps the whole
Expand All @@ -15,6 +15,7 @@
const FIELD_WEIGHTS = Object.freeze({
title: 3,
themes: 2,
author: 2,
summary: 1.5,
body: 1,
});
Expand Down
Loading