Просмотр исходного кода

refactor: rename options for maps, search, and comments

Use provider names rather than integers.

BREAKING CHANGE:

- Rename `site.Params.comments.engine` to `site.Params.comments.provider`
- Rename `site.Params.search.engine` to `site.Params.search.provider`
- Rename `site.Params.map.engine` to `site.Params.map.provider`
- Use provider name (string) rather than integer
- For built-in search, provider is "wowchemy"
- For leaflet maps, the provider is "mapnik" or "mapbox"
George Cushen 4 лет назад
Родитель
Сommit
60d36e66d2

+ 1 - 0
.gitignore

@@ -10,6 +10,7 @@ node_modules/
 
 # Hugo
 resources/
+public/
 
 # Mac
 .DS_Store

+ 2 - 2
wowchemy/assets/js/wowchemy.js

@@ -262,7 +262,7 @@ function initMap() {
     let address = $('#map-dir').val();
     let api_key = $('#map-api-key').val();
 
-    if (map_provider == 1) {
+    if (map_provider === 'google') {
       let map = new GMaps({
         div: '#map',
         lat: lat,
@@ -289,7 +289,7 @@ function initMap() {
       });
     } else {
       let map = new L.map('map').setView([lat, lng], zoom);
-      if (map_provider == 3 && api_key.length) {
+      if (map_provider === 'mapbox' && api_key.length) {
         L.tileLayer('https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}', {
           attribution:
             'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="http://mapbox.com">Mapbox</a>',

+ 2 - 2
wowchemy/layouts/404.html

@@ -4,8 +4,8 @@
 
   <h1>{{ i18n "page_not_found" }}</h1>
 
-  {{/* Show search box if Academic's search engine is enabled. */}}
-  {{ if eq site.Params.search.engine 1 }}
+  {{/* Show search box if built-in search engine is enabled. */}}
+  {{ if eq (lower site.Params.search.provider) "wowchemy" }}
   <form class="d-flex align-items-center mb-3">
     <input name="q" type="search" class="form-control" placeholder="{{ i18n "search_placeholder" }}" autocomplete="off">
   </form>

+ 1 - 1
wowchemy/layouts/partials/book_sidebar.html

@@ -14,7 +14,7 @@
     </div>
   </button>
 
-  {{ if eq site.Params.search.engine 1 }}
+  {{ if eq (lower site.Params.search.provider) "wowchemy" }}
   <button class="form-control sidebar-search js-search d-none d-md-flex">
     <i class="fas fa-search pr-2"></i>
     <span class="sidebar-search-text">{{ i18n "search_placeholder" }}</span>

+ 17 - 9
wowchemy/layouts/partials/comments.html

@@ -1,11 +1,19 @@
-{{ $commentable_map := site.Params.comments.commentable | default dict }}
-{{ $commentable_bool := index $commentable_map .Type | default false }}
-{{ if site.Params.comments.engine | and $commentable_bool | and (ne .Params.commentable false) | or .Params.commentable }}
-<section id="comments">
-  {{ if eq site.Params.comments.engine 1 }}
-    {{ partial "comments/disqus.html" . }}
-  {{ else if eq site.Params.comments.engine 2 }}
-    {{ partial "comments/commento.html" . }}
+{{ $provider := trim (site.Params.comments.provider | lower) " " }}
+
+{{ if $provider }}
+  {{ $provider_tpl := printf "partials/comments/%s" $provider }}
+  {{ $provider_exists := templates.Exists $provider_tpl }}
+  {{ if not $provider_exists }}
+    {{ errorf "The '%s' comment provider was not found." $provider }}
+  {{ end }}
+
+  {{ $commentable_page_types := site.Params.comments.commentable | default dict }}
+  {{ $commentable_page_type := index $commentable_page_types .Type | default false }}
+  {{ $commentable_page := (ne .Params.commentable false) | and $commentable_page_type }}
+
+  {{ if $commentable_page }}
+  <section id="comments">
+    {{ partial $provider_tpl . }}
+  </section>
   {{ end }}
-</section>
 {{ end }}

+ 4 - 0
wowchemy/layouts/partials/comments/disqus.html

@@ -19,4 +19,8 @@
 </script>
 <noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
 <a href="https://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
+
+{{ if (site.Params.comments.disqus.show_count | default true) }}
+  <script id="dsq-count-scr" src="https://{{site.Params.comments.disqus.shortname}}.disqus.com/count.js" async></script>
+{{end}}
 {{end}}

+ 1 - 1
wowchemy/layouts/partials/docs_sidebar.html

@@ -20,7 +20,7 @@
     <span><i class="fas fa-bars"></i></span>
   </button>
 
-  {{ if eq site.Params.search.engine 1 }}
+  {{ if eq (lower site.Params.search.provider) "wowchemy" }}
   <input name="q" type="search" class="form-control" placeholder="{{ i18n "search_placeholder" }}" autocomplete="off">
   {{ end }}
 </form>

+ 1 - 1
wowchemy/layouts/partials/jsonld/website.html

@@ -2,7 +2,7 @@
 {
   "@context": "https://schema.org",
   "@type": "WebSite",
-  {{- if site.Params.search.engine }}
+  {{- if site.Params.search.provider }}
   "potentialAction": {
     "@type": "SearchAction",
     "target": {{ printf "%s?q={search_term_string}" site.BaseURL }},

+ 1 - 1
wowchemy/layouts/partials/navbar.html

@@ -153,7 +153,7 @@
       {{ end }}
 
       {{ $show_search := site.Params.main_menu.show_search | default true }}
-      {{ if and site.Params.search.engine $show_search }}
+      {{ if and site.Params.search.provider $show_search }}
       <li class="nav-item">
         <a class="nav-link js-search" href="#" aria-label="{{ i18n "search" }}"><i class="fas fa-search" aria-hidden="true"></i></a>
       </li>

+ 7 - 6
wowchemy/layouts/partials/page_metadata.html

@@ -46,12 +46,13 @@
   {{ end }}
 
   {{/* Show Disqus comment count if enabled. */}}
-  {{ $commentable_map := site.Params.comments.commentable | default dict }}
-  {{ $commentable_bool := index $commentable_map $page.Type | default false }}
-  {{ $disqus_enabled := eq site.Params.comments.engine 1 | and $commentable_bool | and (ne $page.Params.commentable false) | or $page.Params.commentable }}
-  {{ if and $disqus_enabled (site.Params.comments.disqus.show_count | default true) }}
-  <span class="middot-divider"></span>
-  <a href="{{ $page.RelPermalink }}#disqus_thread"><!-- Count will be inserted here --></a>
+  {{ $comments_provider := trim (site.Params.comments.provider | lower) " " }}
+  {{ $commentable_page_types := site.Params.comments.commentable | default dict }}
+  {{ $commentable_page_type := index $commentable_page_types $page.Type | default false }}
+  {{ $commentable_page := (ne $page.Params.commentable false) | and $commentable_page_type }}
+  {{ if (eq $comments_provider "disqus") | and (site.Params.comments.disqus.show_count | default true) | and $commentable_page }}
+    <span class="middot-divider"></span>
+    <a href="{{ $page.RelPermalink }}#disqus_thread"><!-- Count will be inserted here --></a>
   {{ end}}
 
   {{ $taxonomy := "categories" }}

+ 4 - 3
wowchemy/layouts/partials/search.html

@@ -1,5 +1,6 @@
 {{/* Partial for built-in search and Algolia search. */}}
-{{ if eq site.Params.search.engine 1 | or (eq site.Params.search.engine 2) }}
+{{ $search_provider := lower site.Params.search.provider }}
+{{ if in (slice "wowchemy" "algolia") $search_provider }}
 <aside class="search-modal" id="search">
   <div class="container">
     <section class="search-header">
@@ -14,7 +15,7 @@
       </div>
 
       <div id="search-box">
-        {{ if eq site.Params.search.engine 1 }}
+        {{ if eq $search_provider "wowchemy" }}
         <input name="q" id="search-query" placeholder="{{i18n "search_placeholder"}}" autocapitalize="off"
         autocomplete="off" autocorrect="off" spellcheck="false" type="search" class="form-control"
         aria-label="{{i18n "search_placeholder"}}">
@@ -30,7 +31,7 @@
         {{ $search_queries = site.Data.search_queries }}
       {{end}}
 
-      {{ if eq site.Params.search.engine 1 | and $search_queries }}
+      {{ if eq $search_provider "wowchemy" | and $search_queries }}
       <div id="search-common-queries" class="pt-3">
         <div class="font-weight-bold pb-3">{{ i18n "search_common_queries" | default "Common searches" }}</div>
         <ul class="fa-ul">

+ 4 - 2
wowchemy/layouts/partials/site_head.html

@@ -111,11 +111,13 @@
       {{ end }}
     {{ end }}
 
-    {{ if or (eq site.Params.map.engine 2) (eq site.Params.map.engine 3) }}
+    {{/* Maps CSS. */}}
+    {{ $map_provider := lower site.Params.map.provider }}
+    {{ if in (slice "mapnik" "mapbox") $map_provider }}
       {{ printf "<link rel=\"stylesheet\" href=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\" media=\"print\" onload=\"this.media='all'\">" (printf $css.leaflet.url $css.leaflet.version) $css.leaflet.sri | safeHTML }}
     {{ end }}
 
-    {{ if eq site.Params.search.engine 2 }}
+    {{ if eq (lower site.Params.search.provider) "algolia" }}
       {{ printf "<link rel=\"stylesheet\" href=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\">" (printf $css.instantsearch.url $css.instantsearch.version) $css.instantsearch.sri | safeHTML }}
       {{ printf "<link rel=\"stylesheet\" href=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\">" (printf $css.instantsearchTheme.url $css.instantsearchTheme.version) $css.instantsearchTheme.sri | safeHTML }}
     {{ end }}

+ 37 - 46
wowchemy/layouts/partials/site_js.html

@@ -31,12 +31,13 @@
     {{ end }}
 
     {{/* Maps JS. */}}
-    {{ if eq site.Params.map.engine 1 }}
+    {{ $map_provider := lower site.Params.map.provider }}
+    {{ if eq $map_provider "google" }}
       <script async defer src="https://maps.googleapis.com/maps/api/js?key={{ site.Params.map.api_key }}"></script>
       {{ if ($scr.Get "use_cdn") }}
-      {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $js.gmaps.url $js.gmaps.version) $js.gmaps.sri | safeHTML }}
+        {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $js.gmaps.url $js.gmaps.version) $js.gmaps.sri | safeHTML }}
       {{ end }}
-    {{ else if and (or (eq site.Params.map.engine 2) (eq site.Params.map.engine 3)) ($scr.Get "use_cdn") }}
+    {{ else if (in (slice "mapnik" "mapbox") $map_provider) | and ($scr.Get "use_cdn") }}
       {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $js.leaflet.url $js.leaflet.version) $js.leaflet.sri | safeHTML }}
     {{ end }}
 
@@ -48,43 +49,38 @@
     </script>
     {{ end }}
 
-    {{ if eq site.Params.search.engine 1 }}
-    {{/* Fuse search result template. */}}
-    <script id="search-hit-fuse-template" type="text/x-template">
-      <div class="search-hit" id="summary-{{"{{key}}"}}">
-        <div class="search-hit-content">
-          <div class="search-hit-name">
-            {{ printf "<a href=\"%s\">%s</a>" "{{relpermalink}}" "{{title}}" | safeHTML }}
-            <div class="article-metadata search-hit-type">{{"{{type}}"}}</div>
-            <p class="search-hit-description">{{"{{snippet}}"}}</p>
+    {{ $algoliaConfig := dict }}
+    {{ $search_provider := lower site.Params.search.provider }}
+    {{ if eq $search_provider "wowchemy" }}
+      {{/* Wowchemy built-in search result template (Fuse). */}}
+      <script id="search-hit-fuse-template" type="text/x-template">
+        <div class="search-hit" id="summary-{{"{{key}}"}}">
+          <div class="search-hit-content">
+            <div class="search-hit-name">
+              {{ printf "<a href=\"%s\">%s</a>" "{{relpermalink}}" "{{title}}" | safeHTML }}
+              <div class="article-metadata search-hit-type">{{"{{type}}"}}</div>
+              <p class="search-hit-description">{{"{{snippet}}"}}</p>
+            </div>
           </div>
         </div>
-      </div>
-    </script>
-    {{ else if eq site.Params.search.engine 2 }}
-    {{/* Algolia search result template. */}}
-    <script id="search-hit-algolia-template" type="text/html">
-      <div class="search-hit">
-        <div class="search-hit-content">
-          <div class="search-hit-name">
-            {{ printf "<a href=\"%s\">{{{_highlightResult.title.value}}}</a>" "{{relpermalink}}" | safeHTML }}
+      </script>
+      {{ if ($scr.Get "use_cdn") }}
+        {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $js.fuse.url $js.fuse.version) $js.fuse.sri | safeHTML }}
+        {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $js.mark.url $js.mark.version) $js.mark.sri | safeHTML }}
+      {{ end }}
+    {{ else if eq $search_provider "algolia" }}
+      {{/* Algolia search result template. */}}
+      <script id="search-hit-algolia-template" type="text/html">
+        <div class="search-hit">
+          <div class="search-hit-content">
+            <div class="search-hit-name">
+              {{ printf "<a href=\"%s\">{{{_highlightResult.title.value}}}</a>" "{{relpermalink}}" | safeHTML }}
+            </div>
+            <div class="article-metadata search-hit-type">{{"{{type}}"}}</div>
+            <p class="search-hit-description">{{ safeHTML "{{{_highlightResult.summary.value}}}" }}</p>
           </div>
