search

package
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 6, 2026 License: Apache-2.0, MIT Imports: 15 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package search builds a static full-text search index from exported HTML pages and ships a small client script that queries it in the browser.

It reads the same data-pagefind-body, data-pagefind-ignore, data-pagefind-meta, data-pagefind-default-meta and data-pagefind-index-attrs attributes as Pagefind, so markup written for Pagefind indexes unchanged. Ranking is BM25 over words and word prefixes, with title words weighted 5x and heading words 2x, and excerpts highlight matches with <mark>.

The index is one JSON file, /_search/index.json. Above ShardSize it splits into per-letter term files and page-text chunks that the client loads on demand. The client, /_search/client.js, uses no eval, inline handlers or inline styles, so it runs under a strict CSP that allows 'self'.

Index

Constants

View Source
const Dir = "/_search/"

Dir is the URL directory the index and the client are served from.

Variables

This section is empty.

Functions

func Client

func Client() []byte

Client returns the browser client, served as /_search/client.js. Load it with <script src="/_search/client.js" defer></script>; it finds the index next to its own URL, so a base path needs no setting.

Types

type File

type File struct {
	Path        string
	ContentType string
	Data        []byte
}

File is one file of the built index, with Path relative to the site root, such as "_search/index.json".

type Index

type Index struct {
	// contains filtered or unexported fields
}

Index is a built search index.

func Build

func Build(pages []Page, opts ...Option) *Index

Build indexes pages. A page that fails to parse, or that has no data-pagefind-body while another page has one, is left out.

func (*Index) Files

func (ix *Index) Files() ([]File, error)

Files returns the client and the index: _search/index.json alone, or, when that would exceed ShardSize, an index.json of page entries plus term files t-<hex code point of the first letter>.json and page-text chunks x-<n>.json.

func (*Index) Len

func (ix *Index) Len() int

Len returns the number of indexed pages.

func (*Index) Search

func (ix *Index) Search(query string, limit int) []Result

Search ranks the pages that match every word of query, best first, as the browser client does. limit <= 0 returns every match.

type Option

type Option func(*config)

Option configures Build.

func Exclude

func Exclude(globs ...string) Option

Exclude leaves out pages whose path matches one of globs, with the same syntax as Include. It wins over Include.

func Include

func Include(globs ...string) Option

Include indexes only pages whose path matches one of globs, such as "/" or "/docs/*". A glob ending in /* matches everything below it; otherwise * stays inside one segment. Default: every page.

func ShardSize

func ShardSize(n int) Option

ShardSize sets the index size in bytes above which the index is split into shards. Default 1 MiB.

type Page

type Page struct {
	// Path is the page's URL path, matched against Include and Exclude.
	Path string
	// URL is the link stored in the index. Default Path. Set it when the
	// site is served under a base path.
	URL  string
	HTML []byte
}

Page is one HTML page to index.

type Result

type Result struct {
	URL   string
	Title string
	Meta  map[string]string
	// Excerpt is HTML: the page text around the best match, escaped, with
	// matching words in <mark>.
	Excerpt string
	Score   float64
}

Result is one search hit.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL