#236, #171 Add section to search results

This commit is contained in:
Alex Shpak 2020-07-06 22:17:36 +02:00
parent 05c6336d22
commit f416a71bf8
5 changed files with 29 additions and 9 deletions

View File

@ -194,6 +194,12 @@ ul.pagination {
@include spin(1s); @include spin(1s);
} }
#book-search-results {
small {
opacity: .5;
}
}
} }
.book-toc { .book-toc {

View File

@ -10,18 +10,21 @@
indexCfg.doc = { indexCfg.doc = {
id: 'id', id: 'id',
field: ['title', 'content'], field: ['title', 'content'],
store: ['title', 'href'], store: ['title', 'href', 'section'],
}; };
const index = FlexSearch.create('balance', indexCfg); const index = FlexSearch.create('balance', indexCfg);
window.bookSearchIndex = index; window.bookSearchIndex = index;
{{ range $index, $page := where .Site.Pages "Kind" "in" (slice "page" "section") }} {{ range $index, $page := where .Site.Pages "Kind" "in" (slice "page" "section") }}
{{ if $page.Content }}
index.add({ index.add({
'id': {{ $index }}, 'id': {{ $index }},
'href': '{{ $page.RelPermalink }}', 'href': '{{ $page.RelPermalink }}',
'title': {{ (partial "docs/title" $page) | jsonify }}, 'title': {{ (partial "docs/title" $page) | jsonify }},
'section': {{ (partial "docs/title" $page.Parent) | jsonify }},
'content': {{ $page.Plain | jsonify }} 'content': {{ $page.Plain | jsonify }}
}); });
{{- end -}} {{- end -}}
{{- end -}}
})(); })();

View File

@ -64,11 +64,12 @@
const searchHits = window.bookSearchIndex.search(input.value, 10); const searchHits = window.bookSearchIndex.search(input.value, 10);
searchHits.forEach(function (page) { searchHits.forEach(function (page) {
const li = document.createElement('li'), const li = element('<li><a href></a><small></small></li>');
a = li.appendChild(document.createElement('a')); const a = li.querySelector('a'), small = li.querySelector('small');
a.href = page.href; a.href = page.href;
a.textContent = page.title; a.textContent = page.title;
small.textContent = page.section;
results.appendChild(li); results.appendChild(li);
}); });
@ -87,4 +88,14 @@
document.head.appendChild(script); document.head.appendChild(script);
} }
/**
* @param {String} content
* @returns {Node}
*/
function element(content) {
const div = document.createElement('div');
div.innerHTML = content;
return div.firstChild;
}
})(); })();

View File

@ -1 +1 @@
{"Target":"book.min.b3215d9d1d18d9051e7fbe8f256ff80e30f41d29073b86348e230d297b771002.css","MediaType":"text/css","Data":{"Integrity":"sha256-syFdnR0Y2QUef76PJW/4DjD0HSkHO4Y0jiMNKXt3EAI="}} {"Target":"book.min.f11f00e4d2659e7749c679f7a29960dab5462bcb609a767e0f2ea963159b9024.css","MediaType":"text/css","Data":{"Integrity":"sha256-8R8A5NJlnndJxnn3oplg2rVGK8tgmnZ+Dy6pYxWbkCQ="}}