Skip to content

Commit

Permalink
feat: add language filter
Browse files Browse the repository at this point in the history
  • Loading branch information
sznowicki committed Dec 22, 2023
1 parent 9aaf336 commit 53f4d2a
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 7 deletions.
6 changes: 4 additions & 2 deletions functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ import { search, getFacets } from '../lib/search.js';
import classNames from 'html-classnames';
import {getDefaultViewData} from '../lib/view.js';
import {emitPageView} from '../lib/plausible.js';
import {parseQuery} from '../lib/parseQuery.js';

export const onRequestGet = async (context) => {
const { request, env } = context;
const { searchParams } = new URL(request.url);
const { q, p = 0 } = Object.fromEntries(searchParams.entries());
const { q} = Object.fromEntries(searchParams.entries());
const { q: searchQuery, lang } = parseQuery(q);

const startTime = Date.now();
const result = q ? await search(env, q, p) : null;
const result = q ? await search(env, searchQuery, lang) : null;
const doneIn = Date.now() - startTime;

const viewDefaults = await getDefaultViewData(env);
Expand Down
8 changes: 4 additions & 4 deletions functions/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ <h1>
<span> curated search for web developers</span></h1>
</div>
<div class="form-main">
<input type="search" name="q" value="{{ q }}" aria-label="Search" placeholder="Type your query" autofocus />
<input type="search" name="q" value="{{ q }}" aria-label="Search" placeholder="e.g.: snowflakes lang:en" autofocus />
<button type="submit">Search</button>
</div>
{{#hasQuery}}
Expand All @@ -37,7 +37,7 @@ <h2>Results <span>generated in {{ doneIn }}ms</span></h2>
<span>Jump to:</span>
<a href="#docs">Docs</a>
<a href="#blogs">Blogs</a>
<a href="#magazines">Magazines</a>
<a href="#magazines">Magazines</a>.
</nav>
{{#hasResults}}
{{#results}}
Expand Down Expand Up @@ -94,9 +94,9 @@ <h4>{{ title }}</h4>
<footer>
<h2 class="visually-hidden">About</h2>
<p class="about__brand">Kukei.eu - curated search for web developers</p>
<p>Still MVP, contact me on <a href="https://social.nowicki.io/@hey">my Mastodon</a> or check out the <a href="https://github.com/Kukei-eu">source code</a></p>
<p>You can type "lang:en" for language filter.</p>
<p>Contact me on <a href="https://social.nowicki.io/@hey">my Mastodon</a> or check out the <a href="https://github.com/Kukei-eu">source code</a></p>
<p>So far indexed <format-number>{{totalPages}}</format-number> internet pages</p>
<p>See all sources <a href="https://github.com/Kukei-eu/spider/blob/main/index-sources.js">on GitHub</a>. Edits welcomed!</p>
<p><a href="/about">About (Impressum)</a>, <a href="/status">Index status</a></p>
</footer>
<!-- Cloudflare Web Analytics -->
Expand Down
43 changes: 43 additions & 0 deletions lib/__tests__/parseQuery.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { describe, it} from 'node:test';
import assert from 'node:assert';
import { parseQuery } from '../parseQuery.js';

describe('parseQuery', () => {
it('should return q same as raw', () => {
const { q, lang } = parseQuery('foo');
assert.strictEqual(q, 'foo');
assert.strictEqual(lang, undefined);
});
it('should return q undefined when raw is just whitespaces', () => {
const { q, lang } = parseQuery(' ');
assert.strictEqual(q, undefined);
assert.strictEqual(lang, undefined);
});
it('should return q and lang when raw has lang', () => {
const {q, lang} = parseQuery('foo lang:en');
assert.strictEqual(q, 'foo');
assert.strictEqual(lang, 'en');
});
it('should return q and lang when raw has lang', () => {
const {q, lang} = parseQuery('lang:en lang ');
assert.strictEqual(q, 'lang');
assert.strictEqual(lang, 'en');
});
it('should return q and lang when raw has lang', () => {
const {q, lang} = parseQuery('lang:en');
assert.strictEqual(q, '');
assert.strictEqual(lang, 'en');
});

it('should return undefined q when no query at all', () => {
const { q, lang } = parseQuery();
assert.strictEqual(q, undefined);
assert.strictEqual(lang, undefined);
});

it('should return empty q and lang', () => {
const { q, lang} = parseQuery('lang:pl');;
assert.strictEqual(q, '');
assert.strictEqual(lang, 'pl');
});
});
19 changes: 19 additions & 0 deletions lib/parseQuery.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export const parseQuery = (rawQuery) => {
if (!rawQuery) {
return { q: undefined, lang: undefined };
}

const trimmed = rawQuery.trim();
let q = trimmed.length > 0 ? trimmed : undefined;

if (!q) {
return { q };
}

const lang = trimmed.match(/lang:(\w{2})/)?.[1];
if (lang) {
q = q.replace(/lang:\w{2}/, '').trim();
}

return { q, lang };
};
2 changes: 1 addition & 1 deletion lib/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const makeOptions = (lang) => ({
* @param {Object} options
* @returns {Promise<void>}
*/
const doSearch = async (results, env, index, q, { lang } = {}) => {
const doSearch = async (results, env, index, q, lang) => {
const meiliClient = getMeiliClient(env);
const searchResult = await meiliClient.index(`kukei-${index}`).search(q, makeOptions(lang));

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"license": "UNLICENSED",
"scripts": {
"dev": "wrangler pages dev dist --compatibility-flag=nodejs_compat --compatibility-date=2023-12-05",
"test": "node --test",
"make:hash": "node ./scripts/make-hash.js",
"build": "yarn make:hash && yarn install --production",
"lint": "eslint ."
Expand Down

0 comments on commit 53f4d2a

Please sign in to comment.