@nera-static/plugin-search 1.0.1 → 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,16 @@ 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
+
13
+ ## [1.0.2] - 2025-07-25
14
+
15
+ ### Fix
16
+ - publish serach.js with publish-template command
17
+
8
18
  ## [1.0.1] - 2025-07-25
9
19
 
10
20
  ### Added
@@ -1,6 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  import path from 'path'
4
+ import fs from 'fs/promises'
4
5
  import { fileURLToPath } from 'url'
5
6
  import { publishAllTemplates } from '@nera-static/plugin-utils'
6
7
 
@@ -8,10 +9,29 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url))
8
9
  const pluginName = 'plugin-search'
9
10
  const sourceDir = path.resolve(__dirname, '../views/')
10
11
 
12
+ // Publish all pug templates to views/vendor/plugin-search/
11
13
  const result = publishAllTemplates({
12
14
  pluginName,
13
15
  sourceDir,
14
16
  expectedPackageName: 'dummy', // for test-only override
15
17
  })
16
18
 
17
- process.exit(result ? 0 : 1)
19
+ // Also publish search.js into assets/js/
20
+ const publishSearchJS = async () => {
21
+ try {
22
+ const jsSource = path.join(sourceDir, 'search.js')
23
+ const jsTarget = path.resolve(process.cwd(), 'assets/js/search.js')
24
+
25
+ await fs.mkdir(path.dirname(jsTarget), { recursive: true })
26
+ await fs.copyFile(jsSource, jsTarget)
27
+
28
+ console.log('✓ search.js copied to assets/js/search.js')
29
+ } catch (err) {
30
+ console.error('✗ Failed to copy search.js to assets/js/', err)
31
+ process.exit(1)
32
+ }
33
+ }
34
+
35
+ publishSearchJS().then(() => {
36
+ process.exit(result ? 0 : 1)
37
+ })
package/index.js CHANGED
@@ -1,7 +1,6 @@
1
1
  import path from 'path'
2
2
  import fs from 'fs/promises'
3
3
  import { getConfig } from '@nera-static/plugin-utils'
4
- import { fileURLToPath } from 'url'
5
4
 
6
5
  const CONFIG_PATH = path.resolve(process.cwd(), 'config/search.yaml')
7
6
 
@@ -31,13 +30,8 @@ export async function getAppData({ app, pagesData }) {
31
30
  return item
32
31
  })
33
32
 
34
- fs.writeFile(outputPath, JSON.stringify(index, null, 2), 'utf-8')
35
-
36
- const __dirname = path.dirname(fileURLToPath(import.meta.url))
37
- const sourceJS = path.join(__dirname, 'views', 'search.js')
38
- const targetJS = path.join(app.folders.assets, 'js', 'search.js')
39
- await fs.mkdir(path.dirname(targetJS), { recursive: true })
40
- await fs.copyFile(sourceJS, targetJS)
33
+ await fs.mkdir(path.dirname(outputPath), { recursive: true })
34
+ await fs.writeFile(outputPath, JSON.stringify(index, null, 2), 'utf-8')
41
35
 
42
36
  return {
43
37
  ...app,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nera-static/plugin-search",
3
- "version": "1.0.1",
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
  })