From dda0a0eab19457598b71f2b2b2f978b09d3f95c7 Mon Sep 17 00:00:00 2001
From: Alex Shpak <alex-shpak@users.noreply.github.com>
Date: Mon, 15 Jul 2019 18:25:21 +0200
Subject: Start work on search feature with lunr

---
 layouts/partials/docs/brand.html         |  2 +-
 layouts/partials/docs/html-head.html     |  9 ++++---
 layouts/partials/docs/menu-bundle.html   | 15 ++++++++++++
 layouts/partials/docs/menu-filetree.html |  4 ++--
 layouts/partials/docs/menu.html          | 14 ++++++++---
 layouts/partials/docs/mobile-header.html |  4 ++--
 layouts/partials/docs/search.html        |  2 ++
 layouts/partials/docs/shared.html        | 40 --------------------------------
 layouts/partials/docs/title.html         |  9 +++++++
 9 files changed, 48 insertions(+), 51 deletions(-)
 create mode 100644 layouts/partials/docs/search.html
 delete mode 100644 layouts/partials/docs/shared.html
 create mode 100644 layouts/partials/docs/title.html

(limited to 'layouts/partials/docs')

diff --git a/layouts/partials/docs/brand.html b/layouts/partials/docs/brand.html
index 5a2c9cb..7d7b721 100644
--- a/layouts/partials/docs/brand.html
+++ b/layouts/partials/docs/brand.html
@@ -1,3 +1,3 @@
 <h2 class="book-brand">
   <a href="{{ .Site.BaseURL }}">{{ .Site.Title }}</a>
-</h2>
\ No newline at end of file
+</h2>
diff --git a/layouts/partials/docs/html-head.html b/layouts/partials/docs/html-head.html
index 5fbc549..805c2b5 100644
--- a/layouts/partials/docs/html-head.html
+++ b/layouts/partials/docs/html-head.html
@@ -1,11 +1,14 @@
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
-<title>{{- template "title" . }} | {{ .Site.Title -}}</title>
+<title>{{ partial "docs/title" . }} | {{ .Site.Title -}}</title>
 
-<!-- Theme stylesheet, you can customize scss by creatig `assets/custom.scss` in your website -->
-{{ $styles := resources.Get "book.scss" | resources.ToCSS | resources.Minify | resources.Fingerprint }}
+<!-- Theme stylesheet, you can customize scss by creating `assets/custom.scss` in your website -->
+{{- $styles := resources.Get "book.scss" | resources.ToCSS | resources.Minify | resources.Fingerprint }}
 <link rel="stylesheet" href="{{ $styles.RelPermalink }}">
 
+{{- $search := resources.Get "search.js" | resources.ExecuteAsTemplate "search.js" . | resources.Minify | resources.Fingerprint }}
+<script src="{{ $search.RelPermalink }}"></script>
+
 <!-- Favicon -->
 <link rel="icon" href="{{ "favicon.png" | relURL }}" type="image/x-icon">
 
diff --git a/layouts/partials/docs/menu-bundle.html b/layouts/partials/docs/menu-bundle.html
index bfc1650..2aedb54 100644
--- a/layouts/partials/docs/menu-bundle.html
+++ b/layouts/partials/docs/menu-bundle.html
@@ -2,3 +2,18 @@
 {{ with .Site.GetPage .Site.Params.BookMenuBundle }}
   {{- .Content -}}
 {{ end }}
+
+{{ define "hrefhack" }}
+  {{ $attrEq := "$=" }}
+  {{ $attrVal := .RelPermalink }}
+  {{ if eq .RelPermalink "/" }}
+    {{ $attrEq = "=" }}
+    {{ $attrVal = .Permalink }}
+  {{ end }}
+
+  <style>
+  nav ul a[href{{ $attrEq }}"{{ $attrVal }}"] {
+      color: {{ default "#004ed0" .Site.Params.BookMenuBundleActiveLinkColor }};
+  }
+  </style>
+{{ end }}
diff --git a/layouts/partials/docs/menu-filetree.html b/layouts/partials/docs/menu-filetree.html
index 9ed63ea..abc6fd9 100644
--- a/layouts/partials/docs/menu-filetree.html
+++ b/layouts/partials/docs/menu-filetree.html
@@ -27,7 +27,7 @@
     {{ if .Content }}
       {{ template "book-page-link" (dict "Page" . "CurrentPage" $.CurrentPage) }}
     {{ else }}
-      <span>{{ template "title" . }}</span>
+      <span>{{ partial "docs/title" . }}</span>
     {{ end }}
 
     {{ template "book-section-children" (dict "Section" . "CurrentPage" $.CurrentPage) }}
@@ -53,7 +53,7 @@
 {{ define "book-page-link" }}
 {{ with .Page }}
   <a href="{{ .RelPermalink }}" {{ if eq $.CurrentPage .Permalink }} class="active"{{ end }}>
