aboutsummaryrefslogtreecommitdiff
path: root/assets
diff options
context:
space:
mode:
authorAlex Shpak <alex-shpak@users.noreply.github.com>2020-07-06 22:17:36 +0200
committerAlex Shpak <alex-shpak@users.noreply.github.com>2020-07-06 22:17:36 +0200
commitf416a71bf8bb77e7af9ade9eea7fe3b662962290 (patch)
treeb370efef667fb007895bb5c21614bb61dac46b51 /assets
parent05c6336d224b903880f0c0161263bf849fc41104 (diff)
#236, #171 Add section to search results
Diffstat (limited to 'assets')
-rw-r--r--assets/_main.scss6
-rw-r--r--assets/search-data.js7
-rw-r--r--assets/search.js21
3 files changed, 27 insertions, 7 deletions
diff --git a/assets/_main.scss b/assets/_main.scss
index 1080a0b..c32d945 100644
--- a/assets/_main.scss
+++ b/assets/_main.scss
@@ -194,6 +194,12 @@ ul.pagination {
@include spin(1s);
}
+
+ #book-search-results {
+ small {
+ opacity: .5;
+ }
+ }
}
.book-toc {
diff --git a/assets/search-data.js b/assets/search-data.js
index 859e6ad..f324281 100644
--- a/assets/search-data.js
+++ b/assets/search-data.js
@@ -1,6 +1,6 @@
'use strict';
-(function() {
+(function () {
const indexCfg = {{ with i18n "bookSearchConfig" }}
{{ . }};
{{ else }}
@@ -10,18 +10,21 @@
indexCfg.doc = {
id: 'id',
field: ['title', 'content'],
- store: ['title', 'href'],
+ store: ['title', 'href', 'section'],
};
const index = FlexSearch.create('balance', indexCfg);
window.bookSearchIndex = index;
{{ range $index, $page := where .Site.Pages "Kind" "in" (slice "page" "section") }}
+ {{ if $page.Content }}
index.add({
'id': {{ $index }},
'href': '{{ $page.RelPermalink }}',
'title': {{ (partial "docs/title" $page) | jsonify }},
+ 'section': {{ (partial "docs/title" $page.Parent) | jsonify }},
'content': {{ $page.Plain | jsonify }}
});
{{- end -}}
+ {{- end -}}
})();
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('<li><a href></a><small></small></li>');
+ 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;
+ }
})();