-
Notifications
You must be signed in to change notification settings - Fork 285
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Algolia Search support (#329)
updates #222 Co-authored-by: reuixiy <[email protected]>
- Loading branch information
Showing
20 changed files
with
211 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
window.addEventListener("DOMContentLoaded", event => { | ||
let origContent = null; | ||
|
||
const search = instantsearch({ | ||
appId: "{{ .Site.Params.algoliaAppId }}", | ||
apiKey: "{{ .Site.Params.algoliaApiKey }}", | ||
indexName: "{{ .Site.Params.algoliaIndexName }}", | ||
}); | ||
|
||
search.addWidget({ | ||
init: function (opts) { | ||
const helper = opts.helper; | ||
const input = document.getElementById("search-input"); | ||
|
||
input.addEventListener("input", function (e) { | ||
helper | ||
.setQuery(e.currentTarget.value) // update the parameters | ||
.search(); // launch the query | ||
}); | ||
}, | ||
}); | ||
|
||
search.addWidget({ | ||
render: function (opts) { | ||
let term = opts.state.query | ||
if (!term) { | ||
return | ||
} | ||
|
||
const results = opts.results.hits; | ||
|
||
let target = document.querySelector(".main-inner") || document.querySelector("main.home"); | ||
let replaced = []; | ||
|
||
while (target.firstChild) { | ||
replaced.push(target.firstChild); | ||
target.removeChild(target.firstChild); | ||
} | ||
|
||
if (!origContent) { | ||
origContent = replaced; | ||
} | ||
|
||
let title = document.createElement("h1"); | ||
|
||
title.id = "search-results"; | ||
title.className = "list-title"; | ||
|
||
if (results.length == 0) { | ||
title.textContent = "{{ i18n "searchResultsNone" (dict "Term" "{}") }}".replace("{}", term); | ||
} else if (results.length == 1) { | ||
title.textContent = "{{ i18n "searchResultsTitle" (dict "Count" 1 "Term" "{}") }}".replace("{}", term); | ||
} else { | ||
title.textContent = "{{ i18n "searchResultsTitle" (dict "Count" 13579 "Term" "{}") }}".replace("{}", term).replace("13579", results.length); | ||
} | ||
|
||
target.appendChild(title); | ||
document.title = title.textContent; | ||
|
||
let template = document.getElementById("search-result"); | ||
for (let result of results) { | ||
let element = template.content.cloneNode(true); | ||
element.querySelector(".summary-title-link").href = element.querySelector(".read-more-link").href = result.url; | ||
element.querySelector(".summary-title-link").textContent = result.title; | ||
element.querySelector(".summary").textContent = truncateToEndOfSentence(result.summary, 70); | ||
target.appendChild(element); | ||
} | ||
title.scrollIntoView(true); | ||
|
||
{{ if .Site.Params.enableNavToggle }} | ||
let navToggleLabel = document.querySelector('.nav-toggle'); | ||
if (navToggleLabel && navToggleLabel.classList.contains("open")) { | ||
document.getElementById(navToggleLabel.getAttribute("for")).click(); | ||
} | ||
{{ end }} | ||
}, | ||
}); | ||
|
||
search.start(); | ||
|
||
// This matches Hugo's own summary logic: | ||
// https://github.com/gohugoio/hugo/blob/b5f39d23b86f9cb83c51da9fe4abb4c19c01c3b7/helpers/content.go#L543 | ||
function truncateToEndOfSentence(text, minWords) | ||
{ | ||
let match; | ||
let result = ""; | ||
let wordCount = 0; | ||
let regexp = /(\S+)(\s*)/g; | ||
while (match = regexp.exec(text)) { | ||
wordCount++; | ||
if (wordCount <= minWords) { | ||
result += match[0]; | ||
} | ||
else | ||
{ | ||
let char1 = match[1][match[1].length - 1]; | ||
let char2 = match[2][0]; | ||
if (/[.?!"]/.test(char1) || char2 == "\n") { | ||
result += match[1]; | ||
break; | ||
} | ||
else { | ||
result += match[0]; | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
}, {once: true}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{{- $.Scratch.Add "index" slice -}} | ||
{{- range (where .Site.RegularPages "Section" "in" .Site.Params.mainSections) -}} | ||
{{- $.Scratch.Add "index" (dict "objectID" .File.UniqueID "date" .Date.UTC.Unix "description" .Description "fuzzywordcount" .FuzzyWordCount "kind" .Kind "lang" .Lang "lastmod" .Lastmod.UTC.Unix "publishdate" .PublishDate.UTC.Unix "relpermalink" .RelPermalink "summary" .Summary "title" .Title "url" .Permalink "wordcount" .WordCount "section" .Section "tags" .Params.tags "categories" .Section "content" .Content)}} | ||
{{- end -}} | ||
{{- $.Scratch.Get "index" | jsonify -}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{{- $scripts := slice "https://cdn.jsdelivr.net/npm/instantsearch.js@2/dist/instantsearch.min.js" -}} | ||
|
||
{{- $scripts = union $scripts (slice "js/algolia-search.js") -}} | ||
{{- return $scripts -}} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.