(function() { const searchParams = new URLSearchParams(window.location.search); const keywords = searchParams.get('search').trim(); const searchInput = document.getElementById('search-input'); const resultContainer = document.getElementById('search-result'); const emptyResult = document.getElementById('search-result-empty'); if (keywords) { searchInput.value = keywords; search(keywords); } else { return; } const fuseOptions = { includeScore: true, includeMatches: true, minMatchCharLength: searchOptions.minMatchCharLength, threshold: searchOptions.threshold, // refer layouts/search/search.json keys: [ {name: "title", weight: 0.8}, {name: "content", weight: 0.5}, {name: "tags", weight: 0.2}, {name: "categories", weight: 0.2}, ], }; function search(keywords) { fetch('./index.json').then(response => response.json()).then(data => { const fuse = new Fuse(data, fuseOptions); const result = fuse.search(keywords); if (result.length > 0) { showResult(keywords, result); emptyResult.classList.add('hidden'); } else { resultContainer.innerHTML = ''; emptyResult.classList.remove('hidden'); } }); } const resultTemplate = `

{{! it.title }}

{{! it.snippet }}

`; function showResult(keywords, result) { const templateFn = doT.template(resultTemplate); const tagIcon = document.getElementById('tag-icon').innerHTML; resultContainer.innerHTML = ''; for (const [index, entry] of result.entries()) { const item = entry.item; const content = entry.item.content; item.snippet = content.substring(0, searchOptions.summaryInclude * 2) + '…'; item.tagIcon = tagIcon; item.index = index; resultContainer.innerHTML += templateFn(item); } const instance = new Mark(resultContainer); instance.mark(keywords); } })()