diff options
Diffstat (limited to 'layouts')
-rw-r--r-- | layouts/_default/list.html | 11 | ||||
-rw-r--r-- | layouts/_default/search.html | 19 | ||||
-rw-r--r-- | layouts/_default/search.json.json | 82 | ||||
-rw-r--r-- | layouts/partials/search-form.html | 18 |
4 files changed, 128 insertions, 2 deletions
diff --git a/layouts/_default/list.html b/layouts/_default/list.html index 3070079..492b335 100644 --- a/layouts/_default/list.html +++ b/layouts/_default/list.html @@ -2,8 +2,16 @@ {{ $currentTitle := .Scratch.Get "currentTitle" }} - <main class="list"> + <!-- Collect pages using the "search" layout --> + {{ $searchPages := where .Site.RegularPages "Layout" "eq" "search" }} + <main class="list"> + + <!-- Display search form before listing posts --> + {{ if and .Site.Params.Search.enable (gt (len $searchPages) 0) (eq .Kind "section") }} + {{ partial "search-form" . }} + {{ end }} + <h1>{{ $currentTitle }}</h1> {{ if eq .Kind "taxonomyTerm" }} @@ -15,7 +23,6 @@ {{ else }} {{ $pages := where site.RegularPages "Type" "in" .Site.Params.mainSections }} - {{ $paginator := .Paginate .Pages }} <ul class="posts"> diff --git a/layouts/_default/search.html b/layouts/_default/search.html new file mode 100644 index 0000000..851f6bf --- /dev/null +++ b/layouts/_default/search.html @@ -0,0 +1,19 @@ +{{ define "main" }} + + <main class="list"> + + {{ partial "search-form" . }} + + <section id="search-info"> + <!-- Info --> + </section> + + <ul id="search-results" class="posts"> + <!-- x-js-template --> + </ul> + + <script id=search-result-template type=text/x-js-template><li><a class=btn href='${link}'><p>${title}</p>${date}</a></li></script> + + </main> + +{{ end }} diff --git a/layouts/_default/search.json.json b/layouts/_default/search.json.json new file mode 100644 index 0000000..279aeb2 --- /dev/null +++ b/layouts/_default/search.json.json @@ -0,0 +1,82 @@ +{{- $stopWords := or .Params.stopWords .Site.Params.Search.stopWords -}} + +{{- $pages := where .Site.RegularPages "Type" "in" .Site.Params.mainSections -}} + +{{- $lastIndex := (sub (len $pages) 1) -}} + +{{- $.Scratch.Add "index" slice -}} + +{{- range $index, $page := $pages -}} + + {{- $.Scratch.Delete "date" -}} + {{- $.Scratch.Delete "description" -}} + {{- $.Scratch.Delete "tags" -}} + + {{- if not .Date.IsZero -}} + {{- $.Scratch.Set "date" (.Date.Format "2006-01-02") -}} + {{- end -}} + + {{- if .Description -}} + {{- $.Scratch.Set "description" (.Description | emojify) -}} + {{- end -}} + + {{- if .Params.tags -}} + {{- $.Scratch.Set "tags" .Params.tags -}} + {{- end -}} + + {{- $scratch := newScratch -}} + + {{- $content := (.Content | replaceRE "(?s)<div class=\"highlight\".*?</div>" "" ) -}} + {{- $content := ($content | replaceRE "(?s)<p class=\"error\".*?</p>" "" ) -}} + {{- $content := ($content | replaceRE "<code.*?</code>" "" ) -}} + {{- $content := ($content | replaceRE "(http|https)\\S+" "" ) -}} + {{- $content := ($content | replaceRE "<!--(.|\n)+?-->" "" ) -}} + {{- $content := ($content | plainify | emojify | lower) -}} + {{- $content := (replaceRE "\"" "" $content) -}} + {{- $content := (replaceRE "“" "" $content) -}} + {{- $content := (replaceRE "”" "" $content) -}} + {{- $content := (replaceRE "\n+" " " $content) -}} + {{- $content := (replaceRE "\r+" " " $content) -}} + {{- $content := (replaceRE "\t+" " " $content) -}} + {{- $content := (replaceRE "<" "" $content) -}} + {{- $content := (replaceRE ">" "" $content) -}} + {{- $content := (replaceRE "…" "" $content) -}} + {{- $content := (replaceRE "\\(" "" $content) -}} + {{- $content := (replaceRE "\\)" "" $content) -}} + {{- $content := (replaceRE "\\. " " " $content) -}} + {{- $content := (replaceRE "\\," "" $content) -}} + {{- $content := (replaceRE "\\:" "" $content) -}} + {{- $content := (replaceRE "\\;" "" $content) -}} + {{- $content := (replaceRE "\\!" "" $content) -}} + {{- $content := (replaceRE "\\?" "" $content) -}} + {{- $content := (replaceRE "\\/" " " $content) -}} + {{- $content := (replaceRE " +" " " $content) -}} + {{- $content := (trim $content " ") -}} + {{- $words := (split $content " ") -}} + + + {{- range $words -}} + {{- $word := (.) -}} + {{- if and (not (in ($scratch.Get "seenWords") $word)) (ne (substr $word 0 1) $word) (not (in $stopWords $word)) -}} + {{- $.Scratch.Add "currentWords" (slice $word) -}} + {{- end -}} + {{- $scratch.Add "seenWords" (slice $word) -}} + {{- end -}} + + {{- $scratch.Delete "seenWords" -}} + + + {{- $filtered := ($.Scratch.Get "currentWords") -}} + + {{- if $filtered -}} + {{- $.Scratch.Set "uniqueContent" (delimit $filtered " ") -}} + {{- end -}} + + + {{- $.Scratch.Add "index" (dict "title" (.Title | emojify) "date" ($.Scratch.Get "date") "description" ($.Scratch.Get "description") "content" ($.Scratch.Get "uniqueContent") "permalink" .Permalink "tags" ($.Scratch.Get "tags")) -}} + + {{- $.Scratch.Delete "currentWords" -}} + +{{- end -}} + +{{- $.Scratch.Get "index" | jsonify -}} diff --git a/layouts/partials/search-form.html b/layouts/partials/search-form.html new file mode 100644 index 0000000..cea29c8 --- /dev/null +++ b/layouts/partials/search-form.html @@ -0,0 +1,18 @@ +{{ $icons := (.Scratch.Get "svgBundle").RelPermalink }} + +<!-- + Assume that the first page in a collection with pages + using the "search" layout is the main search page +--> +{{ $searchPage := index (where .Site.RegularPages "Layout" "eq" "search") 0 }} + +<section class="req-js search-box"> + {{ if eq .Layout "search" }} + <form role="search"> + {{ else }} + <form role="search" action="{{ $searchPage.RelPermalink }}"> + {{ end }} + <label for="search">{{ T "search_label" }}</label><input type="search" id="search" title='{{ T "search_input_title" }}' name="q" {{ with .Site.Params.Search.placeholder }}placeholder="{{ . }}"{{ end }} minlength="3" maxlength="32" pattern="^[\S].*" required><button aria-label='{{ T "search_button_aria_label" }}' type="submit"><svg aria-hidden="true"><use xlink:href="{{ $icons }}#search"/></svg></button> + </form> + <hr> +</section> |