From 45e297e8a28c93d7fae4d96a74163ba8c206d7f1 Mon Sep 17 00:00:00 2001 From: Joe Date: Thu, 1 Aug 2024 11:52:10 -0700 Subject: [PATCH] js: remove unused flexsearch code --- assets/js/flexsearch.js | 147 ---------------------------------------- 1 file changed, 147 deletions(-) delete mode 100644 assets/js/flexsearch.js diff --git a/assets/js/flexsearch.js b/assets/js/flexsearch.js deleted file mode 100644 index 3e23b8b..0000000 --- a/assets/js/flexsearch.js +++ /dev/null @@ -1,147 +0,0 @@ -// Patch Doks' builtin flexsearch implementation so that it resolves the search index correctly when deployed to a -// subdirectory. Original code: https://github.com/gethyas/doks-core/blob/main/assets/js/flexsearch.js. -// -// The only line changed compared to the original code above is L134; see comment there. - -/*! - * FlexSearch for Bootstrap based Hyas sites - * Copyright 2021-2023 Hyas - * Licensed under the MIT License - * Based on https://github.com/frjo/hugo-theme-zen/blob/main/assets/js/search.js - */ - -/* eslint-disable no-undef, guard-for-in */ - -/** - * @file - * A JavaScript file for flexsearch. - */ - -// import * as FlexSearch from 'flexsearch'; -import Index from 'flexsearch'; - -(function () { - - 'use strict'; - - // const index = new FlexSearch.Document({ - const index = new Index.Document({ - tokenize: 'forward', - document: { - id: 'id', - index: [ - { - field: 'title' - }, - { - field: 'tags' - }, - { - field: {{ if site.Params.doks.indexSummary }}'summary'{{ else }}'content'{{ end }} - }, - { - field: 'date', - tokenize: 'strict', - encode: false - } - ], - store: ['title','summary','date','permalink'] - } - }); - - function showResults(items) { - const template = document.querySelector('template').content; - const fragment = document.createDocumentFragment(); - - const results = document.querySelector('.search-results'); - results.textContent = ''; - - const itemsLength = Object.keys(items).length; - - // Show/hide "No recent searches" and "No search results" messages - if ((itemsLength === 0) && (query.value === '')) { - // Hide "No search results" message - document.querySelector('.search-no-results').classList.add('d-none'); - // Show "No recent searches" message - document.querySelector('.search-no-recent').classList.remove('d-none'); - } else if ((itemsLength === 0) && (query.value !== '')) { - // Hide "No recent searches" message - document.querySelector('.search-no-recent').classList.add('d-none'); - // Show "No search results" message - const queryNoResults = document.querySelector('.query-no-results'); - queryNoResults.innerText = query.value; - document.querySelector('.search-no-results').classList.remove('d-none'); - } else { - // Hide both "No recent searches" and "No search results" messages - document.querySelector('.search-no-recent').classList.add('d-none'); - document.querySelector('.search-no-results').classList.add('d-none'); - } - - for (const id in items) { - const item = items[id]; - const result = template.cloneNode(true); - const a = result.querySelector('a'); - const time = result.querySelector('time'); - const content = result.querySelector('.content'); - a.innerHTML = item.title; - a.href = item.permalink; - time.innerText = item.date; - content.innerHTML = item.summary; - fragment.appendChild(result); - } - - results.appendChild(fragment); - } - - function doSearch() { - const query = document.querySelector('.search-text').value.trim(); - const limit = {{ .searchLimit }}; - const results = index.search({ - query: query, - enrich: true, - limit: limit, - }); - const items = {}; - - results.forEach(function (result) { - result.result.forEach(function (r) { - items[r.id] = r.doc; - }); - }); - - showResults(items); - } - - function enableUI() { - const searchform = document.querySelector('.search-form'); - searchform.addEventListener('submit', function (e) { - e.preventDefault(); - doSearch(); - }); - searchform.addEventListener('input', function () { - doSearch(); - }); - document.querySelector('.search-loading').classList.add('d-none'); - document.querySelector('.search-input').classList.remove('d-none'); - document.querySelector('.search-text').focus(); - } - - function buildIndex() { - document.querySelector('.search-loading').classList.remove('d-none'); - - // Originally `fetch("{{ site.LanguagePrefix }}/search-index.json")`; our patch resolves the search index relative - // to the base URL of the deployment. - fetch("{{ relURL `search-index.json` }}") - .then(function (response) { - return response.json(); - }) - .then(function (data) { - data.forEach(function (item) { - index.add(item); - }); - }); - } - - buildIndex(); - enableUI(); -})(); \ No newline at end of file