search: make build and js forgiving against config errors #400

This commit is contained in:
Sören Weber 2022-11-20 23:00:40 +01:00
parent e738c3c0fd
commit 8aeb69fa56
No known key found for this signature in database
GPG key ID: BEC6D55545451B6D
3 changed files with 8 additions and 3 deletions

View file

@ -1,6 +1,8 @@
{{- $link := "" }} {{- $link := "" }}
{{- with .Site.Home.OutputFormats.Get "SEARCHPAGE" }} {{- if or (.Site.Home.OutputFormats.Get "JSON") (.Site.Home.OutputFormats.Get "SEARCH") }}
{{- $link = partial "relLangPrettyUglyURL.hugo" (dict "to" .) }} {{- with .Site.Home.OutputFormats.Get "SEARCHPAGE" }}
{{- $link = partial "relLangPrettyUglyURL.hugo" (dict "to" .) }}
{{- end }}
{{- end }} {{- end }}
{{- $c:="" }} {{ if $link }}<form action="{{ $link }}" method="get">{{ end }}<div class="searchbox default-animation"> {{- $c:="" }} {{ if $link }}<form action="{{ $link }}" method="get">{{ end }}<div class="searchbox default-animation">
{{ if $link }}<button type="submit" title="{{ T "Search" }} (CTRL+ALT+f)">{{ end }}<i class="fas fa-search"{{ if not $link }} title="{{ T "Search" }} (CTRL+ALT+f)"{{ end }}></i>{{ if $link }}</button>{{ end }} {{ if $link }}<button type="submit" title="{{ T "Search" }} (CTRL+ALT+f)">{{ end }}<i class="fas fa-search"{{ if not $link }} title="{{ T "Search" }} (CTRL+ALT+f)"{{ end }}></i>{{ if $link }}</button>{{ end }}

View file

@ -117,7 +117,7 @@ function search(term) {
// Find the item in our index corresponding to the lunr one to have more info // Find the item in our index corresponding to the lunr one to have more info
// Remove Lunr special search characters: https://lunrjs.com/guides/searching.html // Remove Lunr special search characters: https://lunrjs.com/guides/searching.html
var searchTerm = lunr.tokenizer(term.replace(/[*:^~+-]/, ' ')).reduce( function(a,token){return a.concat(searchPatterns(token.str))}, []).join(' '); var searchTerm = lunr.tokenizer(term.replace(/[*:^~+-]/, ' ')).reduce( function(a,token){return a.concat(searchPatterns(token.str))}, []).join(' ');
return !searchTerm ? [] : lunrIndex.search(searchTerm).map(function(result) { return !searchTerm || !lunrIndex ? [] : lunrIndex.search(searchTerm).map(function(result) {
return { index: result.ref, matches: Object.keys(result.matchData.metadata) } return { index: result.ref, matches: Object.keys(result.matchData.metadata) }
}); });
} }

View file

@ -523,6 +523,9 @@ function printShortcutHandler( event ){
function showSearch(){ function showSearch(){
var s = document.querySelector( '#search-by' ); var s = document.querySelector( '#search-by' );
if( !s ){
return;
}
var b = document.querySelector( 'body' ); var b = document.querySelector( 'body' );
if( s == document.activeElement ){ if( s == document.activeElement ){
if( b.classList.contains( 'sidebar-flyout' ) ){ if( b.classList.contains( 'sidebar-flyout' ) ){