Skip to content

Commit

Permalink
Swap in PageFind for Google Custom Search
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Abraham <cjyabraham@gmail.com>
  • Loading branch information
cjyabraham committed Apr 3, 2024
1 parent 680ff1a commit 68bc21c
Show file tree
Hide file tree
Showing 15 changed files with 1,477 additions and 197 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -3,4 +3,5 @@ website/resources
public/
go.local.mod
go.sum
.DS_Store
.DS_Store
pagefind
4 changes: 0 additions & 4 deletions website/Dockerfile

This file was deleted.

28 changes: 28 additions & 0 deletions website/Makefile
@@ -0,0 +1,28 @@
serve:
hugo server \
--disableFastRender \
--buildDrafts \
--buildFuture \
--ignoreCache
--printI18nWarnings \
--printMemoryUsage \
--printPathWarnings \
--printUnusedTemplates \
--templateMetrics \
--templateMetricsHints \
--gc

production-build:
git submodule update --init --recursive
hugo \
--minify
npx -y pagefind --site public

preview-build:
git submodule update --init --recursive
hugo \
--baseURL $(DEPLOY_PRIME_URL) \
--buildDrafts \
--buildFuture \
--minify
npx -y pagefind --site public
43 changes: 43 additions & 0 deletions website/assets/js/search.js
@@ -0,0 +1,43 @@
/*
Copyright 2018 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

(function($) {

'use strict';

var Search = {
init: function() {
$(document).ready(function() {
$(document).on('keypress', '.td-search-input', function(e) {
if (e.keyCode !== 13) {
return
}

var query = $(this).val();
var searchPage = $(this).data('search-page') + "?q=" + query;
document.location = searchPage;

return false;
});

});
},
};

Search.init();


}(jQuery));
12 changes: 12 additions & 0 deletions website/assets/scss/_search.scss
Expand Up @@ -76,3 +76,15 @@
}
}
}


.pagefind-ui__result-link {
color: $link-color !important;
}
.pagefind-ui__search-clear {
padding-left: 20px !important;
padding-right: 20px !important;
}
#search {
margin-top: 40px;
}
2 changes: 1 addition & 1 deletion website/config.toml
Expand Up @@ -98,7 +98,7 @@ version = "0.0"
url_latest_version = "https://maturitymodel.cncf.io"

# Google Custom Search Engine ID. Remove or comment out to disable search.
gcs_engine_id = "5104307cea6b94e6b"
# gcs_engine_id = "5104307cea6b94e6b"

# Enable Algolia DocSearch
algolia_docsearch = false
Expand Down
2 changes: 1 addition & 1 deletion website/content/de/contribute/_index.md
Expand Up @@ -201,4 +201,4 @@ cd website
npm installieren
```

Anschließend können Sie die Website mit `hugo server` ausführen.
Anschließend können Sie die Website mit `npm run serve` ausführen.
2 changes: 1 addition & 1 deletion website/content/en/contribute/_index.md
Expand Up @@ -202,4 +202,4 @@ cd website
npm install
```

You can then run the site using `hugo server`.
Then run the site using `npm run serve`. To have the site run locally with a functioning local search, run `npm run serve:with-pagefind`.
13 changes: 0 additions & 13 deletions website/docker-compose.yaml

This file was deleted.

2 changes: 1 addition & 1 deletion website/layouts/_default/content.html
@@ -1,4 +1,4 @@
<div class="td-content">
<div data-pagefind-body class="td-content">
<h1>{{ .Title }}</h1>
{{ with .Params.description }}<div class="lead">{{ . | markdownify }}</div>{{ end }}
{{ if (and (not .Params.hide_readingtime) (.Site.Params.ui.readingtime.enable)) }}
Expand Down
75 changes: 59 additions & 16 deletions website/layouts/_default/search.html
@@ -1,20 +1,63 @@
{{ define "main" }}
<div>
<h1 class="thin">{{ .Title }}</h2>
{{ with .Site.Params.gcs_engine_id }}
<script>
(function() {
var cx = '{{ . }}';
var gcse = document.createElement('script');
gcse.type = 'text/javascript';
gcse.async = true;
gcse.src = 'https://cse.google.com/cse.js?cx=' + cx;
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(gcse, s);
})();
</script>
<gcse:searchresults-only></gcse:searchresults-only>
{{ end }}
</div>
<h1 class="thin">{{ .Title }}</h1>

<link href='{{ "/pagefind/pagefind-ui.css" }}' rel="stylesheet">

<style>
:root {
--pagefind-ui-scale: 1;
--pagefind-ui-primary: #000;
--pagefind-ui-text: #000;
--pagefind-ui-background: #ffffff;
--pagefind-ui-border: #eeeeee;
--pagefind-ui-tag: #eeeeee;
--pagefind-ui-border-width: 2px;
--pagefind-ui-border-radius: 8px;
--pagefind-ui-image-border-radius: 8px;
--pagefind-ui-image-box-ratio: 3 / 2;
--pagefind-ui-font: 'Clarity City', -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif, Roboto,
Ubuntu, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol';
}
</style>
<script src='{{ "/pagefind/pagefind-ui.js" }}' defer></script>

<div id="search"></div>
<script>
window.addEventListener("DOMContentLoaded", (event) => {
pagefind = new PagefindUI({ element: "#search", showSubResults: true, showImages: false, resetStyles: false });

const urlParams = new URLSearchParams(window.location.search);
const q = urlParams.get('q');
if(q){
setTimeout(function(){
pagefind.triggerSearch(q);
}, 1000);
}

$("#search input").on("input", function() {
var inputValue = $(this).val();
var queryStringVar = "q";
updateQueryString(queryStringVar, inputValue);
});
});

function updateQueryString(key, value) {
var baseUrl = window.location.href.split("?")[0];
var queryString = window.location.search.slice(1);
var urlParams = new URLSearchParams(queryString);

if (urlParams.has(key)) {
urlParams.set(key, value);
} else {
urlParams.append(key, value);
}

var newUrl = baseUrl + "?" + urlParams.toString();
// Update the browser history (optional)
window.history.pushState({}, null, newUrl);
}
</script>

</div>
{{ end }}
37 changes: 3 additions & 34 deletions website/layouts/partials/search-input.html
@@ -1,35 +1,4 @@
{{ if or .Site.Params.gcs_engine_id .Site.Params.algolia_docsearch }}
<div class="add-search-icon">
<input type="search" class="form-control td-search-input" placeholder="{{ T "ui_search" }}" aria-label="{{ T "ui_search" }}" autocomplete="off">
<svg xmlns="http://www.w3.org/2000/svg" role="img" viewBox="0.88 0.63 32.17 30.24"><g opacity=".945"><path stroke="#000" stroke-width="4.392" d="M13.746 23.279c5.536 0 10.024-4.432 10.024-9.9 0-5.466-4.488-9.898-10.024-9.898-5.536 0-10.024 4.432-10.024 9.899 0 5.467 4.488 9.899 10.024 9.899zm6.56-5.069l10.652 10.52" fill="transparent" /></g></svg>
</div>
{{ else if .Site.Params.offlineSearch }}
{{ $offlineSearchIndex := resources.Get "json/offline-search-index.json" | resources.ExecuteAsTemplate "offline-search-index.json" . }}
{{ $offlineSearchLink := $offlineSearchIndex.RelPermalink -}}
{{ if hugo.IsProduction -}}
{{/* Use `md5` as finger print hash function to shorten file name to avoid `file name too long` error. */}}
{{ $offlineSearchIndexFingerprint := $offlineSearchIndex | resources.Fingerprint "md5" }}
{{ $offlineSearchLink = $offlineSearchIndexFingerprint.RelPermalink -}}
{{ end -}}

