astro-accelerator 0.0.101 → 0.0.102

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.0.101",
2
+ "version": "0.0.102",
3
3
  "author": "Steve Fenton",
4
4
  "name": "astro-accelerator",
5
5
  "description": "A super-lightweight, accessible, SEO-friendly starter project for Astro",
@@ -642,6 +642,12 @@ form.site-search button {
642
642
  font-family: var(--code-font);
643
643
  }
644
644
 
645
+ .show-more {
646
+ padding: 0.5em 1em;
647
+ background-color: var(--fore-link);
648
+ color: var(--aft-link-alt);
649
+ }
650
+
645
651
  @media (max-width: 860px) {
646
652
  form.site-search div {
647
653
  grid-template-columns: repeat(1, auto);
@@ -5,40 +5,52 @@ import { raiseEvent } from './modules/events.js';
5
5
  import { contains, containsWord, sanitise, explode, highlight } from './modules/string.js';
6
6
 
7
7
  /**
8
- type Heading = {
9
- text: string;
10
- safeText: string;
11
- slug: string;
12
- }
13
-
14
- type SearchEntry = {
15
- score: number;
16
- title: string;
17
- safeTitle: string;
18
- description: string;
19
- safeDescription: string;
20
- headings: Heading[];
21
- tags: string;
22
- url: string;
23
- date: string;
24
- matchedHeadings: Heading[];
25
- }
8
+ @typedef {
9
+ {
10
+ text: string;
11
+ safeText: string;
12
+ slug: string;
13
+ }
14
+ } Heading
15
+
16
+ @typedef {
17
+ {
18
+ foundWords: number;
19
+ score: number;
20
+ title: string;
21
+ keywords: string;
22
+ safeTitle: string;
23
+ description: string;
24
+ safeDescription: string;
25
+ headings: Heading[];
26
+ tags: string[];
27
+ url: string;
28
+ date: string;
29
+ matchedHeadings: Heading[];
30
+ }
31
+ } SearchEntry
26
32
  */
27
33
 
28
- var dataUrl = qs('#site-search').dataset.sourcedata;
29
- var haystack = /** @type {SearchEntry} */ [];
34
+ /** @type {SearchEntry[]} */
35
+ var haystack = [];
30
36
  var currentQuery = '';
37
+ var dataUrl = qs('#site-search').dataset.sourcedata;
31
38
 
32
39
  var ready = false;
33
40
  var scrolled = false;
34
41
 
35
42
  /**
36
- *
43
+ * Search term `s` and number of results `r`
37
44
  * @param {string} s
45
+ * @param {number|null} [r=12]
38
46
  * @returns
39
47
  */
40
- function search(s) {
41
- const needles = /** @type {SearchEntry} */ [];
48
+ function search(s, r) {
49
+ const numberOfResults = r ?? 12;
50
+ console.log('search', s, numberOfResults);
51
+
52
+ /** @type {SearchEntry[]} */
53
+ const needles = [];
42
54
 
43
55
  // Clean the input
44
56
  const cleanQuery = sanitise(s);
@@ -52,7 +64,7 @@ function search(s) {
52
64
  currentQuery = cleanQuery;
53
65
  const queryTerms = explode(currentQuery);
54
66
 
55
- s.length > 0 && haystack.forEach( (item) => {
67
+ cleanQuery.length > 0 && haystack.forEach( (item) => {
56
68
 
57
69
  let foundWords = 0;
58
70
  item.score = 0;
@@ -152,7 +164,7 @@ function search(s) {
152
164
  const ol = document.createElement('ol');
153
165
  ol.className = 'site-search-results';
154
166
 
155
- const limit = Math.min(needles.length, 12);
167
+ const limit = Math.min(needles.length, numberOfResults);
156
168
 
157
169
  // @ts-ignore
158
170
  const siteUrl = new URL(site_url);
@@ -178,7 +190,7 @@ function search(s) {
178
190
 
179
191
  const headings = document.createElement('ul');
180
192
  markers.className = 'result-headings';
181
- console.log(needle.matchedHeadings);
193
+
182
194
  needle.matchedHeadings
183
195
  .forEach(h => {
184
196
  const item = document.createElement('li');
@@ -204,14 +216,30 @@ function search(s) {
204
216
  ? results.dataset.emptytitle || 'No Results'
205
217
  : results.dataset.title || 'Results';
206
218
 
219
+ const more = document.createElement('button');
220
+ more.className = 'show-more';
221
+ more.type = 'button';
222
+ more.innerHTML = 'See more';
223
+ more.addEventListener('click', function() {
224
+ currentQuery = '';
225
+ const newTotal = numberOfResults + 12;
226
+ console.log('More', newTotal);
227
+ search(s, newTotal);
228
+ })
229
+
207
230
  results.innerHTML = '';
208
231
  results.appendChild(h2);
209
232
  results.appendChild(ol);
210
233
 
234
+ if (needles.length > numberOfResults) {
235
+ results.appendChild(more);
236
+ }
237
+
211
238
  const address = window.location.href.split('?')[0];
212
239
  window.history.pushState({}, '', address + '?q=' + encodeURIComponent(cleanQuery));
213
240
  }
214
241
 
242
+ /** @type {Number} */
215
243
  var debounceTimer;
216
244
 
217
245
  function debounceSearch() {
@@ -248,9 +276,11 @@ fetch(dataUrl)
248
276
  item.headings.forEach(h => h.safeText = sanitise(h.text));
249
277
  }
250
278
 
251
- var siteSearch = qs('#site-search');
279
+ /** @type {HTMLFormElement} */
280
+ const siteSearch = qs('#site-search');
252
281
 
253
- var siteSearchQuery = qs('#site-search-query');
282
+ /** @type {HTMLInputElement} */
283
+ const siteSearchQuery = qs('#site-search-query');
254
284
 
255
285
  if (siteSearch == null || siteSearchQuery == null) {
256
286
  throw new Error('Cannot find #site-search or #site-search-query');
@@ -276,7 +306,7 @@ fetch(dataUrl)
276
306
 
277
307
  const params = new URLSearchParams(window.location.search);
278
308
  if (params.has('q')) {
279
- siteSearchQuery.value = params.get('q');
309
+ siteSearchQuery.value = params.get('q') ?? '';
280
310
  }
281
311
 
282
312
  debounceSearch();