Skip to content

Commit 6c610b0

Browse files
committed
add summaries to js code
1 parent 5b755e7 commit 6c610b0

File tree

3 files changed

+27
-24
lines changed

3 files changed

+27
-24
lines changed

assets/js/generate-lunr-index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
const fs = require('fs');
22
const 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+
411
const args = process.argv.slice(2);
512

613
// Arguments should only be provided from a pipeline build.

assets/js/offline-search.js

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,26 @@
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';
@@ -128,15 +123,9 @@
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;
@@ -150,10 +139,7 @@
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

assets/js/worker.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
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+
110
if (typeof importScripts === 'function') {
211
importScripts('https://unpkg.com/[email protected]/lunr.min.js');
312
}
413

514
let idx;
615
const 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();
817
let indexReadyPromise;
918

10-
// Initialize the index
1119
self.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);

0 commit comments

Comments
 (0)