|
| 1 | +--- |
| 2 | +title: Search |
| 3 | +description: Learn how to customize the search in your Svecodocs project. |
| 4 | +section: Configuration |
| 5 | +--- |
| 6 | + |
| 7 | +The search functionality provides fast, client-side search across your documentation using FlexSearch for indexing and fuzzy matching. |
| 8 | + |
| 9 | +## How It Works |
| 10 | + |
| 11 | +The search system consists of three main parts: |
| 12 | + |
| 13 | +1. **Build-time indexing** - Documents are processed and indexed during the build |
| 14 | +2. **Static API endpoint** - Search data is served from a static JSON file |
| 15 | +3. **Client-side search** - Fast, interactive search interface |
| 16 | + |
| 17 | +## Build Process |
| 18 | + |
| 19 | +During the build process, the `build-search-data.js` script is used to generate the search index: |
| 20 | + |
| 21 | +```ts |
| 22 | +import { fileURLToPath } from "node:url"; |
| 23 | +import { writeFileSync } from "node:fs"; |
| 24 | +import { resolve } from "node:path"; |
| 25 | +import { docs } from "../.velite/index.js"; |
| 26 | +import { defineSearchContent, cleanMarkdown } from "@svecodocs/kit"; |
| 27 | + |
| 28 | +const __dirname = fileURLToPath(new URL(".", import.meta.url)); |
| 29 | + |
| 30 | +export function buildDocsSearchIndex() { |
| 31 | + return defineSearchContent( |
| 32 | + docs.map((doc) => ({ |
| 33 | + title: doc.title, |
| 34 | + href: `/docs/${doc.slug}`, |
| 35 | + description: doc.description, |
| 36 | + content: cleanMarkdown(doc.raw), |
| 37 | + category: doc.section, |
| 38 | + })) |
| 39 | + ); |
| 40 | +} |
| 41 | + |
| 42 | +writeFileSync( |
| 43 | + resolve(__dirname, "../src/routes/api/search.json/search.json"), |
| 44 | + JSON.stringify(buildDocsSearchIndex()), |
| 45 | + { flag: "w" } |
| 46 | +); |
| 47 | +``` |
| 48 | + |
| 49 | +This script: |
| 50 | + |
| 51 | +- Imports processed documentation data from Velite |
| 52 | +- Maps each document to a search entry with `title`, `href`, `description`, `content`, and optional `category` |
| 53 | +- Uses `cleanMarkdown()` to remove markdown syntax from content for better search |
| 54 | +- Writes the search index to a static JSON file |
| 55 | + |
| 56 | +## API Endpoint |
| 57 | + |
| 58 | +The search data is served through a static API route: |
| 59 | + |
| 60 | +```ts |
| 61 | +import type { RequestHandler } from "@sveltejs/kit"; |
| 62 | +import search from "./search.json" assert { type: "json" }; |
| 63 | + |
| 64 | +export const prerender = true; |
| 65 | + |
| 66 | +export const GET: RequestHandler = () => { |
| 67 | + return Response.json(search); |
| 68 | +}; |
| 69 | +``` |
| 70 | + |
| 71 | +This route is prerendered for optimal performance and serves the search index as JSON. |
| 72 | + |
| 73 | +## Client-Side Search |
| 74 | + |
| 75 | +The search interface is implemented as a reusable Svelte component: |
| 76 | + |
| 77 | +```svelte |
| 78 | +<script lang="ts"> |
| 79 | + import { onMount } from "svelte"; |
| 80 | + import { type SearchResult, createContentIndex, searchContentIndex } from "./search-utils.js"; |
| 81 | +
|
| 82 | + let searchState = $state<"loading" | "ready">("loading"); |
| 83 | + let searchQuery = $state(""); |
| 84 | +
|
| 85 | + onMount(async () => { |
| 86 | + const content = await fetch("/api/search.json").then((res) => res.json()); |
| 87 | + if (!content) return; |
| 88 | + createContentIndex(content); |
| 89 | + searchState = "ready"; |
| 90 | + }); |
| 91 | +</script> |
| 92 | +``` |
| 93 | + |
| 94 | +The component: |
| 95 | + |
| 96 | +- Fetches search data on mount |
| 97 | +- Creates FlexSearch indexes for fast querying |
| 98 | +- Provides a dialog-based search interface |
| 99 | +- Supports keyboard shortcut (Cmd/Ctrl + K) |
| 100 | + |
| 101 | +## Search Result Structure |
| 102 | + |
| 103 | +Each search result contains: |
| 104 | + |
| 105 | +```ts |
| 106 | +type SearchContent = { |
| 107 | + title: string; |
| 108 | + content: string; |
| 109 | + description: string; |
| 110 | + href: string; |
| 111 | + category?: string; |
| 112 | +}; |
| 113 | + |
| 114 | +type SearchResult = SearchContent & { |
| 115 | + snippet?: string; // Content snippet with highlights |
| 116 | + highlights?: string[]; // Array of highlighted terms |
| 117 | + category?: string; // Document category/section |
| 118 | +}; |
| 119 | +``` |
| 120 | + |
| 121 | +## Search Algorithm |
| 122 | + |
| 123 | +The search uses a multi-tiered approach: |
| 124 | + |
| 125 | +1. **Primary search**: FlexSearch indexes for title and content with different weights |
| 126 | + - Title matches: 10 points |
| 127 | + - Content matches: 5 points |
| 128 | +2. **Fallback search**: Fuzzy matching when no exact results found |
| 129 | + - Fuzzy title matches: 8 points |
| 130 | + - Fuzzy content matches: 3 points |
| 131 | + |
| 132 | +Results are ranked by score and limited to 10 items. |
| 133 | + |
| 134 | +## Usage |
| 135 | + |
| 136 | +### Basic Integration |
| 137 | + |
| 138 | +To use the search component in your site: |
| 139 | + |
| 140 | +```svelte |
| 141 | +<script> |
| 142 | + import Search from "@svecodocs/kit/components/search/search.svelte"; |
| 143 | +</script> |
| 144 | +
|
| 145 | +<Search /> |
| 146 | +``` |
0 commit comments