Enhanced Sphinx Searchο
This document contains information on the search-phrase.js wrapper that will provide enhanced search functionality that supports both exact phrase matching and multi-term searches with intelligent highlighting.
Featuresο
π Smart Search Typesο
Search Input |
Behavior |
Example |
|---|---|---|
|
Treats as exact phrase |
|
|
Treats as exact phrase |
|
|
Exact phrase search |
|
|
Multiple terms (AND logic) |
|
|
All terms required |
|
|
Clear highlights |
|
β¨ Enhanced Highlightingο
Phrase Highlighting: Complete phrases highlighted as units (not broken into individual words)
Multi-term Highlighting: When using comma-separated search, all matching terms are highlighted
Persistent Highlighting: Highlights remain when navigating between search result pages
Manual Clear: Use
""(empty quotes) to instantly clear all highlightingClean Transitions: Highlights are properly cleared when performing new searches
π― User Experienceο
Intuitive: No need to remember complex search syntax
Flexible: Support for both phrase and multi-term searches
Visual: Clear highlighting shows exactly what matched
Fast: Asynchronous filtering for responsive search experience
Installationο
Requirementsο
Sphinx documentation generator
Any Sphinx theme (tested with Read the Docs theme)
Modern web browser with JavaScript enabled
β οΈ Important: When opening HTML files directly in a browser using
Ctrl+Oor thefile://protocol, JavaScript functionality is limited by browser security policies. The enhanced search features may not work properly. For full functionality, serve the documentation through a web server (even a local one likepython -m http.server).
Setupο
Add the search enhancement script to your Sphinx project:
source/_static/js/search-phrase.js
Update your
conf.pyto include the script:html_js_files = [ 'js/search-phrase.js', # ... your other JS files ]
Build your documentation:
make html
Technical Detailsο
Architectureο
The enhancement uses a wrapper approach that:
Preserves all original Sphinx functionality
Adds phrase search capabilities on top
Avoids conflicts with existing JavaScript
Works with any Sphinx theme that doesnβt override the search system
Files Structureο
source/
βββ _static/
β βββ js/
β βββ search-phrase.js # Enhanced search wrapper
βββ conf.py # Sphinx configuration
βββ ... # Your documentation files
How It Worksο
Search Interception: Wraps the original
Search.queryfunctionQuery Processing: Analyzes input to determine search type (phrase vs. multi-term)
Content Filtering: Fetches and analyzes page content to verify phrase/term presence
Result Display: Shows filtered results using original Sphinx display functions
Highlighting: Applies custom highlighting on destination pages via localStorage
Browser Supportο
Modern browsers with ES6+ support
Async/await functionality required
localStorage support required
Usage Examplesο
Basic Searchesο
openvpn # Find pages about OpenVPN
"public key infrastructure" # Find exact phrase
server configuration # Find "server configuration" phrase
Multi-term Searchesο
openvpn, server # Pages with both "openvpn" AND "server"
key, certificate, config # Pages with all three terms
public, private, key # Pages discussing all key types
Advanced Examplesο
"elliptic curve", RSA # Exact phrase + additional term
configuration, setup # Multiple related terms
"step by step" # Exact instructional phrase
Clear Highlightingο
"" # Clear all highlighting from pages
Workflow example:
Search for
openvpnβ Pages highlighted with βopenvpnβNavigate through results β Highlighting persists
Search for
""β All highlighting clearedContinue reading β Clean pages without highlights
Customizationο
Stylingο
The highlighting uses this CSS class:
.highlighted {
background-color: yellow;
}
You can customize the appearance by adding CSS rules to your theme.
Debug Modeο
The script includes comprehensive debug logging. Check browser console for:
Search query processing
Content filtering results
Highlighting operations
localStorage management
Theme Compatibilityο
This enhancement should work with any Sphinx theme that:
Uses the standard Sphinx search system
Doesnβt completely override the
SearchobjectSupports custom JavaScript files via
html_js_files
Tested themes:
Read the Docs theme β
Basic Sphinx theme β
Most standard themes β
Developmentο
Buildingο
make clean
make html
Testingο
Test various search patterns:
Single words:
testPhrases:
test phraseMulti-terms:
test, phrase, exampleNavigation: Back button, multiple result pages
Highlighting: Check console for debug info
Debuggingο
Enable browser developer console to see detailed logging:
Query processing steps
Content analysis results
Highlighting operations
Storage management
Troubleshootingο
Common Issuesο
Q: Search enhancement not working
A: Check that search-phrase.js is loaded and there are no JavaScript console errors
Q: Search doesnβt work when opening files directly (file:// protocol) A: This is expected due to browser security restrictions. Use one of these solutions:
Serve through a web server:
python -m http.server 8000then visithttp://localhost:8000Use the built-in development server if available
Deploy to a web server for production use
Q: Highlighting not appearing A: Ensure localStorage is enabled and check browser console for any errors
Q: Multi-term search not working
A: Verify youβre using commas to separate terms: term1, term2, term3
Q: Phrase search giving wrong results
A: Check if quotes are needed for exact phrase matching: "exact phrase"
Browser Compatibilityο
Chrome/Firefox/Safari/Edge: Full support when served via HTTP(S)
File:// Protocol: Limited functionality due to security restrictions
Older Browsers: May not support all ES6+ features
Changelogο
v1.0.0ο
Initial implementation with phrase search
Auto-quoting for single words
Multi-term search with commas
Persistent highlighting system
Theme-agnostic wrapper approach
This enhanced search system was developed to provide users with intuitive and powerful search capabilities while maintaining full compatibility with existing Sphinx documentation workflows.