summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.md1
-rw-r--r--README.md1
-rw-r--r--assets/js/search.js31
-rw-r--r--exampleSite/content/about.md1
-rw-r--r--i18n/en.toml9
-rw-r--r--i18n/ru.toml9
-rw-r--r--layouts/partials/data.html6
-rw-r--r--layouts/partials/scripts.html5
-rw-r--r--layouts/partials/search-results.html2
-rw-r--r--package-lock.json10
-rw-r--r--theme.toml2
11 files changed, 57 insertions, 20 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 792d503..68725cd 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Provide template to render pure JSON of the site contents.
+- Provide search feature.
### Changed
diff --git a/README.md b/README.md
index 0a006d7..f1647ba 100644
--- a/README.md
+++ b/README.md
@@ -32,6 +32,7 @@ theme by [Alex Gil](https://twitter.com/elotroalex).
- Unobtrusive footnotes
- Metadata in OpenGraph to play nice with social media and search engines
- Automatic table of content generation
+- Simple search functionality
- Contact form
- Custom `robots.txt` (changes values based on environment)
- RSS/Atom/Json Feeds Discovery
diff --git a/assets/js/search.js b/assets/js/search.js
index d218127..9e25e16 100644
--- a/assets/js/search.js
+++ b/assets/js/search.js
@@ -1,6 +1,6 @@
'use strict';
-let pagesIndex, searchIndex;
+let config, pagesIndex, searchIndex;
// Maximum length (in words) of each text blurb. You can change this
// value if you find that 100 is too short or too long for your taste.
@@ -17,10 +17,28 @@ const SENTENCE_BOUNDARY_REGEX = /\b\.\s/gm;
// in the blurb as it is being built.
const WORD_REGEX = /\b(\w*)[\W|\s|\b]?/gm;
+function initConfig() {
+ const defaults = {
+ strings: {
+ searchEnterTerm: 'Please enter a search term.',
+ searchNoResults: 'No results found.'
+ }
+ };
+
+ try {
+ const config = JSON.parse($('#ed-data').html());
+ return Object.assign(config, defaults);
+ } catch (e) {
+ return defaults;
+ }
+}
+
async function initSearchIndex() {
try {
const response = await fetch('/index.json');
pagesIndex = await response.json();
+
+ // Create the lunr index for the search
searchIndex = lunr(function () { // eslint-disable-line no-undef
this.field('title');
this.field('categories');
@@ -39,14 +57,15 @@ function handleSearchQuery(event) {
event.preventDefault();
const query = $('#search').val().trim().toLowerCase();
+
if (!query) {
- displayErrorMessage('Please enter a search term.');
+ displayErrorMessage(config.strings.searchEnterTerm);
return;
}
const results = searchSite(query);
if (!results.length) {
- displayErrorMessage('No results found.');
+ displayErrorMessage(config.strings.searchNoResults);
return;
}
@@ -128,7 +147,6 @@ function renderSearchResults(query, results) {
function clearSearchResults() {
$('#search-results-body').empty();
$('#results-count').empty();
- $('#results-count-text').empty();
}
function clearAndFocusSearchInput() {
@@ -148,9 +166,7 @@ function updateSearchResults(query, results) {
`
).join(''));
- const searchResultListItems = $('#search-results-body article');
- $('#results-count').html(searchResultListItems.length);
- $('#results-count-text').html(searchResultListItems.length > 1 ? 'results' : 'result');
+ $('#results-count').html($('#search-results-body article').length);
}
function createSearchResultBlurb(query, pageContent) {
@@ -287,6 +303,7 @@ if (!String.prototype.matchAll) {
$(function() {
const searchForm = $('#search-form');
const searchInput = $('#search');
+ config = initConfig();
if (searchForm.length === 0 || searchInput.length === 0) {
return;
diff --git a/exampleSite/content/about.md b/exampleSite/content/about.md
index 1142466..b624677 100644
--- a/exampleSite/content/about.md
+++ b/exampleSite/content/about.md
@@ -45,6 +45,7 @@ for you, Kindle!
- Unobtrusive footnotes
- Metadata in OpenGraph to play nice with social media and search engines
- Automatic table of content generation
+- Simple search functionality
- Contact form
- Custom `robots.txt` (changes values based on environment)
- RSS/Atom/Json Feeds Discovery
diff --git a/i18n/en.toml b/i18n/en.toml
index d892a10..f65d31f 100644
--- a/i18n/en.toml
+++ b/i18n/en.toml
@@ -60,3 +60,12 @@
[clear_search_results]
other = 'New Search'
+
+[search_enter_term]
+ other = 'Please enter a search term.'
+
+[search_no_results]
+ other = 'No results found.'
+
+[search_results]
+ other = 'Found results:'
diff --git a/i18n/ru.toml b/i18n/ru.toml
index 23ecf30..1bbb38e 100644
--- a/i18n/ru.toml
+++ b/i18n/ru.toml
@@ -60,3 +60,12 @@
[clear_search_results]
other = 'Новый поиск'
+
+[search_enter_term]
+ other = 'Введите поисковый запрос.'
+
+[search_no_results]
+ other = 'Ничего не найдено.'
+
+[search_results]
+ other = 'Найдено результатов:'
diff --git a/layouts/partials/data.html b/layouts/partials/data.html
index afacc83..cdd445c 100644
--- a/layouts/partials/data.html
+++ b/layouts/partials/data.html
@@ -2,6 +2,10 @@
{
"analytics_code": {{ site.GoogleAnalytics | default "" }},
"page_title": {{- partial "title.html" . -}},
- "language": {{ site.LanguageCode | default site.Language.Lang }}
+ "language": {{ site.LanguageCode | default site.Language.Lang }},
+ "strings": {
+ "searchEnterTerm": {{ i18n "search_enter_term" }},
+ "searchNoResults": {{ i18n "search_no_results" }}
+ }
}
</script>
diff --git a/layouts/partials/scripts.html b/layouts/partials/scripts.html
index 02634ad..1574f7e 100644
--- a/layouts/partials/scripts.html
+++ b/layouts/partials/scripts.html
@@ -17,7 +17,10 @@
<script src="{{ $vendors.RelPermalink }}"></script>
{{- end -}}
-{{- $search := resources.Get "js/search.js" | minify -}}
+{{- $search := resources.Get "js/search.js" -}}
+{{- if or (eq (getenv "HUGO_ENV") "production") (eq site.Params.env "production") }}
+ {{- $search = $search | minify -}}
+{{- end -}}
{{- if not site.Params.assets.disable_fingerprinting -}}
{{- $search = $search | fingerprint -}}
<script src="{{ $search.RelPermalink }}" integrity="{{ $search.Data.Integrity }}"></script>
diff --git a/layouts/partials/search-results.html b/layouts/partials/search-results.html
index f9059a3..fd4c48d 100644
--- a/layouts/partials/search-results.html
+++ b/layouts/partials/search-results.html
@@ -1,8 +1,8 @@
<section id="search-results" class="hide-element">
<div id="search-results-header">
<div class="search-query search-query-right">
+ <span id="results-count-text">{{ i18n "search_results" }}</span>
<span id="results-count"></span>
- <span id="results-count-text"></span>
</div>
</div>
<div id="search-results-body" class="post-list"></div>
diff --git a/package-lock.json b/package-lock.json
index 3e06963..f68f7d8 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -491,11 +491,6 @@
"integrity": "sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==",
"dev": true
},
- "jquery": {
- "version": "3.6.0",
- "resolved": "https://registry.npmjs.org/jquery/-/jquery-3.6.0.tgz",
- "integrity": "sha512-JVzAR/AjBvVt2BmYhxRCSYysDsPcssdmTFnzyLEts9qNwmjmu4JTAMYubEfwVOSwpQ1I1sKKFcxhZCI2buerfw=="
- },
"js-yaml": {
"version": "4.1.0",
"resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-4.1.0.tgz",
@@ -533,11 +528,6 @@
"integrity": "sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==",
"dev": true
},
- "lunr": {
- "version": "2.3.9",
- "resolved": "https://registry.npmjs.org/lunr/-/lunr-2.3.9.tgz",
- "integrity": "sha512-zTU3DaZaF3Rt9rhN3uBMGQD3dD2/vFQqnvZCDv4dl5iOzq2IZQqTxu90r4E5J+nP70J3ilqVCrbho2eWaeW8Ow=="
- },
"minimatch": {
"version": "3.1.2",
"resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz",
diff --git a/theme.toml b/theme.toml
index c661f3b..09301a9 100644
--- a/theme.toml
+++ b/theme.toml
@@ -19,6 +19,7 @@ tags = [
"minimalist",
"multilingual",
"responsive",
+ "search",
"simple"
]
@@ -37,6 +38,7 @@ features = [
"posts",
"responsive",
"rss",
+ "search",
"shortcodes",
"simple"
]