diff --git a/.github/workflows/checklink.yml b/.github/workflows/checklink.yml index d9bcc164f..54001eb1d 100644 --- a/.github/workflows/checklink.yml +++ b/.github/workflows/checklink.yml @@ -37,6 +37,9 @@ jobs: HUGO_ENVIRONMENT: production HUGO_ENV: production run: hugo --environment GitHubPages -d $GITHUB_WORKSPACE/dist --buildFuture + - name: Generate Search index + run: | + node ./assets/js/generate-lunr-index.js $GITHUB_WORKSPACE/dist - name: Test HTML uses: wjdp/htmltest-action@master with: diff --git a/.github/workflows/hugo.yml b/.github/workflows/hugo.yml index b62b3cdea..b8e4d2a34 100644 --- a/.github/workflows/hugo.yml +++ b/.github/workflows/hugo.yml @@ -63,6 +63,9 @@ jobs: HUGO_ENV: production run: | hugo --environment GitHubPages + - name: Generate Search index + run: | + node ./assets/js/generate-lunr-index.js - name: Upload artifact uses: actions/upload-pages-artifact@v3 with: diff --git a/.gitignore b/.gitignore index cb8937b14..4c46e5463 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,5 @@ content/en/docs/latest/ content/static/latest/ content/static/**/*.dtmp content/static/**/*.bkp -content/static/**/*.crswap \ No newline at end of file +content/static/**/*.crswap +content/static/lunr-index.json \ No newline at end of file diff --git a/assets/js/generate-lunr-index.js b/assets/js/generate-lunr-index.js new file mode 100644 index 000000000..51519fb9e --- /dev/null +++ b/assets/js/generate-lunr-index.js @@ -0,0 +1,54 @@ +const fs = require('fs'); +const lunr = require('lunr'); + +/** + * This script is used to generate a lunr index from the offline-search-index.json file. + * Hugo must be built before running this script, as jt requires the offline-search-index.json file to have been generated. + * + * The script will output a lunr-index.json file in the content/static directory and the docs directory. + */ + +const args = process.argv.slice(2); + +// Arguments should only be provided from a pipeline build. +const isFromPipeline = args[0] !== undefined; + +const source = isFromPipeline + ? args[0] + : "./docs"; + +const destination = isFromPipeline + ? `${args[0]}` + : "./docs"; + +const data = JSON.parse(fs.readFileSync(`${source}/offline-search-index.json`)); + +const idx = lunr(function () { + this.ref('ref'); + this.field('title', { boost: 5 }); + this.field('categories', { boost: 3 }); + this.field('tags', { boost: 3 }); + this.field('description', { boost: 2 }); + this.field('body'); + + data.forEach((doc) => { + if (doc + && doc.ref !== undefined + && !doc.ref.includes('/_shared/') + ) { + this.add(doc); + } + }); +}); + +if (!isFromPipeline) { + fs.writeFileSync(`./content/static/lunr-index.json`, JSON.stringify(idx)); +} + +fs.writeFileSync(`${destination}/lunr-index.json`, JSON.stringify(idx)); + +// check if file got created +if (!fs.existsSync(`${destination}/lunr-index.json`)) { + console.error('Failed to create lunr index, hugo must be build using `hugo` command before running this script.'); + process.exit(1); +} \ No newline at end of file diff --git a/assets/js/offline-search.js b/assets/js/offline-search.js index 6f99da65a..7151cf686 100644 --- a/assets/js/offline-search.js +++ b/assets/js/offline-search.js @@ -1,4 +1,10 @@ -// Adapted from code by Matt Walters https://www.mattwalters.net/posts/hugo-and-lunr/ + +/** + * This script is used for the offline search feature. + * It calls the worker.js to generate the index and search the index. + * + * Adapted from code by Matt Walters https://www.mattwalters.net/posts/hugo-and-lunr/ + */ (function ($) { 'use strict'; @@ -6,10 +12,6 @@ $(document).ready(function () { const $searchInput = $('.td-search-input'); - // - // Options for popover - // - $searchInput.data('html', true); $searchInput.data('placement', 'bottom'); $searchInput.data( @@ -17,192 +19,137 @@ '
').text(`No results found for query "${event.data.query}"`) + ); + } else { + docs.forEach((doc, key) => { + if (doc === undefined) { + return; } - if (docToAdd - && docToAdd.ref !== undefined - && !docToAdd.ref.includes('/_shared/') - ) { - this.add(doc); + const href = + $searchInput.data('offline-search-base-href') + + key.replace(/^\//, ''); - resultDetails.set(doc.ref, { - title: doc.title, - excerpt: doc.excerpt, - }); - } + const $entry = $('