@nera-static/plugin-search 1.0.2 → 1.0.3

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/CHANGELOG.md CHANGED
@@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [1.0.3] - 2025-07-25
9
+
10
+ ### Change
11
+ - Enhance search.js to show parts of the results text
12
+
8
13
  ## [1.0.2] - 2025-07-25
9
14
 
10
15
  ### Fix
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nera-static/plugin-search",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "A plugin for the Nera static site generator that builds a JSON search index and provides a client-side search script.",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/views/search.js CHANGED
@@ -12,21 +12,46 @@ document.addEventListener('DOMContentLoaded', () => {
12
12
  input.addEventListener('input', () => {
13
13
  const query = input.value.toLowerCase()
14
14
 
15
+ if (!query) {
16
+ resultList.innerHTML = ''
17
+ return
18
+ }
19
+
15
20
  const results = data.filter((item) => {
16
- return Object.values(item).some(val =>
17
- typeof val === 'string' && val.toLowerCase().includes(query)
18
- )
21
+ return Object.values(item).some(val => {
22
+ if (typeof val === 'string') {
23
+ const lowerVal = val.toLowerCase()
24
+ const matchIndex = lowerVal.indexOf(query)
25
+ if (matchIndex !== -1) {
26
+ const words = val.split(/\s+/)
27
+ const matchWordIndex = words.findIndex(word => word.toLowerCase().includes(query))
28
+
29
+ const snippetStart = Math.max(0, matchWordIndex - 5)
30
+ const snippetEnd = Math.min(words.length, matchWordIndex + 6)
31
+ const snippet = words.slice(snippetStart, snippetEnd).join(' ')
32
+
33
+ const highlighted = snippet.replace(
34
+ new RegExp(`(${query})`, 'ig'),
35
+ '<span class="bg-yellow-100">$1</span>'
36
+ )
37
+ item.resultText = `...${highlighted}...`
38
+ return true
39
+ }
40
+ }
41
+
42
+ return false
43
+ })
19
44
  })
20
45
 
21
46
  resultList.innerHTML = results.map((item) => {
22
47
  const title = item.title || item.href || 'Untitled'
23
- const desc = item.excerpt || item.description || ''
48
+ const desc = item.excerpt || item.description || item.resultText
24
49
  return `
25
- <li class="search__item">
26
- <a class="search__link" href="${item.href}">${title}</a>
27
- ${desc ? `<p class="search__description">${desc}</p>` : ''}
28
- </li>
29
- `
50
+ <li class="list-none">
51
+ <a class="" href="${item.href}">${title}</a>
52
+ ${desc ? `<p class="">${desc}</p>` : ''}
53
+ </li>
54
+ `
30
55
  }).join('')
31
56
  })
32
57
  })