Skip to main content
doc0blade47/fumadocsdocumentation home
Sign in

Search Query Regex Compilation

Last updated Oct 12, 2026
Relevant source files

The following files were used as context for generating this wiki page:

Overview

Introduction

The execution flow from createFromSource down to buildRegexFromQuery represents the core lifecycle of a search request in Fumadocs, transitioning from document loader outputs to indexed search servers, execution against an Orama database instance, and finally result post-processing with dynamic search term highlighting.

Sources: packages/core/src/search/orama/create-server.ts:273-324, packages/core/src/search/orama/search/simple.ts:5-27, packages/core/src/search/index.ts:36-43

Sources: packages/core/src/search/orama/create-server.ts:187-324, packages/core/src/search/orama/search/simple.ts:5-19, packages/core/src/search/index.ts:36-48

Step 1: createFromSource

createFromSource initiates the search server configuration using a document loader or an asynchronous loader function. It sets up a WeakMap-based cache linked to specific loader outputs to optimize server initialization and handle re-indexing when loader instances change.

Sources: packages/core/src/search/orama/create-server.ts:273-279

Step 2: getCurrentServer

getCurrentServer resolves the current loader output—evaluating it if passed as a function—and checks the internal cache for an existing server promise. If a cached server instance does not exist for the current loader reference, it triggers server initialization and stores the resulting promise in the cache.

Sources: packages/core/src/search/orama/create-server.ts:306-314

Step 3: initServer

initServer processes all pages retrieved from the loader. It maps each page through buildIndex to create searchable index records, generates missing breadcrumbs automatically, assigns locale metadata, and inspects whether internationalization (_i18n) is configured.

Sources: packages/core/src/search/orama/create-server.ts:280-304

Step 4: createI18nSearchAPI

When internationalization is enabled (loader._i18n), createI18nSearchAPI wraps search operations across multiple language contexts. It initiates background server setup per language via initSearchServers and routes queries to the appropriate language handler based on user options or defaults.

Sources: packages/core/src/search/orama/create-server.ts:178-259

Step 5: initSearchServers

initSearchServers iterates through all configured languages in the internationalization settings, filters document indexes matching the current locale, maps locale codes to compatible stemmers or tokenizers, and instantiates language-specific search servers.

Sources: packages/core/src/search/orama/create-server.ts:190-237

Step 6: initSimpleSearch

initSimpleSearch constructs a simple search server instance over an Orama database (createDBSimple). It exposes export capabilities for static bundle generation and a search method that queries the underlying database instance using searchSimple.

Sources: packages/core/src/search/orama/create-server.ts:78-98

Step 7: searchSimple

searchSimple executes the search query against the Orama database with tolerance and term boosting configured. Upon receiving search hits, it invokes createContentHighlighter to generate highlighted content strings for presentation.

Sources: packages/core/src/search/orama/search/simple.ts:5-27

Step 8: createContentHighlighter

createContentHighlighter accepts a query string or precompiled regular expression. If given a string, it hands the query to buildRegexFromQuery to generate a case-insensitive matching pattern, returning helper methods for text and Markdown highlighting.

Sources: packages/core/src/search/index.ts:47-49

Step 9: buildRegexFromQuery

buildRegexFromQuery cleans and splits the query string into distinct search terms, escapes special regular expression characters via escapeRegExp, deduplicates terms, and builds a combined case-insensitive RegExp matching group for highlighting matches. If the query trims down to an empty string, it returns null.

Sources: packages/core/src/search/index.ts:36-43

Key Observations

  • Cross-Module Boundaries: The flow bridges higher-level content source loading (@/source/loader) with Orama indexing infrastructure (@orama/orama), routing through server endpoint wrappers and text processing utilities in @/search.
  • Caching Strategy: Server instances are cached using a WeakMap mapped to loader output references. This ensures memory efficiency while preventing redundant re-indexing unless a fresh loader instance is produced.
  • Robust Query Normalization: Query strings undergo trimming, whitespace splitting, deduplication, and regex escaping inside buildRegexFromQuery to safely handle arbitrary user inputs without crashing parser or highlighter steps.
Was this page helpful?