-          <div class="article-metadata search-hit-type">{{"{{type}}"}}</div>
-          <p class="search-hit-description">{{ safeHTML "{{{_highlightResult.summary.value}}}" }}</p>
         </div>
-      </div>
-    </script>
-    {{ end }}
-
-    {{/* Fuse search engine. */}}
-    {{ if and (eq site.Params.search.engine 1) ($scr.Get "use_cdn") }}
-    {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $js.fuse.url $js.fuse.version) $js.fuse.sri | safeHTML }}
-    {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $js.mark.url $js.mark.version) $js.mark.sri | safeHTML }}
-    {{ end }}
-
-    {{/* Algolia search engine. */}}
-    {{ $algoliaConfig := dict }}
-    {{ if eq site.Params.search.engine 2 }}
+      </script>
       {{ if ($scr.Get "use_cdn") }}
         {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $js.instantsearch.url $js.instantsearch.version) $js.instantsearch.sri | safeHTML }}
       {{ end }}
@@ -93,12 +89,7 @@
 
     {{/* Charts */}}
     {{ if .HasShortcode "chart" }}
-    {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $js.plotly.url $js.plotly.version) $js.plotly.sri | safeHTML }}
-    {{ end }}
-
-    {{/* Disqus Comment Count JS. */}}
-    {{ if and (eq site.Params.comments.engine 1) (site.Params.comments.disqus.show_count | default true) }}
-    <script id="dsq-count-scr" src="https://{{site.Params.comments.disqus.shortname}}.disqus.com/count.js" async></script>
+      {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $js.plotly.url $js.plotly.version) $js.plotly.sri | safeHTML }}
     {{ end }}
 
     {{ $js_bootstrap := resources.Get "js/_vendor/bootstrap.bundle.js" }}
