@nera-static/plugin-search 1.0.0

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 ADDED
@@ -0,0 +1,21 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [1.0.0] - 2025-07-25
9
+
10
+ ### Added
11
+
12
+ - Initial release of `@nera-static/plugin-search`
13
+ - Generates a `search-index.json` file from page metadata and content
14
+ - Supports configurable fields via `config/search.yaml`
15
+ - Option to strip HTML tags from content before indexing
16
+ - Auto-injects `search.js` script to `assets/js/search.js`
17
+ - Simple search interface and results rendering via `views/search.pug`
18
+ - Allows multiple search inputs per page via `[data-search-input]` selectors
19
+ - Provides `publish-template` command to copy `search.pug` for customization
20
+ - Comprehensive test suite to validate index generation and file handling
21
+
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Michael Becker
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,159 @@
1
+ # @nera-static/plugin-search
2
+
3
+ A plugin for the [Nera](https://github.com/seebaermichi/nera) static site generator that adds a lightweight client-side search functionality. It builds a searchable JSON index from your content and provides a ready-to-use search interface – all without requiring a backend.
4
+
5
+ ## ✨ Features
6
+
7
+ - Creates a `search-index.json` from page content and metadata
8
+ - Fully client-side – no backend or JavaScript framework needed
9
+ - Configurable search fields (e.g., `title`, `excerpt`, `description`)
10
+ - Option to strip HTML from content before indexing
11
+ - Auto-includes search script in assets
12
+ - Includes ready-to-use Pug template with BEM-compatible markup
13
+ - Supports multiple search inputs per page
14
+ - Full compatibility with Nera v4.2.0+
15
+
16
+ ## 🚀 Installation
17
+
18
+ Install the plugin in your Nera project:
19
+
20
+ ```bash
21
+ npm install @nera-static/plugin-search
22
+ ```
23
+
24
+ The plugin is automatically detected and run during the render process.
25
+
26
+ ## ⚙️ Configuration
27
+
28
+ Configure the plugin via `config/search.yaml` (optional):
29
+
30
+ ```yaml
31
+ output_filename: search-index.json
32
+
33
+ fields:
34
+ - title
35
+ - excerpt
36
+ - content
37
+ - description
38
+ - href
39
+
40
+ strip_html: true
41
+ ```
42
+
43
+ ### Field Notes
44
+
45
+ - `fields`: List of metadata fields to include in the index.
46
+ - `content`: Always pulled from the markdown content itself.
47
+ - `strip_html`: If `true`, HTML is removed from content before indexing.
48
+
49
+ ## 🧩 Usage
50
+
51
+ ### Add Search Page
52
+
53
+ ```yaml
54
+ ---
55
+ title: Search
56
+ layout: pages/default.pug
57
+ ---
58
+ ```
59
+
60
+ ```pug
61
+ .section.search
62
+ h1.search__title Search
63
+
64
+ input.search__input(
65
+ type="search",
66
+ placeholder="Search...",
67
+ data-search-input,
68
+ data-results="[data-search__results]"
69
+ )
70
+
71
+ ul.search__results(data-search__results)
72
+ script(src="/js/search.js")
73
+ ```
74
+
75
+ The plugin will create `assets/js/search.js` and inject the `search-index.json` into your `/public` output.
76
+
77
+ You can include multiple search inputs on a page by using different `[data-results]` selectors.
78
+
79
+ ### Example Search Result Output
80
+
81
+ ```html
82
+ <li class="search__item">
83
+ <a class="search__link" href="${item.href}">${title}</a>
84
+ ${desc ? `<p class="search__description">${desc}</p>` : ''}
85
+ </li>
86
+ ```
87
+
88
+ ## 🛠️ Template Publishing
89
+
90
+ To customize the default view:
91
+
92
+ ```bash
93
+ npx @nera-static/plugin-search run publish-template
94
+ ```
95
+
96
+ This will copy:
97
+
98
+ ```
99
+ views/vendor/plugin-search/search.pug
100
+ ```
101
+
102
+ to your local project. You can now edit or extend the search markup freely.
103
+
104
+ The plugin also automatically copies `assets/search-content.json` to `assets/js/search.js` on render. This file handles DOM bindings and result generation.
105
+
106
+ ## 🎨 Styling
107
+
108
+ Default template uses semantic HTML and includes minimal structure. Recommended BEM class names:
109
+
110
+ - `.search`
111
+ - `.search__input`
112
+ - `.search__results`
113
+ - `.search__item`
114
+ - `.search__title`
115
+ - `.search__description`
116
+
117
+ Customize freely via your own stylesheets.
118
+
119
+ ## 📊 Generated Output
120
+
121
+ - `assets/search-index.json`: contains all indexed page data
122
+ - `assets/js/search.js`: minimal client-side logic for filtering and rendering
123
+ - `app.searchIndexPath`: injected into `app` data for template access
124
+
125
+ ## 🧪 Development
126
+
127
+ ```bash
128
+ npm install
129
+ npm test
130
+ npm run lint
131
+ ```
132
+
133
+ Tests use [Vitest](https://vitest.dev) and cover:
134
+
135
+ - Index creation from `pagesData`
136
+ - Correct JSON structure and file writing
137
+ - `search.js` copy to assets folder
138
+ - Config-driven field control and HTML stripping
139
+
140
+ ## 🧑‍💻 Author
141
+
142
+ Michael Becker
143
+ [https://github.com/seebaermichi](https://github.com/seebaermichi)
144
+
145
+ ## 🔗 Links
146
+
147
+ - [Plugin Repository](https://github.com/seebaermichi/nera-plugin-search)
148
+ - [NPM Package](https://www.npmjs.com/package/@nera-static/plugin-search)
149
+ - [Nera Static Site Generator](https://github.com/seebaermichi/nera)
150
+
151
+ ## 🧩 Compatibility
152
+
153
+ - **Nera**: v4.2.0+
154
+ - **Node.js**: >= 18
155
+ - **Plugin API**: Uses `getAppData()` for index creation and asset copying
156
+
157
+ ## 📦 License
158
+
159
+ MIT
@@ -0,0 +1,7 @@
1
+ fields:
2
+ - title
3
+ - excerpt
4
+ - content
5
+ - href
6
+ strip_html: true
7
+ output_filename: search-index.json
package/index.js ADDED
@@ -0,0 +1,46 @@
1
+ import path from 'path'
2
+ import fs from 'fs/promises'
3
+ import { getConfig } from '@nera-static/plugin-utils'
4
+ import { fileURLToPath } from 'url'
5
+
6
+ const CONFIG_PATH = path.resolve(process.cwd(), 'config/search.yaml')
7
+
8
+ function stripHtml(html) {
9
+ return html.replace(/<[^>]*>/g, '').replace(/\s+/g, ' ').trim()
10
+ }
11
+
12
+ export async function getAppData({ app, pagesData }) {
13
+ const config = getConfig(CONFIG_PATH)
14
+ const filename = config.output_filename || 'search-index.json'
15
+ const outputPath = path.join(app.folders.assets, filename)
16
+
17
+ const fields = config.fields || ['title', 'excerpt', 'content', 'href']
18
+ const strip = config.strip_html ?? true
19
+
20
+ const index = pagesData.map(({ meta, content }) => {
21
+ const item = {}
22
+
23
+ for (const field of fields) {
24
+ if (field === 'content') {
25
+ item.content = strip ? stripHtml(content) : content
26
+ } else if (meta[field]) {
27
+ item[field] = meta[field]
28
+ }
29
+ }
30
+
31
+ return item
32
+ })
33
+
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)
41
+
42
+ return {
43
+ ...app,
44
+ searchIndexPath: `/${filename}`
45
+ }
46
+ }
package/package.json ADDED
@@ -0,0 +1,57 @@
1
+ {
2
+ "name": "@nera-static/plugin-search",
3
+ "version": "1.0.0",
4
+ "description": "A plugin for the Nera static site generator that builds a JSON search index and provides a client-side search script.",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "publish-template": "node ./bin/publish-template.js",
8
+ "lint": "eslint .",
9
+ "test": "vitest"
10
+ },
11
+ "type": "module",
12
+ "exports": {
13
+ ".": "./index.js"
14
+ },
15
+ "bin": {
16
+ "nera-search": "./bin/publish-template.js"
17
+ },
18
+ "files": [
19
+ "bin",
20
+ "config",
21
+ "views",
22
+ "index.js",
23
+ "CHANGELOG.md"
24
+ ],
25
+ "keywords": [
26
+ "nera",
27
+ "plugin",
28
+ "search",
29
+ "static-site",
30
+ "client-side-search",
31
+ "search-index",
32
+ "lunr",
33
+ "json"
34
+ ],
35
+ "author": "Michael Becker",
36
+ "license": "MIT",
37
+ "repository": {
38
+ "type": "git",
39
+ "url": "https://github.com/seebaermichi/nera-plugin-search.git"
40
+ },
41
+ "bugs": {
42
+ "url": "https://github.com/seebaermichi/nera-plugin-search/issues"
43
+ },
44
+ "homepage": "https://github.com/seebaermichi/nera-plugin-search#readme",
45
+ "dependencies": {
46
+ "@nera-static/plugin-utils": "^1.1.0"
47
+ },
48
+ "engines": {
49
+ "node": ">=18"
50
+ },
51
+ "devDependencies": {
52
+ "eslint": "^9.31.0",
53
+ "husky": "^9.1.7",
54
+ "pug": "^3.0.3",
55
+ "vitest": "^3.2.4"
56
+ }
57
+ }
@@ -0,0 +1,34 @@
1
+ document.addEventListener('DOMContentLoaded', () => {
2
+ const inputs = document.querySelectorAll('[data-search-input]')
3
+
4
+ fetch('/search-index.json')
5
+ .then((res) => res.json())
6
+ .then((data) => {
7
+ inputs.forEach((input) => {
8
+ const resultSelector = input.getAttribute('data-results')
9
+ const resultList = document.querySelector(resultSelector)
10
+ if (!resultList) return
11
+
12
+ input.addEventListener('input', () => {
13
+ const query = input.value.toLowerCase()
14
+
15
+ const results = data.filter((item) => {
16
+ return Object.values(item).some(val =>
17
+ typeof val === 'string' && val.toLowerCase().includes(query)
18
+ )
19
+ })
20
+
21
+ resultList.innerHTML = results.map((item) => {
22
+ const title = item.title || item.href || 'Untitled'
23
+ const desc = item.excerpt || item.description || ''
24
+ 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
+ `
30
+ }).join('')
31
+ })
32
+ })
33
+ })
34
+ })
@@ -0,0 +1,13 @@
1
+ .section.search
2
+ h1.search__title Search
3
+
4
+ input.search__input(
5
+ type="search",
6
+ placeholder="Search...",
7
+ data-search-input,
8
+ data-results="[data-search__results]"
9
+ )
10
+
11
+ ul.search__results(data-search__results)
12
+
13
+ script(src="/js/search.js")