-    {{ template "title" . }}
+      {{ partial "docs/title" . }}
   </a>
 {{ end }}
 {{ end }}
diff --git a/layouts/partials/docs/menu.html b/layouts/partials/docs/menu.html
index d280713..6a72ac9 100644
--- a/layouts/partials/docs/menu.html
+++ b/layouts/partials/docs/menu.html
@@ -1,5 +1,6 @@
 <nav>
 {{ partial "docs/brand" . }}
+{{ partial "docs/search" . }}
 {{ partial "docs/inject/menu-before" . }}
 
 {{ if .Site.Params.BookMenuBundle }}
@@ -11,6 +12,13 @@
 {{ partial "docs/inject/menu-after" . }}
 </nav>
 
-{{ if .Site.Params.BookEnableJS }}
-  {{ template "jsmenu" . }}
-{{ end }}
+<!-- Restore menu position as soon as possible to avoid flickering -->
+<script>
+(function() {
+  var menu = document.querySelector("aside.book-menu nav");
+  addEventListener("beforeunload", function(event) {
+    localStorage.setItem("menu.scrollTop", menu.scrollTop);
+  });
+  menu.scrollTop = localStorage.getItem("menu.scrollTop");
+})();
+</script>
diff --git a/layouts/partials/docs/mobile-header.html b/layouts/partials/docs/mobile-header.html
index b8fa2a9..720b83d 100644
--- a/layouts/partials/docs/mobile-header.html
+++ b/layouts/partials/docs/mobile-header.html
@@ -1,6 +1,6 @@
-<header class="align-center justify-between book-header">
+<header class="flex align-center justify-between book-header">
   <label for="menu-control">
     <img src="{{ "svg/menu.svg" | relURL }}" alt="Menu" />
   </label>
-  <strong>{{- template "title" . }}</strong>
+  <strong>{{ partial "docs/title" . }}</strong>
 </header>
diff --git a/layouts/partials/docs/search.html b/layouts/partials/docs/search.html
new file mode 100644
index 0000000..d4b0e65
--- /dev/null
+++ b/layouts/partials/docs/search.html
@@ -0,0 +1,2 @@
+<input type="text" placeholder="Search" class="book-search" id="book-search" />
+<ul id="book-search-results"></ul>
diff --git a/layouts/partials/docs/shared.html b/layouts/partials/docs/shared.html
deleted file mode 100644
index 2545dea..0000000
--- a/layouts/partials/docs/shared.html
+++ /dev/null
@@ -1,40 +0,0 @@
-{{/*These templates contains some more complex logic and shared between partials*/}}
-{{ define "title" }}
-  {{ if and .IsSection .File }}
-    {{ $sections := split (trim .File.Dir "/") "/" }}
-    {{ $title := index ($sections | last 1) 0 | humanize | title }}
-    {{ default $title .Title }}
-  {{ else if and .IsPage .File }}
-    {{ $title := .File.BaseFileName | humanize | title }}
-    {{ default $title .Title }}
-  {{ else }}
-    {{ .Title }}
-  {{ end }}
-{{ end }}
-
-{{ define "hrefhack" }}
-  {{ $attrEq := "$=" }}
-  {{ $attrVal := .RelPermalink }}
-  {{ if eq .RelPermalink "/" }}
-    {{ $attrEq = "=" }}
-    {{ $attrVal = .Permalink }}
-  {{ end }}
-
-  <style>
-  nav ul a[href{{ $attrEq }}"{{ $attrVal }}"] {
-      color: {{ default "#004ed0" .Site.Params.BookMenuBundleActiveLinkColor }};
-  }
-  </style>
-{{ end }}
-
-{{ define "jsmenu" }}
-<script>
-(function() {
-  var menu = document.querySelector('aside.book-menu nav')
-  addEventListener('beforeunload', function(event) {
-    localStorage.setItem('menu.scrollTop', menu.scrollTop)
-  });
-  menu.scrollTop = localStorage.getItem('menu.scrollTop')
-})()
-</script>
-{{ end }}
diff --git a/layouts/partials/docs/title.html b/layouts/partials/docs/title.html
new file mode 100644
index 0000000..91d5f81
--- /dev/null
+++ b/layouts/partials/docs/title.html
@@ -0,0 +1,9 @@
+{{ $title := .Title }}
+{{ if and .IsSection .File }}
+  {{ $sections := split (trim .File.Dir "/") "/" }}
+  {{ $title = index ($sections | last 1) 0 | humanize | title }}
+{{ else if and .IsPage .File }}
+  {{ $title = .File.BaseFileName | humanize | title }}
+{{ end }}
+
+{{ return $title }}
-- 
cgit v1.2.3