Skip to content

Commit 0bb2453

Browse files
committed
refactor(search): refactor search logic to query across distinct content sections #7
1 parent 789e610 commit 0bb2453

File tree

4 files changed

+63
-17
lines changed

4 files changed

+63
-17
lines changed

assets/js/search.js

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
const dataJSON = '{{ $searchData.RelPermalink }}'
77
const input = document.getElementById('search-input')
88
const results = document.getElementById('search-results')
9+
const currentVersion = document.getElementById('current-version')
910

1011
// eslint-disable-next-line no-undef
1112
const index = FlexSearch.Index({
@@ -16,6 +17,21 @@ const index = FlexSearch.Index({
1617
// Object to store documents and sections
1718
const documents = {}
1819

20+
function getCurrentVersion () {
21+
if (currentVersion) {
22+
return currentVersion.innerText.trim()
23+
}
24+
25+
const path = window.location.pathname.split('/')
26+
const version = path[1]
27+
28+
if (version && version.length > 0) {
29+
return version
30+
}
31+
32+
return null
33+
}
34+
1935
// Main function to initialize the search
2036
async function initSearch () {
2137
input.removeEventListener('focus', initSearch)
@@ -55,8 +71,15 @@ async function search () {
5571
if (!input.value) return
5672

5773
try {
58-
const hits = await index.searchAsync(input.value, 10)
59-
const groupedHits = groupResultsByParent(hits)
74+
const hits = await index.searchAsync(input.value, 100)
75+
76+
const currentVersion = getCurrentVersion()
77+
const filteredHits = hits.filter(hitId => {
78+
const page = documents.pages.find(doc => doc.id === hitId)
79+
return page && page.parent.startsWith(`/${currentVersion}/`)
80+
})
81+
82+
const groupedHits = groupResultsByParent(filteredHits)
6083

6184
// Display grouped results
6285
displayGroupedResults(groupedHits)
@@ -157,7 +180,11 @@ function stringToHTML (str) {
157180
// Function to initialize search events when the input is focused or when typing
158181
function initSearchEvents () {
159182
input.addEventListener('focus', initSearch)
160-
input.addEventListener('keyup', search)
183+
input.addEventListener('keyup', (event) => {
184+
if (event.key.length === 1 || event.key === 'Backspace' || event.key === 'Delete') {
185+
search()
186+
}
187+
})
161188
}
162189

163190
// Initialize the search events

assets/json/index.json

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{{- $.Scratch.Add "index" slice -}}
22
{{- $.Scratch.Add "sections" (dict) -}}
33
{{- $section := $.Site.GetPage .Section }}
4-
{{- $regex := "/(?:[a-z]{2}/)?[^/]+/([^/]+)/.*$" }}
4+
{{- $regex := "^(/[^/]+/[^/]+/)(.*)$" -}}
5+
{{- $langPrefix := "^/[a-z]{2}/" -}}
56

67
{{- range $index, $page := .Site.Pages -}}
7-
{{- if or (and (.IsDescendant $section) (and (not .Draft) (not .Params.hidden))) $section.IsHome -}}
8-
{{- if .File -}}
9-
10-
{{- $parentSection := (replaceRE $regex "$1" $page.RelPermalink) -}}
8+
{{- if .File -}}
9+
{{- $cleanPath := replaceRE $langPrefix "/" $page.RelPermalink -}}
10+
{{- $parentSection := (replaceRE $regex "$1" $cleanPath) -}}
1111
{{- $parentPage := $.Site.GetPage $parentSection -}}
1212
{{- $parentIcon := $parentPage.Params.icon | default "bookmark" -}}
1313
{{- $parentIconSVG := (resources.Get (printf "icons/%s.svg" $parentIcon)) | default "" -}}
@@ -26,12 +26,10 @@
2626
{{- $.Scratch.Add "index" (dict
2727
"id" $index
2828
"url" $page.RelPermalink
29-
"summary" (truncate 200 $page.Plain)
29+
"summary" (truncate 120 $page.Plain)
3030
"title" $page.Title
3131
"parent" $parentSection
3232
) -}}
33-
34-
{{- end -}}
3533
{{- end -}}
3634
{{- end -}}
3735

assets/scss/components/_search.scss

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@
1111
pointer-events: none;
1212
}
1313

14-
&-toggle span {
15-
@include breakpoint(l, max) {
16-
display: none;
14+
&-toggle {
15+
outline: none;
16+
17+
span {
18+
@include breakpoint(l, max) {
19+
display: none;
20+
}
1721
}
1822
}
1923

@@ -34,11 +38,22 @@
3438
&-form {
3539
padding: var(--search-padding);
3640
border-bottom: 1px solid var(--color-border);
37-
display: flex;
3841
font-size: 1rem;
39-
align-items: center;
4042
}
4143

44+
&-footer {
45+
padding: var(--search-padding);
46+
border-top: 1px solid var(--color-border);
47+
gap: 1rem;
48+
49+
kbd {
50+
margin-right: .25rem;
51+
font-size: .75rem;
52+
}
53+
}
54+
55+
&-version {}
56+
4257
&-icon {
4358
flex: none;
4459
margin-right: 1rem;

layouts/partials/search.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<div class="search" id="search-box">
22
<div class="search-content">
3-
<div class="search-form">
3+
<div class="search-form flex items-center ">
44
<label class="search-icon">{{ partial "svg" (dict "icon" "search") }}</label>
55
<input class="search-input" id="search-input" type="text" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" placeholder='{{- i18n "search_in_doc" -}}' maxlength="64"/>
66
<button data-close="" aria-label='{{ i18n "close" }}' class="search-close">
@@ -10,6 +10,12 @@
1010
<div class="search-results">
1111
<div class="search-list" id="search-results"></div>
1212
</div>
13+
<div class="search-footer flex items-center">
14+
<span class="search-keyboard">
15+
<kbd>ESC</kbd>
16+
{{ i18n "to_close" }}
17+
</span>
18+
</div>
1319
</div>
1420
</div>
1521
<div class="overlay"></div>

0 commit comments

Comments
 (0)