Skip to content

Commit

Permalink
feat: add basic scaffolding for wasm ranker integration
Browse files Browse the repository at this point in the history
commented out to not cause build issues, as the ranker package is not yet available on the production server
  • Loading branch information
xynydev committed Nov 18, 2024
1 parent fb8f9b4 commit ced7d79
Show file tree
Hide file tree
Showing 4 changed files with 240 additions and 3 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
"typescript": "^5.5.0",
"typescript-eslint": "^8.0.0",
"unplugin-icons": "^0.19.3",
"vite": "^5.4.6"
"vite": "^5.4.6",
"vite-plugin-top-level-await": "^1.4.4",
"vite-plugin-wasm": "^3.3.0"
},
"type": "module",
"dependencies": {
Expand Down
177 changes: 177 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 53 additions & 0 deletions src/routes/search/+page.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
// add the ranker to dependencies to use wasm ranker ("ranker": "file:./pkg/" when testing)

// uncomment to use wasm ranker
// export const ssr = false;

export async function load({ url }) {
// const useWasmRanker = url.searchParams.get('useWasmRanker') === 'true';

// if (!useWasmRanker) {
const response = await fetch('https://mwmbl.org/search/?s=' + url.searchParams.get('q'));
const results: Array<{
title: Array<{ value: string; is_bold: boolean }>;
Expand All @@ -11,4 +19,49 @@ export async function load({ url }) {
query: url.searchParams.get('q') as string | undefined,
results: results
};
// }
// else {
// const wasm = await import('ranker');

// const query = url.searchParams.get('q');
// const ranker = wasm.Ranker.new(query ?? '');
// const terms = ranker.get_query_terms();
// for (const term of terms) {
// const res = await fetch(`https://api.mwmbl.org/api/v1/search/raw?s=${term}`);
// const json = await res.json();
// for (const result of json.results) {
// ranker.add_search_result(result.url, result.title, result.extract);
// }
// }
// const rankedData = ranker.rank();

// const results: Array<{
// title: Array<{ value: string; is_bold: boolean }>;
// extract: Array<{ value: string; is_bold: boolean }>;
// url: string;
// source: string;
// }> = await rankedData.map((result: { url: string; title: string; extract: string }) => {
// return {
// title: result.title.split(' ').map((word: string) => {
// return {
// value: word + ' ',
// is_bold: query?.split(' ').some((q) => q.toLowerCase() === word.toLowerCase())
// };
// }),
// extract: result.extract.split(' ').map((word: string) => {
// return {
// value: word + ' ',
// is_bold: query?.split(' ').some((q) => q.toLowerCase() === word.toLowerCase())
// };
// }),
// url: result.url,
// source: ''
// };
// });

// return {
// query: url.searchParams.get('q') as string | undefined,
// results: results
// };
// }
}
9 changes: 7 additions & 2 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';

import { sveltekit } from '@sveltejs/kit/vite';
import Icons from 'unplugin-icons/vite';
import wasm from 'vite-plugin-wasm';
import topLevelAwait from 'vite-plugin-top-level-await';

export default defineConfig({
plugins: [
sveltekit(),
Icons({
compiler: 'svelte'
})
}),
wasm(),
topLevelAwait()
]
});

0 comments on commit ced7d79

Please sign in to comment.