Search

Search is provided by the search plugin, which is enabled by default. Docspire indexes your content at build time and searches it in the browser: no service to sign up for, no API keys, no network requests beyond your own site.

A button in the header opens the search palette, as does ⌘K (Ctrl K on Windows and Linux). Results are grouped by page and link straight to the relevant heading. ↑ and ↓ move through them, Enter opens the highlighted one, and Esc closes the palette.

The index is written to assets/search-index.json and is only fetched the first time you search, so it costs nothing on pages where it isn’t used. It holds only what can’t be worked out later: a heading’s anchor is stored only when it isn’t the one its text implies, and how common each word is gets counted as you search rather than shipped.

What gets indexed

Each page is split into sections, one per heading, so that results can link to the heading they matched. Front matter titles and descriptions are indexed along with the content.

Code blocks are not indexed by default: they tend to be long and to match everything. Set includeCode: true to index them too.

To keep a page out of the index, set search: false in its front matter:

---
title: Internal notes
search: false
---

The page is still built and still reachable, it just won’t come up in search.

Options

Options are configured under search in your Docspire config file:

export default {
	title: "My docs",
	search: {
		placeholder: "Search the docs",
		includeCode: true,
	},
};
Option Default Description
provider "local" Which search backend to use. See Providers.
label "Search" Text of the button in the header.
placeholder "Search docs" Placeholder of the search field.
shortcut "k" Key that opens the palette, together with ⌘ or Ctrl.
minLength 2 Don’t search until the query is at least this long.
maxResults 20 Maximum number of results shown.
sectionsPerPage 2 At most this many sections of the same page in the results.
minLevel 2 Heading levels that start a new indexed section…
maxLevel 6 …and the deepest level that still does.
includeCode false Whether to index the text of code blocks.
index URL of the index, if you host it elsewhere.

Set search: false to disable search entirely: no button, no client-side code, and no index in your output.

Ranking

Results are scored per section. Every word of the query has to appear somewhere in that section — its text, its heading, or its page title — and the whole query appearing in one piece scores higher still, so an exact phrase floats to the top without needing quotes.

Beyond that, three things shape the order:

Matching is on substrings, so part of a word finds the whole one as you type: cust finds “Customization”.

A word that appears nowhere on the site is looked up again among the words that do, within a small edit distance — one edit, or two for words of eight characters or more. That covers a plural where the page is written in the singular, an inflected form, and a typo, without any per-language rules: sidbar finds “Sidebar” the same way sidebars does. These matches always rank below a word that is really there, and the further away, the lower.

Words shorter than four characters aren’t stretched this way — too many other words lie within one edit of them to be a useful guess.

Questions work too. When no section holds every word — “what is a sidebar”, where nothing on the site says “what” — the search keeps the word that tells sections apart best and lets the rest merely count towards the score, so a section carrying more of the question still comes first. Which words those are comes from your own content, not from a list of filler words in some particular language.

Markup and styling

The palette is ordinary HTML, written into the page at build time. The element’s shadow DOM holds nothing but a <slot>, so every part of the palette is in light DOM and there is nothing to pierce — docs-search/element.js only adds behavior to the markup it finds.

The button in the header is .search-trigger; the palette is a <dialog class="search-dialog"> containing .search-panel, .search-field, .search-input, .search-close, .search-results and .search-status. Style any of it with ordinary selectors:

.search-panel {
	border-radius: 0;
}

Each result is cloned from a <template class="search-result-template"> in the same markup, so overriding components/docs-search/template.njk in your own components directory changes what a result looks like without touching the script. It fills in .search-result-title, .search-result-page and .search-result-text when they are present, and skips whatever isn’t.

Opening the palette morphs the button into it: the two share a view-transition-name, so the browser tweens one box into the other. Readers who ask for reduced motion get the dialog with no animation.

Putting search in a page

Writing <docs-search></docs-search> in a page expands it into that same markup, for a dedicated search page or a landing page that leads with search:

<docs-search></docs-search>

A page can hold more than one — the header’s and its own, say. ⌘K opens whichever is nearest whatever has focus, so the shortcut lands where the reader is looking.

Providers

The local provider described above is the default, and is all most sites need. A provider is just two halves: one that runs at build time and produces whatever the search needs, and one that runs in the browser and answers queries:

export default {
	id: "my-provider",

	// Module in assets/components/, exporting
	// createSearcher(options) → { search(query) → Result[] }
	client: "my-searcher.js",

	build (config, options) {
		// Register 11ty hooks, emit an index…
	},
};

<docs-search> handles the UI and keyboard interaction either way, so a provider only deals with searching. Pass one as provider to use it.