<div class="add-search-icon">
<input
type="search"
class="form-control td-search-input"
placeholder="{{ T "ui_search" }}"
aria-label="{{ T "ui_search" }}"
autocomplete="off"
{{/*
The data attribute name of the json file URL must end with `src` since
Hugo's absurlreplacer requires `src`, `href`, `action` or `srcset` suffix for the attribute name.
If the absurlreplacer is not applied, the URL will start with `/`.
It causes the json file loading error when when relativeURLs is enabled.
https://github.com/google/docsy/issues/181
*/}}
data-offline-search-index-json-src="{{ $offlineSearchLink }}"
data-offline-search-base-href="/"
data-offline-search-max-results="{{ .Site.Params.offlineSearchMaxResults | default 10 }}"
>
<svg xmlns="http://www.w3.org/2000/svg" role="img" viewBox="0.88 0.63 32.17 30.24"><g opacity=".945"><path stroke="#000" stroke-width="4.392" d="M13.746 23.279c5.536 0 10.024-4.432 10.024-9.9 0-5.466-4.488-9.898-10.024-9.898-5.536 0-10.024 4.432-10.024 9.899 0 5.467 4.488 9.899 10.024 9.899zm6.56-5.069l10.652 10.52" fill="transparent" /></g></svg>
</div>
{{ end }}
<input type="search" data-search-page="{{ "search/" | relLangURL }}" class="form-control td-search-input" placeholder="{{ T "ui_search" }}" aria-label="{{ T "ui_search" }}" autocomplete="off">
<svg xmlns="http://www.w3.org/2000/svg" role="img" viewBox="0.88 0.63 32.17 30.24"><g opacity=".945"><path stroke="#000" stroke-width="4.392" d="M13.746 23.279c5.536 0 10.024-4.432 10.024-9.9 0-5.466-4.488-9.898-10.024-9.898-5.536 0-10.024 4.432-10.024 9.899 0 5.467 4.488 9.899 10.024 9.899zm6.56-5.069l10.652 10.52" fill="transparent" /></g></svg>
</div>
16 changes: 13 additions & 3 deletions website/netlify.toml
@@ -1,10 +1,20 @@
[build]
publish = "public"
command = "make production-build"
ignore = "exit 1"

[build.environment]
HUGO_VERSION = "0.104.3"
GO_VERSION = "1.19.2"
NODE_VERSION = "18.16.1"
HUGO_VERSION = "0.115.2"

[context.production.environment]
HUGO_ENV = "production"

[context.deploy-preview.environment]
HUGO_ENV = "preview"
HUGO_ENV = "preview"

[context.deploy-preview]
command = "make preview-build"

[context.branch-deploy]
command = "make preview-build"

0 comments on commit 68bc21c

Please sign in to comment.