From f416a71bf8bb77e7af9ade9eea7fe3b662962290 Mon Sep 17 00:00:00 2001 From: Alex Shpak Date: Mon, 6 Jul 2020 22:17:36 +0200 Subject: #236, #171 Add section to search results --- assets/search.js | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'assets/search.js') diff --git a/assets/search.js b/assets/search.js index 1feb701..3635a1f 100644 --- a/assets/search.js +++ b/assets/search.js @@ -3,7 +3,7 @@ {{ $searchDataFile := printf "%s.search-data.js" .Language.Lang }} {{ $searchData := resources.Get "search-data.js" | resources.ExecuteAsTemplate $searchDataFile . | resources.Minify | resources.Fingerprint }} -(function() { +(function () { const input = document.querySelector('#book-search-input'); const results = document.querySelector('#book-search-results'); @@ -47,7 +47,7 @@ input.required = true; loadScript('{{ "flexsearch.min.js" | relURL }}'); - loadScript('{{ $searchData.RelPermalink }}', function() { + loadScript('{{ $searchData.RelPermalink }}', function () { input.required = false; search(); }); @@ -63,12 +63,13 @@ } const searchHits = window.bookSearchIndex.search(input.value, 10); - searchHits.forEach(function(page) { - const li = document.createElement('li'), - a = li.appendChild(document.createElement('a')); + searchHits.forEach(function (page) { + const li = element('
  • '); + const a = li.querySelector('a'), small = li.querySelector('small'); a.href = page.href; a.textContent = page.title; + small.textContent = page.section; results.appendChild(li); }); @@ -87,4 +88,14 @@ document.head.appendChild(script); } + + /** + * @param {String} content + * @returns {Node} + */ + function element(content) { + const div = document.createElement('div'); + div.innerHTML = content; + return div.firstChild; + } })(); -- cgit v1.2.3