@@ -108,7 +99,7 @@
     <script src="{{ $js_bootstrap.RelPermalink }}"></script>
 
     {{ $js_search_params := dict }}
-    {{ if ne site.Params.search.engine 0 }}
+    {{ if in (slice "wowchemy" "algolia") $search_provider }}
       {{/* Configure search engine. */}}
       {{ $min_length := site.Params.search.wowchemy.min_length | default 1 }}
       {{ $threshold := site.Params.search.wowchemy.threshold | default 0.3 }}
@@ -122,13 +113,13 @@
     {{ $js_license := $js_license | printf "%s/*! Copyright 2016-present George Cushen (https://georgecushen.com/) */\n" }}
     {{ $js_license := $js_license | printf "%s/*! License: https://github.com/wowchemy/wowchemy-hugo-modules/blob/master/LICENSE.md */\n" }}
     {{ $js_bundle_head := $js_license | resources.FromString "js/bundle-head.js" }}
-    {{ $js_params := dict "hugoEnvironment" hugo.Environment "codeHighlighting" ($scr.Get "highlight_enabled" | default false) "searchEnabled" (ne site.Params.search.engine 0) }}
+    {{ $js_params := dict "hugoEnvironment" hugo.Environment "codeHighlighting" ($scr.Get "highlight_enabled" | default false) "searchEnabled" (in (slice "wowchemy" "algolia") $search_provider) }}
     {{ $js_academic := resources.Get "js/wowchemy.js" | js.Build (dict "targetPath" (printf "%s/js/wow-core.js" .Lang ) "params" $js_params) }}
     {{ $js_bundle := slice $js_academic }}
