File tree Expand file tree Collapse file tree 3 files changed +27
-24
lines changed Expand file tree Collapse file tree 3 files changed +27
-24
lines changed Original file line number Diff line number Diff line change 11const fs = require ( 'fs' ) ;
22const lunr = require ( 'lunr' ) ;
33
4+ /**
5+ * This script is used to generate a lunr index from the offline-search-index.json file.
6+ * Hugo must be built before running this script, as jt requires the offline-search-index.json file to have been generated.
7+ *
8+ * The script will output a lunr-index.json file in the content/static directory and the docs directory.
9+ */
10+
411const args = process . argv . slice ( 2 ) ;
512
613// Arguments should only be provided from a pipeline build.
Original file line number Diff line number Diff line change 1- // Adapted from code by Matt Walters https://www.mattwalters.net/posts/hugo-and-lunr/
1+
2+ /**
3+ * This script is used for the offline search feature.
4+ * It calls the worker.js to generate the index and search the index.
5+ *
6+ * Adapted from code by Matt Walters https://www.mattwalters.net/posts/hugo-and-lunr/
7+ */
28
39( function ( $ ) {
410 'use strict' ;
511
612 $ ( document ) . ready ( function ( ) {
713 const $searchInput = $ ( '.td-search-input' ) ;
814
9- //
10- // Options for popover
11- //
12-
1315 $searchInput . data ( 'html' , true ) ;
1416 $searchInput . data ( 'placement' , 'bottom' ) ;
1517 $searchInput . data (
1618 'template' ,
1719 '<div class="popover offline-search-result" role="tooltip"><div class="arrow"></div><h3 class="popover-header"></h3><div class="popover-body"></div></div>'
1820 ) ;
1921
20- //
21- // Lunr
22- //
23-
24- let idx = null ; // Lunr index
25- const resultDetails = new Map ( ) ; // Will hold the data for the search results (titles and summaries)
2622 let worker = null ;
2723
28-
2924 if ( window . Worker ) {
3025 worker = new Worker ( '/js/worker.js' ) ;
3126 const url = '/lunr-index.json' ;
128123 }
129124
130125 const render = ( $targetSearchInput ) => {
131- // Dispose the previous result
132126 $targetSearchInput . popover ( 'dispose' ) ;
133127 currentTarget = $targetSearchInput ;
134128
135- //
136- // Search
137- //
138-
139-
140129 const searchQuery = $targetSearchInput . val ( ) ;
141130 if ( searchQuery === '' ) {
142131 return ;
150139 } ) ;
151140 } ;
152141
153- //
154- // Register handler
155- //
156-
142+ // Renders the search results when the input changes.
157143 $searchInput . on ( 'change' , ( event ) => {
158144 render ( $ ( event . target ) ) ;
159145
Original file line number Diff line number Diff line change 1+ /**
2+ * This worker is responsible for loading the lunr index, searching the index and returning the results.
3+ *
4+ * It has 2 action types, 'init' and 'search'.
5+ *
6+ * 'init' action is used to load the lunr index and the raw index data.
7+ * 'search' action is used to search the lunr index with the provided query.
8+ */
9+
110if ( typeof importScripts === 'function' ) {
211 importScripts ( 'https://unpkg.com/[email protected] /lunr.min.js' ) ; 312}
413
514let idx ;
615const versionRegex = new RegExp ( "^\/docs\/([0-9\.]*|latest)\/" ) ;
7- const resultDetails = new Map ( ) ; // Will hold the data for the search results (titles and summaries)
16+ const resultDetails = new Map ( ) ;
817let indexReadyPromise ;
918
10- // Initialize the index
1119self . onmessage = async function ( event ) {
1220 if ( event . data . type === 'init' ) {
21+ // Initialize the lunr index and the raw index data.
1322 indexReadyPromise = new Promise ( async ( resolve , reject ) => {
1423 try {
1524 const rawIndex = await fetch ( event . data . rawIndexUrl ) ;
@@ -33,6 +42,7 @@ self.onmessage = async function (event) {
3342 }
3443 } ) ;
3544 } else if ( event . data . type === 'search' ) {
45+ // Search the lunr index with the provided query.
3646 try {
3747 await indexReadyPromise ;
3848 const regexResults = versionRegex . exec ( event . data . currentPath ) ;
You can’t perform that action at this time.
0 commit comments