Add search to documentation

This commit is contained in:
Simon Binder
2021-08-09 18:32:09 +02:00
parent 40a1c91e25
commit edf94f26cc
7 changed files with 79 additions and 3 deletions
+43
View File
@@ -0,0 +1,43 @@
import 'dart:html';
// ignore: non_constant_identifier_names
void built_site_main() {
if (window.location.pathname?.startsWith('/search') == true) {
final privacyTag = document.getElementById('privacy');
void loadSearchResults() {
privacyTag?.remove();
final element = ScriptElement()
..async = true
// ignore: unsafe_html
..src =
'https://cse.google.com/cse.js?cx=002567324444333206795:_yptu7lact8';
document.head?.append(element);
}
final storage = window.localStorage;
// Only load search results after consent.
if (storage.containsKey('google_ok')) {
loadSearchResults();
} else {
privacyTag?.style.visibility = 'visible';
document.getElementById('accept')?.onClick.first.whenComplete(() {
storage['google_ok'] = '';
loadSearchResults();
});
}
}
// Make the search box functional
for (final element in document
.querySelectorAll('.td-search-input')
.whereType<InputElement>()) {
element.onKeyPress.where((e) => e.keyCode == 13).first.whenComplete(() {
final value = element.value;
if (value != null && value.isNotEmpty) {
window.location.assign('/search/?q=${Uri.encodeQueryComponent(value)}');
}
});
}
}
+6
View File
@@ -0,0 +1,6 @@
---
data:
title: Search results
path: /search
template: search.html
---
+3 -2
View File
@@ -1,2 +1,3 @@
<link rel="stylesheet" href="{{ "/main.css" | relUrl }}">
<script defer src="{{ "/main.dart.js" | relUrl }}"></script>
{% if page.path == '/search' %}
<link rel="stylesheet" href="/search.css">
{% endif %}
+22
View File
@@ -0,0 +1,22 @@
{% capture main %}
<section class="row td-search-result">
<div class="col-12 col-md-8 offset-md-2">
<h1 id="title">Search results</h1>
<div id="privacy">
<p>
The search results on this site are powered by Google.
By clicking continue and continuing to use the search function on this site,
you agree to the transmission of data to Google under
<a href="http://www.google.com/intl/policies/privacy">Google's privacy policy.</a>
</p>
<a class="btn btn-secondary mr-3 mb-4" id="accept">
Continue and show results
</a>
</div>
<div class="gcse-searchresults-only"></div>
</div>
</section>
{% endcapture %}
{% include "_default/baseof.html" %}
+1
View File
@@ -1,2 +1,3 @@
contributes:
sass: 'lib/_style.scss'
dart: 'lib/site.dart'
+3
View File
@@ -0,0 +1,3 @@
#privacy {
visibility: hidden;
}
+1 -1
View File
@@ -8,7 +8,7 @@ site:
github_repo: "https://github.com/simolus3/moor"
github_subdir: "docs/"
github_branch: master
# gcs_engine_id: "002567324444333206795:_yptu7lact8"
gcs_engine_id: set # this just needs to be set so that the search box shows
sidebar_search_disable: true
links: