Skip to content

Commit

Permalink
Merge pull request #9 from Al-assad/dev
Browse files Browse the repository at this point in the history
Correct error website link
  • Loading branch information
Al-assad committed Oct 19, 2023
2 parents cfc77cc + 26ec784 commit f1434b0
Show file tree
Hide file tree
Showing 2 changed files with 426 additions and 0 deletions.
132 changes: 132 additions & 0 deletions website/themes/my-doks-theme/assets/js/flexsearch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*!
* 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 * as params from '@params';

(function () {

'use strict';

const index = new FlexSearch.Document({
document: {
id: 'id',
index: ['title', 'tags', 'content', 'date'],
store: ['title', 'summary', 'date', 'permalink'],
},
tokenize: 'forward',
});

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 = params.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 getSearchIndexFile() {
const path = window.location.pathname;
if (path.includes('/zh/')) {
return '/doris-operator/zh/search-index.json'
} else {
return '/doris-operator/search-index.json'
}
}

function buildIndex() {
document.querySelector('.search-loading').classList.remove('d-none');
fetch(getSearchIndexFile())
.then(function (response) {
return response.json();
})
.then(function (data) {
data.forEach(function (item) {
index.add(item);
});
});
}

buildIndex();
enableUI();
})();
Loading

0 comments on commit f1434b0

Please sign in to comment.