Svelte
An example of how you can integrate Boox with Svelte:
Project setup
Create a new Svelte project using Vite with TypeScript template:
npm create vite@latest boox-svelte --template svelte-ts
BASH
Now run:
cd boox-svelte && npm install
BASH
Install Boox and other dependencies:
npm install -D boox debounce metaphone stemmer stopword @types/stopword
BASH
File structure
boox-svelte
├── lib
│ ├── components
│ │ ├── Footer.svelte
│ │ ├── Search.svelte
│ │ ├── SearchInfo.svelte
│ │ ├── SearchResult.svelte
│ │ └── SearchResults.svelte
│ ├── App.svelte
│ ├── main.ts
│ └── types.ts
├── index.html
└── vite.config.ts
TEXT
Did you know?
You can remove unnecessary files from the default Vite installation.
Code snippets
Kindly replicate the code snippets provided below:
<!doctype html>
<html lang="en" data-bs-theme="dark">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Svelte example - Boox</title>
<link
href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH"
crossorigin="anonymous"
/>
</head>
<body class="bg-body-secondary">
<div id="app" class="vstack min-vh-100"></div>
<script type="module" src="/src/main.ts"></script>
</body>
</html>
HTML
import { defineConfig } from 'vite'
import { svelte } from '@sveltejs/vite-plugin-svelte'
export default defineConfig({
plugins: [svelte()],
build: {
rollupOptions: {
external: ['os']
}
}
})
TS
Below is the content of the src/*
files:
<script lang="ts">
import Footer from './lib/Footer.svelte'
import Search from './lib/Search.svelte'
</script>
<main
class="mx-auto my-4 bg-body w-100 rounded-3 shadow"
style="max-width: 30rem"
>
<Search />
</main>
<Footer />
SVELTE
import App from './App.svelte'
const app = new App({
target: document.getElementById('app') as HTMLElement
})
export default app
TS
export interface Pokemon {
id: string
image_url: string
caption: string
name: string
hp: number
set_name: string
}
TS
Below is the content of the src/lib/*
files:
<footer class="container mt-auto py-3">
<p class="d-flex justify-content-center text-muted">
Search by
<a
class="icon-link ms-2"
href="https://github.com/bent10/boox"
target="_blank"
>
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 180 180"
height="28"
aria-hidden="true"
>
<g fill="none" fill-rule="nonzero">
<path
fill="currentColor"
d="M111.223 75.983 91.937 66.34a3.214 3.214 0 0 0-2.877 0l-19.286 9.643A3.214 3.214 0 0 0 68 78.859v22.5c0 1.219.688 2.333 1.778 2.877l19.285 9.643a3.214 3.214 0 0 0 2.874 0l19.286-9.643A3.214 3.214 0 0 0 113 101.36v-22.5a3.214 3.214 0 0 0-1.777-2.876ZM90.5 72.81l12.099 6.05L90.5 84.908l-12.099-6.05L90.5 72.81ZM74.429 84.06l12.857 6.429v15.313l-12.857-6.429V84.06Zm19.285 21.742V90.489l12.857-6.429v15.313l-12.857 6.429Z"
></path>
<path
fill="var(--bs-secondary-color)"
d="M180 90c0-19.343-18-36.203-44.692-45.307C126.203 18 109.343 0 90 0 61.005 0 37.5 40.297 37.5 90c-.01 9.195.825 18.371 2.498 27.412C24.323 110.063 15 99.75 15 90a23.678 23.678 0 0 1 8.288-16.335A154.035 154.035 0 0 1 27 52.552C10.343 62.079 0 75.33 0 90c0 19.343 18 36.203 44.693 45.308C53.797 162 70.657 180 90 180c14.67 0 27.922-10.343 37.448-27a154.035 154.035 0 0 1-21.113 3.683A23.677 23.677 0 0 1 90 165c-9.75 0-20.063-9.322-27.413-24.998A149.798 149.798 0 0 0 90 142.5c49.702 0 90-23.505 90-52.5ZM90 15c9.75 0 20.063 9.323 27.412 24.998A149.798 149.798 0 0 0 90 37.5c-3.06 0-6.08.09-9.06.27a64.89 64.89 0 0 0-7.77 15.75A140.557 140.557 0 0 1 90 52.5a131.4 131.4 0 0 1 33.345 4.155A131.4 131.4 0 0 1 127.5 90c0 5.625-.339 11.245-1.013 16.83a64.89 64.89 0 0 0 15.75-7.77c.175-2.985.263-6.005.263-9.06.01-9.195-.825-18.371-2.498-27.413C155.678 69.938 165 80.25 165 90c0 17.737-30.795 37.5-75 37.5a131.4 131.4 0 0 1-33.345-4.155A131.4 131.4 0 0 1 52.5 90c0-44.205 19.763-75 37.5-75Z"
></path>
</g>
</svg>
<span class="fw-bold text-body">Boox</span>
</a>
</p>
</footer>
SVELTE
<script lang="ts">
import Boox, { type SearchResult } from 'boox'
import { onMount } from 'svelte'
import debounce from 'debounce'
import { metaphone } from 'metaphone'
import { stemmer } from 'stemmer'
import { removeStopwords } from 'stopword'
import type { Pokemon } from '../types'
import SearchInfo from './SearchInfo.svelte'
import SearchResults from './SearchResults.svelte'
let pokemons: Pokemon[] = []
let totalDocuments: number = 0
let results: SearchResult<Pokemon>[] = []
let query = ''
const boox = new Boox<Pokemon>({
features: ['name', 'caption', 'set_name'],
attributes: ['hp', 'image_url'],
modelOptions: {
tokenizer(input) {
return removeStopwords(Array.from(input.match(/\b\w+\b/g) || []))
},
stemmer: stemmer,
phonetic: metaphone
}
})
onMount(async () => {
pokemons = await fetchData()
await boox.addDocuments(pokemons)
totalDocuments = Object.keys(boox.currentState.documents).length
})
const performSearch = debounce(async () => {
results = await boox.search(query)
}, 300)
async function fetchData(): Promise<Pokemon[]> {
try {
const response = await fetch(
'https://stilearning.com/boox/demo/datasets/pokemon-100r.json',
{ cache: 'default' }
)
return await response.json()
} catch (error) {
throw error
}
}
</script>
<form id="search-form" class="p-3" role="search">
<div class="form-group">
<input
name="search"
type="search"
class="form-control form-control-lg"
bind:value={query}
on:input={performSearch}
placeholder="Search (e.g. pikachu, water pwer)"
aria-label="Search pokemons"
/>
<div class="form-text">
<strong>Note:</strong>
the
<code class="bg-body-secondary">pwer</code>
demonstrates a typing error for
<code class="bg-body-secondary">power</code>.
</div>
</div>
</form>
<SearchInfo bind:results bind:totalDocuments />
<SearchResults bind:results />
SVELTE
<script lang="ts">
import type { SearchResult } from 'boox'
import type { Pokemon } from '../types'
export let results: SearchResult<Pokemon>[] = []
export let totalDocuments: number = 0
</script>
{#if results.length}
<p id="results-length" class="px-3 text-muted">
Found {results.length} of {totalDocuments} pokemons
</p>
{/if}
SVELTE
<script lang="ts">
import type { SearchResult } from 'boox'
import type { Pokemon } from '../types'
export let result: SearchResult<Pokemon>
</script>
<a
class="list-group-item list-group-item-action d-flex gap-3 py-3"
href={result.attributes.image_url}
target="_blank"
>
<img
loading='lazy'
src={result.attributes.image_url}
alt={result.attributes.name}
width="32"
height="32"
class="rounded-circle flex-shrink-0"
/>
<div class="d-flex gap-2 w-100 justify-content-between">
<div>
<h6 class="mb-1">
{@html result.context('name').text} – {@html result.context('set_name')
.text}
</h6>
<p class="mb-0 opacity-75">
{@html result.context('caption', 80).text}...
</p>
</div>
<small class="opacity-50 text-nowrap">HP {result.attributes.hp}</small>
</div>
</a>
SVELTE
<script lang="ts">
import type { SearchResult } from 'boox'
import type { Pokemon } from '../types'
import SearchResultComponents from './SearchResult.svelte'
export let results: SearchResult<Pokemon>[] = []
</script>
{#if results.length}
<div id="results" class="list-group list-group-flush overflow-auto rounded-3">
{#each results as result}
<SearchResultComponents {result} />
{/each}
</div>
{/if}
SVELTE
Running the app
1. Start the development server:
npm run dev
BASH
Open http://localhost:5173 in your browser to see the search app in action.
2. Build for Production:
npm run build
BASH
Heads up
Remember to consult the documentation for Boox and other libraries for more advanced usage and configuration options.