-    {{ if eq site.Params.search.engine 1 }}
+    {{ if eq $search_provider "wowchemy" }}
       {{ $js_academic_search := resources.Get "js/wowchemy-search.js" | js.Build (dict "targetPath" (printf "%s/js/wow-search-built.js" .Lang) "params" $js_search_params) }}
       {{ $js_bundle = $js_bundle | append $js_academic_search }}
-    {{ else if eq site.Params.search.engine 2 }}
+    {{ else if eq $search_provider "algolia" }}
       {{ $js_algolia_search := resources.Get "js/algolia-search.js" | js.Build (dict "targetPath" (printf "%s/js/algolia-search-built.js" .Lang) "params" $js_search_params) }}
       {{ $js_bundle = $js_bundle | append $js_algolia_search }}
     {{ end }}

+ 2 - 2
wowchemy/layouts/partials/widgets/contact.html

@@ -136,9 +136,9 @@
 
   </ul>
 
-  {{ if and site.Params.map.engine $data.coordinates.latitude }}
+  {{ if and site.Params.map.provider $data.coordinates.latitude }}
   <div class="d-none">
-    <input id="map-provider" value="{{ site.Params.map.engine }}">
+    <input id="map-provider" value="{{ lower site.Params.map.provider }}">
     <input id="map-lat" value="{{ $data.coordinates.latitude }}">
     <input id="map-lng" value="{{ $data.coordinates.longitude }}">
     <input id="map-dir" value="{{ $addr_formatted }}">