Skip to content

Commit 6eabc26

Browse files
committed
make search ts
1 parent 36a3db0 commit 6eabc26

File tree

9 files changed

+26
-27
lines changed

9 files changed

+26
-27
lines changed

apps/codingcatdev/src/lib/search/SearchModal.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ It appears when the user clicks on the `Search` component or presses the corresp
88
99
import { onMount } from 'svelte';
1010
import { afterNavigate } from '$app/navigation';
11-
import { query, recent } from './stores.js';
11+
import { query, recent } from './stores';
1212
import SearchResults from './SearchResults.svelte';
13-
import SearchWorker from './search-worker.js?worker';
13+
import SearchWorker from './search-worker?worker';
1414
import { modalStore } from '@codingcatdev/blackcatui';
1515
1616
/** @type {HTMLElement} */

apps/codingcatdev/src/lib/search/SearchResultList.svelte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
{#each results as result (result.href)}
4343
<li>
4444
<a
45+
class="!no-underline !text-token"
4546
data-sveltekit-preload-data
4647
href={result.href}
4748
on:click={() => dispatch('select', { href: result.href })}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export { default as Search } from './Search.svelte';
22
export { default as SearchModal } from './SearchModal.svelte';
33
export { default as SearchResults } from './SearchResults.svelte';
4-
export { init, inited, lookup, search } from './search.js';
4+
export { init, inited, lookup, search } from './search';
55

66
/**
77
* @typedef {import('./types.js').Block} Block

apps/codingcatdev/src/lib/search/search-worker.js renamed to apps/codingcatdev/src/lib/search/search-worker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { init, search, lookup } from './search.js';
1+
import { init, search, lookup } from './search';
22

33
addEventListener('message', async (event) => {
44
const { type, payload } = event.data;

apps/codingcatdev/src/lib/search/search.js renamed to apps/codingcatdev/src/lib/search/search.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
11
import flexsearch from 'flexsearch';
2-
2+
import type { Block, Tree } from './types';
3+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
34
// @ts-expect-error
45
const Index = /** @type {import('flexsearch').Index} */ (flexsearch.Index) ?? flexsearch;
56

67
/** If the search is already initialized */
78
export let inited = false;
89

9-
/** @type {import('flexsearch').Index<any>[]} */
10-
let indexes;
10+
let indexes: import('flexsearch').Index<any>[];
1111

1212
/** @type {Map<string, import('./types').Block>} */
1313
const map = new Map();
1414

1515
/** @type {Map<string, string>} */
1616
const hrefs = new Map();
1717

18-
/**
19-
* Initialize the search index
20-
* @param {import('./types').Block[]} blocks
21-
*/
22-
export function init(blocks) {
18+
19+
export function init(blocks: Block[]) {
2320
if (inited) return;
2421

2522
// we have multiple indexes, so we can rank sections (migration guide comes last)
@@ -38,6 +35,9 @@ export function init(blocks) {
3835
// We'd probably want to test both implementations across browsers if memory usage becomes an issue
3936
// TODO: fix the type by updating flexsearch after
4037
// https://github.com/nextapps-de/flexsearch/pull/364 is merged and released
38+
39+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
40+
//@ts-ignore
4141
indexes[block.rank ?? 0].add(block.href, `${title} ${block.content}`);
4242

4343
hrefs.set(block.breadcrumbs.join('::'), block.href);
@@ -51,17 +51,19 @@ export function init(blocks) {
5151
* @param {string} query
5252
* @returns {import('./types').Tree[]}
5353
*/
54-
export function search(query) {
54+
export function search(query: string) {
5555
const escaped = query.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
5656
const regex = new RegExp(`(^|\\b)${escaped}`, 'i');
5757

5858
const blocks = indexes
5959
.flatMap((index) => index.search(query))
60+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
61+
//@ts-ignore
6062
.map(lookup)
6163
.map((block, rank) => ({ block: /** @type{import('./types').Block} */ (block), rank }))
6264
.sort((a, b) => {
63-
const a_title_matches = regex.test(/** @type {string} */ (a.block.breadcrumbs.at(-1)));
64-
const b_title_matches = regex.test(/** @type {string} */ (b.block.breadcrumbs.at(-1)));
65+
const a_title_matches = regex.test(/** @type {string} */(a.block.breadcrumbs.at(-1)));
66+
const b_title_matches = regex.test(/** @type {string} */(b.block.breadcrumbs.at(-1)));
6567

6668
// massage the order a bit, so that title matches
6769
// are given higher priority
@@ -82,16 +84,11 @@ export function search(query) {
8284
* Get a block with details by its href
8385
* @param {string} href
8486
*/
85-
export function lookup(href) {
87+
export function lookup(href: string) {
8688
return map.get(href);
8789
}
8890

89-
/**
90-
* @param {string[]} breadcrumbs
91-
* @param {import('./types').Block[]} blocks
92-
* @returns {import('./types').Tree}
93-
*/
94-
function tree(breadcrumbs, blocks) {
91+
function tree(breadcrumbs: string[], blocks: Block[]): Tree {
9592
const depth = breadcrumbs.length;
9693

9794
const node = blocks.find((block) => {
@@ -109,6 +106,8 @@ function tree(breadcrumbs, blocks) {
109106
return {
110107
breadcrumbs,
111108
href: /** @type {string} */ (hrefs.get(breadcrumbs.join('::'))),
109+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
110+
//@ts-ignore
112111
node: /** @type {import('./types').Block} */ (node),
113112
children: child_parts.map((part) => tree([...breadcrumbs, part], descendants))
114113
};

apps/codingcatdev/src/lib/search/stores.js renamed to apps/codingcatdev/src/lib/search/stores.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@ export const query = writable('');
66
* @param {string} key
77
* @param {any} fallback
88
*/
9-
function storable(key, fallback) {
9+
function storable(key: string, fallback: any[]) {
1010
try {
1111
let value = key in localStorage ? JSON.parse(localStorage[key]) : fallback;
1212

1313
const store = writable(value);
1414

15-
const set = (new_value) => {
15+
const set = (new_value: any) => {
1616
store.set((value = new_value));
1717
localStorage[key] = JSON.stringify(value);
1818
};
1919

2020
return {
2121
subscribe: store.subscribe,
2222
set,
23-
update: (fn) => set(fn(value))
23+
update: (fn: (arg0: any) => any) => set(fn(value))
2424
};
2525
} catch {
2626
return writable(fallback);

apps/codingcatdev/src/routes/api/content.json/+server.js renamed to apps/codingcatdev/src/routes/api/content.json/+server.ts

File renamed without changes.

apps/codingcatdev/src/routes/api/content.json/content.server.js renamed to apps/codingcatdev/src/routes/api/content.json/content.server.ts

File renamed without changes.

apps/codingcatdev/src/routes/search/+page.server.js renamed to apps/codingcatdev/src/routes/search/+page.server.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { init, inited, search } from '$lib/search';
22

3-
/** @type {import('./$types').PageServerLoad} */
43
export async function load({ url, fetch }) {
54
if (!inited) {
65
const res = await fetch('/api/content.json');
@@ -10,7 +9,7 @@ export async function load({ url, fetch }) {
109
init(blocks);
1110
}
1211

13-
const query = url.searchParams.get('q');
12+
const query = url.searchParams.get('q') || '';
1413

1514
const results = query ? search(query) : [];
1615

0 commit comments

Comments
 (0)