|
| 1 | +/*! |
| 2 | + * FlexSearch for Bootstrap based Thulite sites |
| 3 | + * Copyright 2021-2024 Thulite |
| 4 | + * Licensed under the MIT License |
| 5 | + * Based on https://github.com/frjo/hugo-theme-zen/blob/main/assets/js/search.js |
| 6 | + */ |
| 7 | + |
| 8 | +/* eslint-disable no-undef, guard-for-in */ |
| 9 | + |
| 10 | +/** |
| 11 | + * @file |
| 12 | + * A JavaScript file for flexsearch. |
| 13 | + */ |
| 14 | + |
| 15 | +// import * as FlexSearch from 'flexsearch'; |
| 16 | +import Index from 'flexsearch'; |
| 17 | + |
| 18 | +(function () { |
| 19 | + |
| 20 | + 'use strict'; |
| 21 | + |
| 22 | + // const index = new FlexSearch.Document({ |
| 23 | + const index = new Index.Document({ |
| 24 | + tokenize: 'forward', |
| 25 | + document: { |
| 26 | + id: 'id', |
| 27 | + index: [ |
| 28 | + { |
| 29 | + field: 'title' |
| 30 | + }, |
| 31 | + { |
| 32 | + field: 'tags' |
| 33 | + }, |
| 34 | + { |
| 35 | + field: {{ if site.Params.doks.indexSummary }}'summary'{{ else }}'content'{{ end }} |
| 36 | + }, |
| 37 | + { |
| 38 | + field: 'date', |
| 39 | + tokenize: 'strict', |
| 40 | + encode: false |
| 41 | + } |
| 42 | + ], |
| 43 | + store: ['title','summary','date','permalink'] |
| 44 | + } |
| 45 | + }); |
| 46 | + |
| 47 | + function showResults(items, order) { |
| 48 | + const template = document.querySelector('template').content; |
| 49 | + const fragment = document.createDocumentFragment(); |
| 50 | + |
| 51 | + const results = document.querySelector('.search-results'); |
| 52 | + results.textContent = ''; |
| 53 | + |
| 54 | + const itemsLength = Object.keys(items).length; |
| 55 | + |
| 56 | + // Show/hide "No recent searches" and "No search results" messages |
| 57 | + if ((itemsLength === 0) && (query.value === '')) { |
| 58 | + // Hide "No search results" message |
| 59 | + document.querySelector('.search-no-results').classList.add('d-none'); |
| 60 | + // Show "No recent searches" message |
| 61 | + document.querySelector('.search-no-recent').classList.remove('d-none'); |
| 62 | + } else if ((itemsLength === 0) && (query.value !== '')) { |
| 63 | + // Hide "No recent searches" message |
| 64 | + document.querySelector('.search-no-recent').classList.add('d-none'); |
| 65 | + // Show "No search results" message |
| 66 | + const queryNoResults = document.querySelector('.query-no-results'); |
| 67 | + queryNoResults.innerText = query.value; |
| 68 | + document.querySelector('.search-no-results').classList.remove('d-none'); |
| 69 | + } else { |
| 70 | + // Hide both "No recent searches" and "No search results" messages |
| 71 | + document.querySelector('.search-no-recent').classList.add('d-none'); |
| 72 | + document.querySelector('.search-no-results').classList.add('d-none'); |
| 73 | + } |
| 74 | + |
| 75 | + order.forEach((id) => { |
| 76 | + const item = items[id]; |
| 77 | + const result = template.cloneNode(true); |
| 78 | + const a = result.querySelector('a'); |
| 79 | + const time = result.querySelector('time'); |
| 80 | + const content = result.querySelector('.content'); |
| 81 | + a.innerHTML = item.title; |
| 82 | + a.href = item.permalink; |
| 83 | + time.innerText = ""; |
| 84 | + content.innerHTML = item.summary; |
| 85 | + fragment.appendChild(result); |
| 86 | + }); |
| 87 | + |
| 88 | + results.appendChild(fragment); |
| 89 | + } |
| 90 | + |
| 91 | + function doSearch() { |
| 92 | + const query = document.querySelector('.search-text').value.trim(); |
| 93 | + const limit = {{ .searchLimit }}; |
| 94 | + const results = index.search({ |
| 95 | + query: query, |
| 96 | + enrich: true, |
| 97 | + limit: limit, |
| 98 | + }); |
| 99 | + const items = {}; |
| 100 | + const order = []; |
| 101 | + |
| 102 | + results.forEach(function (result) { |
| 103 | + |
| 104 | + result.result.forEach(function (r) { |
| 105 | + if(!order.includes(r.id)) { |
| 106 | + order.push(r.id); |
| 107 | + } |
| 108 | + |
| 109 | + items[r.id] = r.doc; |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + showResults(items, order); |
| 114 | + } |
| 115 | + |
| 116 | + function enableUI() { |
| 117 | + const searchform = document.querySelector('.search-form'); |
| 118 | + searchform.addEventListener('submit', function (e) { |
| 119 | + e.preventDefault(); |
| 120 | + doSearch(); |
| 121 | + }); |
| 122 | + searchform.addEventListener('input', function () { |
| 123 | + doSearch(); |
| 124 | + }); |
| 125 | + document.querySelector('.search-loading').classList.add('d-none'); |
| 126 | + document.querySelector('.search-input').classList.remove('d-none'); |
| 127 | + document.querySelector('.search-text').focus(); |
| 128 | + } |
| 129 | + |
| 130 | + function buildIndex() { |
| 131 | + document.querySelector('.search-loading').classList.remove('d-none'); |
| 132 | + fetch("{{ site.LanguagePrefix }}/search-index.json") |
| 133 | + .then(function (response) { |
| 134 | + return response.json(); |
| 135 | + }) |
| 136 | + .then(function (data) { |
| 137 | + data.forEach(function (item) { |
| 138 | + index.add(item); |
| 139 | + }); |
| 140 | + }); |
| 141 | + } |
| 142 | + |
| 143 | + buildIndex(); |
| 144 | + enableUI(); |
| 145 | +})(); |
0 commit comments