Implement search, loading on demand

This commit is contained in:
Alex Shpak 2019-07-16 14:34:02 +02:00
parent dda0a0eab1
commit 1f3addcca5
16 changed files with 119 additions and 69 deletions

View file

@ -153,7 +153,8 @@ BookEditPath = 'edit/master/exampleSite/content'
# - In blog posts # - In blog posts
BookDateFormat = 'Jan 2, 2006' BookDateFormat = 'Jan 2, 2006'
# (Optional, default true) Enables or disables search function with lunr.js # (Optional, default true) Enables search function with lunr.js,
# Index is built on fly, therefore it might slowdown your website.
BookSearch = true BookSearch = true
``` ```

View file

@ -42,6 +42,10 @@
text-align: center; text-align: center;
} }
.hidden {
display: none;
}
@mixin fixed { @mixin fixed {
position: fixed; position: fixed;
top: 0; top: 0;

View file

@ -138,22 +138,28 @@ ul.pagination {
} }
.book-search { .book-search {
#book-search-input {
border: 0; border: 0;
border-bottom: $padding-1 solid $gray-200; border-bottom: $padding-1 solid $gray-200;
outline: none; outline: none;
// padding: $padding-4 $padding-8 $padding-4 $padding-16+$padding-4; padding: $padding-4 $padding-8 $padding-4 $padding-16 + $padding-4;
padding: $padding-4 $padding-8 $padding-4 0;
margin-bottom: $padding-4;
width: 100%;
// background: url("svg/search.svg") left center no-repeat; width: 85%;
// background-size: $padding-16;
background: url("svg/search.svg") left center no-repeat;
background-size: $padding-16;
&:focus { &:focus {
border-bottom-color: $body-font-color; border-bottom-color: $body-font-color;
} }
} }
#book-search-results li:last-child {
margin-bottom: $padding-16 * 2;
}
}
.book-toc { .book-toc {
flex: 0 0 $toc-width; flex: 0 0 $toc-width;
font-size: $font-size-12; font-size: $font-size-12;
@ -196,7 +202,7 @@ ul.pagination {
.book-posts { .book-posts {
min-width: $body-min-width; min-width: $body-min-width;
max-width: $sm-breakpoint; max-width: $body-min-width * 2;
flex-grow: 1; flex-grow: 1;
padding: $padding-16; padding: $padding-16;

24
assets/search-data.js Normal file
View file

@ -0,0 +1,24 @@
(function() {
const pages = [
{{ range $index, $page := .Site.Pages }}
{{- if and $index (gt $index 0) -}},{{- end }}
{
"idx": {{ $index }},
"href": "{{ $page.RelPermalink }}",
"title": "{{ partial "docs/title" $page }}",
"content": {{ $page.Plain | jsonify }}
}
{{- end -}}
];
window.bookSearch = {
pages: pages,
idx: lunr(function() {
this.ref('idx');
this.field('title');
this.field('content');
pages.forEach(this.add, this);
}),
}
})();

View file

@ -1,41 +1,53 @@
addEventListener("load", function() { {{- $searchData := resources.Get "search-data.js" | resources.ExecuteAsTemplate "search-data.js" . | resources.Minify | resources.Fingerprint }}
let input = document.querySelector("#book-search");
let results = document.querySelector("#book-search-results");
Promise.all([ (function() {
loadScript("{{ "lunr.min.js" | relURL }}"), const input = document.querySelector("#book-search-input");
loadScript("{{ "index.json" | relURL }}") const results = document.querySelector("#book-search-results");
]).then(enableLunr); const dummy = document.querySelector("#book-search-dummy");
function enableLunr() { input.addEventListener("focus", init);
results.idx = lunr(function() {
this.ref('href')
this.field('title')
this.field('content')
window.lunrData.forEach(function (page) { function init() {
this.add(page) loadScript("{{ $searchData.RelPermalink }}", function() {
}, this) input.disabled = false;
});
input.addEventListener("keyup", search); input.addEventListener("keyup", search);
search();
});
input.removeEventListener("focus", init);
} }
function search() { function search() {
while (results.firstChild) {
results.removeChild(results.firstChild);
}
if (input.value) { if (input.value) {
var hits = results.idx.search(`${input.value}*`); const hits = window.bookSearch.idx.search(`${input.value}*`);
results.innerHTML = JSON.stringify(hits); hits.slice(0, 10).forEach(function(hit) {
} else { const page = window.bookSearch.pages[hit.ref];
results.innerHTML = ''; const li = dummy.querySelector("li").cloneNode(true),
a = li.querySelector("a");
a.href = page.href;
a.textContent = page.title;
results.appendChild(li);
});
} }
} }
function loadScript(src) { function newLi(href, title) {
return new Promise(function(resolve, reject) {
let script = document.createElement('script');
return li;
}
function loadScript(src, callback) {
const script = document.createElement("script");
script.defer = true;
script.src = src; script.src = src;
script.onload = () => resolve(script); script.onload = callback;
document.head.append(script); document.head.append(script);
});
} }
}); })();

View file

@ -14,9 +14,6 @@ enableGitInfo = true
# pygmentsStyle = 'monokailight' # pygmentsStyle = 'monokailight'
pygmentsCodeFences = true pygmentsCodeFences = true
[outputs]
home = ["HTML", "JSON"]
[params] [params]
# (Optional, default 6) Set how many table of contents levels to be showed on page. # (Optional, default 6) Set how many table of contents levels to be showed on page.
# Use false to hide ToC, note that 0 will default to 6 (https://gohugo.io/functions/default/) # Use false to hide ToC, note that 0 will default to 6 (https://gohugo.io/functions/default/)
@ -48,5 +45,6 @@ pygmentsCodeFences = true
# - In blog posts # - In blog posts
BookDateFormat = 'Jan 2, 2006' BookDateFormat = 'Jan 2, 2006'
# (Optional, default true) Enables or disables search function with lunr.js # (Optional, default true) Enables search function with lunr.js,
# Index is built on fly, therefore it might slowdown your website.
BookSearch = true BookSearch = true

View file

@ -44,3 +44,7 @@ params:
# - In git information # - In git information
# - In blog posts # - In blog posts
BookDateFormat: 'Jan 2, 2006' BookDateFormat: 'Jan 2, 2006'
# (Optional, default true) Enables search function with lunr.js,
# Index is built on fly, therefore it might slowdown your website.
BookSearch: true

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1 @@
{"Target":"search-data.min.cab714d6cdc1fd81a6e2d8e454a03c6b8c74b4680118a20d06f2f4cc11365cdf.js","MediaType":"application/javascript","Data":{"Integrity":"sha256-yrcU1s3B/YGm4tjkVKA8a4x0tGgBGKINBvL0zBE2XN8="}}

View file

@ -1,6 +1,5 @@
addEventListener("load",function(){let input=document.querySelector("#book-search");let results=document.querySelector("#book-search-results");Promise.all([loadScript("/example/lunr.min.js"),loadScript("/example/index.json")]).then(enableLunr);function enableLunr(){results.idx=lunr(function(){this.ref('href') (function(){const input=document.querySelector("#book-search-input");const results=document.querySelector("#book-search-results");const dummy=document.querySelector("#book-search-dummy");input.addEventListener("focus",init);function init(){loadScript("/example/search-data.min.cab714d6cdc1fd81a6e2d8e454a03c6b8c74b4680118a20d06f2f4cc11365cdf.js",function(){input.disabled=false;input.addEventListener("keyup",search);search();});input.removeEventListener("focus",init);}
this.field('title') function search(){while(results.firstChild){results.removeChild(results.firstChild);}
this.field('content') if(input.value){const hits=window.bookSearch.idx.search(`${input.value}*`);hits.slice(0,10).forEach(function(hit){const page=window.bookSearch.pages[hit.ref];const li=dummy.querySelector("li").cloneNode(true),a=li.querySelector("a");a.href=page.href;a.textContent=page.title;results.appendChild(li);});}}
window.lunrData.forEach(function(page){this.add(page)},this)});input.addEventListener("keyup",search);} function newLi(href,title){return li;}
function search(){if(input.value){var hits=results.idx.search(`${input.value}*`);results.innerHTML=JSON.stringify(hits);}else{results.innerHTML='';}} function loadScript(src,callback){const script=document.createElement("script");script.defer=true;script.src=src;script.onload=callback;document.head.append(script);}})();
function loadScript(src){return new Promise(function(resolve,reject){let script=document.createElement('script');script.src=src;script.onload=()=>resolve(script);document.head.append(script);});}});

View file

@ -1 +1 @@
{"Target":"search.min.f37565c4baa28364f7b8e1e53c35bdb0ab92f2215165bd3f083f3f4f1ae78c71.js","MediaType":"application/javascript","Data":{"Integrity":"sha256-83VlxLqig2T3uOHlPDW9sKuS8iFRZb0/CD8/TxrnjHE="}} {"Target":"search.min.5b3c1973db27e97251bca8f705d321f8de35004cd65d2aaf71ca544590ce6b9d.js","MediaType":"application/javascript","Data":{"Integrity":"sha256-WzwZc9sn6XJRvKj3BdMh+N41AEzWXSqvccpURZDOa50="}}

View file

@ -1 +1 @@
{"Target":"book.min.60422b170f7beee5a981f3288523ee2bf275a0b48eba3a8983a3736189b9027f.css","MediaType":"text/css","Data":{"Integrity":"sha256-YEIrFw977uWpgfMohSPuK/J1oLSOujqJg6NzYYm5An8="}} {"Target":"book.min.d43e03103ef0f496eb5dfc0fb35ee914237fdcac29cb9b8e2764dac6fdc45ba1.css","MediaType":"text/css","Data":{"Integrity":"sha256-1D4DED7w9JbrXfwPs17pFCN/3Kwpy5uOJ2Taxv3EW6E="}}

View file

@ -1,10 +0,0 @@
window.lunrData = [
{{ range $index, $page := .Site.Pages }}
{{- if and $index (gt $index 0) -}},{{- end }}
{
"href": "{{ $page.RelPermalink }}",
"title": "{{ htmlEscape $page.Title }}",
"content": {{ $page.Plain | jsonify }}
}
{{- end -}}
]

View file

@ -6,8 +6,11 @@
{{- $styles := resources.Get "book.scss" | resources.ToCSS | resources.Minify | resources.Fingerprint }} {{- $styles := resources.Get "book.scss" | resources.ToCSS | resources.Minify | resources.Fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}"> <link rel="stylesheet" href="{{ $styles.RelPermalink }}">
{{- $search := resources.Get "search.js" | resources.ExecuteAsTemplate "search.js" . | resources.Minify | resources.Fingerprint }} {{ if default true .Site.Params.BookSearch }}
<script src="{{ $search.RelPermalink }}"></script> {{- $searchJS := resources.Get "search.js" | resources.ExecuteAsTemplate "search.js" . | resources.Minify | resources.Fingerprint }}
<script defer src="{{ "lunr.min.js" | relURL }}"></script>
<script defer src="{{ $searchJS.RelPermalink }}"></script>
{{ end }}
<!-- Favicon --> <!-- Favicon -->
<link rel="icon" href="{{ "favicon.png" | relURL }}" type="image/x-icon"> <link rel="icon" href="{{ "favicon.png" | relURL }}" type="image/x-icon">

View file

@ -1,2 +1,9 @@
<input type="text" placeholder="Search" class="book-search" id="book-search" /> {{ if default true .Site.Params.BookSearch }}
<div class="book-search">
<ul id="book-search-dummy" class="hidden">
<li><a href></a></li>
</ul>
<input type="text" placeholder="Search" id="book-search-input" />
<ul id="book-search-results"></ul> <ul id="book-search-results"></ul>
</div>
{{ end }}