Skip to content

Commit

Permalink
feat support search customization #79
Browse files Browse the repository at this point in the history
  • Loading branch information
apvarun committed Dec 25, 2022
1 parent 609a9ab commit 12d2173
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
4 changes: 4 additions & 0 deletions assets/css/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,8 @@
details.toc ul li a {
@apply no-underline text-gray-700 dark:text-gray-200 text-base;
}

a:empty {
display: none;
}
}
8 changes: 4 additions & 4 deletions assets/js/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function openSearch() {
loadSearch(); // loads our json data and builds fuse.js search index
firstRun = false; // let's never do this again
}

//Close the mobile menu when search is click.
mobileMenu.classList.toggle('hidden');

Expand Down Expand Up @@ -158,10 +158,10 @@ function executeSearch(term) {

for (let item in results.slice(0, 5)) { // only show first 5 results
const title = '<div class="text-2xl mb-2 font-bold">' + results[item].item.title + '</div>';
const date = results[item].item.date ? '<div><em class="">' + new Date(results[item].item.date).toUTCString().substring(0, 16) + '</em></div>' : '';
const contents = '<div>' + results[item].item.contents + '</div>';
const date = results[item].item.date ? '<div><em class="px-4">' + new Date(results[item].item.date).toUTCString().substring(0, 16) + '</em></div>' : '';
const contents = '<div class="prose px-4">' + results[item].item.contents + '</div>';

searchitems = searchitems + '<li><a class="block mb-2 px-4 py-2 rounded pb-2 border-b border-gray-200 dark:border-gray-600 focus:bg-gray-100 dark:focus:bg-gray-700 focus:outline-none" href="' + results[item].item.permalink + '" tabindex="0">' + title + date + contents + '</a></li>';
searchitems = searchitems + '<li><a class="block mb-2 px-4 py-2 rounded pb-2 border-b border-gray-200 dark:border-gray-600 focus:bg-gray-100 dark:focus:bg-gray-700 focus:outline-none" href="' + results[item].item.permalink + '" tabindex="0">' + title + '</a>' + date + contents + '</li>';
}
resultsAvailable = true;
}
Expand Down
14 changes: 14 additions & 0 deletions exampleSite/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,20 @@ DefaultContentLanguageInSubdir = true
# Enable search in header
enableSearch = true

# Option to customize the search parameters of a page
# Below are the supported options; Note that including entire content
# may slowdown the loading of search results
# Title of page is included by default
searchKeys = [
"tags",
"date",
"categories",
"summary",
"content",
"link",
"author"
]

# Custom copyright - optional
copyright = "Copyright © 2021 - Katheryn Fox · All rights reserved"
favicon = "/favicon.svg"
Expand Down
31 changes: 31 additions & 0 deletions layouts/_default/index.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,36 @@
{{- $.Scratch.Add "index" slice -}}
{{- range .Site.RegularPages -}}
{{- if .Site.Params.searchKeys -}}
{{ $page := . }}
{{ $dict := dict "title" $page.Title }}
{{- range .Site.Params.searchKeys -}}
{{- if (eq . "tags") -}}
{{ $dict = merge $dict (dict "tags" $page.Params.tags) }}
{{- end -}}
{{- if (eq . "date") -}}
{{ $dict = merge $dict (dict "date" $page.Params.Lastmod) }}
{{- end -}}
{{- if (eq . "categories") -}}
{{ $dict = merge $dict (dict "categories" $page.Params.categories) }}
{{- end -}}
{{- if (eq . "author") -}}
{{ $dict = merge $dict (dict "author" $page.Params.author) }}
{{- end -}}
{{- if (eq . "summary") -}}
{{ $dict = merge $dict (dict "contents" $page.Summary) }}
{{- end -}}
{{- if (eq . "contents") -}}
{{ $dict = merge $dict (dict "contents" $page.Content) }}
{{- end -}}
{{- if (eq . "link") -}}
{{ $dict = merge $dict (dict "permalink" $page.Permalink) }}
{{- end -}}

{{- end -}}
{{- $.Scratch.Add "index" $dict -}}
{{- else -}}
{{- $.Scratch.Add "index" (dict "title" .Title "tags" .Params.tags "date" .Params.Lastmod "categories" .Params.categories "contents" .Summary "permalink" .Permalink) -}}
{{- end -}}

{{- end -}}
{{- $.Scratch.Get "index" | jsonify -}}

1 comment on commit 12d2173

@sramekmichal
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it really works? I cannot see any difference :-(

Please sign in to comment.