@nera-static/plugin-utils 1.3.0 → 1.4.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.
Files changed (3) hide show
  1. package/README.md +93 -6
  2. package/index.js +37 -0
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -23,11 +23,50 @@ npm install @nera-static/plugin-utils
23
23
 
24
24
  Reads and parses a YAML config file, returning its contents as a JavaScript object.
25
25
 
26
+ Returns `{}` if the file does not exist or is empty — it never throws. **Supply a JS fallback for every key you read.**
27
+
28
+ Plugins read config from the *user's* site, not from the package. The `config/<name>.yaml` shipped inside a plugin is documentation only; it is never merged with the user's copy. Resolve against `process.cwd()`:
29
+
26
30
  ```js
31
+ import path from 'path'
27
32
  import { getConfig } from '@nera-static/plugin-utils'
28
33
 
29
- const config = getConfig('./path/to/config.yaml')
30
- console.log(config.title)
34
+ const config = getConfig(path.resolve(process.cwd(), 'config/my-plugin.yaml'))
35
+ const title = config.title || 'Default title'
36
+ ```
37
+
38
+ ### Slugs
39
+
40
+ #### `slugify(text: string): string`
41
+
42
+ Turns a user-authored string into a URL-safe slug, for anything that becomes a path segment, an HTML `id`, or a URL fragment.
43
+
44
+ ```js
45
+ import { slugify } from '@nera-static/plugin-utils'
46
+
47
+ slugify('Web Development') // 'web-development'
48
+ slugify('Über uns') // 'uber-uns'
49
+ slugify('Straße') // 'strasse'
50
+ slugify('Café') // 'cafe'
51
+ slugify('About Us!') // 'about-us'
52
+ slugify('日本語') // ''
53
+ ```
54
+
55
+ **Use this rather than writing your own.** The obvious one-liner is ASCII-only and produces broken output:
56
+
57
+ ```js
58
+ text.toLowerCase().replace(/[^\w]+/g, '-') // ❌ don't
59
+ ```
60
+
61
+ `\w` is `[A-Za-z0-9_]`, so `Über uns` becomes `-ber-uns` and `Straße` becomes `stra-e`. A leading hyphen is legal in an HTML `id` but is **not a valid CSS identifier** — `#-ber-uns` matches nothing in a stylesheet and `document.querySelector('#-ber-uns')` throws. That defect shipped in `plugin-one-page` until v3.0.0.
62
+
63
+ The rule: `ß` → `ss` (it has no NFKD decomposition, so it would otherwise become a hyphen mid-word), then NFKD with combining marks dropped, then lowercase, then every run of non-alphanumerics collapsed to a single `-`, then leading and trailing hyphens trimmed. It is idempotent.
64
+
65
+ A string with no Latin letters or digits returns `''`. **Decide what that means at the call site** — omitting the element is usually better than emitting `id=""`:
66
+
67
+ ```js
68
+ const id = slugify(heading)
69
+ return id ? `<a id="${id}"></a>` : ''
31
70
  ```
32
71
 
33
72
  ### Template Publishing
@@ -36,7 +75,7 @@ console.log(config.title)
36
75
 
37
76
  Validates if the current working directory is a valid Nera project.
38
77
 
39
- A directory qualifies if it *looks* like a Nera project — it contains both `config/app.yaml` and `pages/` — or if its `package.json` name matches `expectedPackageName` or starts with `nera`. The shape check means a project can be named anything.
78
+ A `package.json` must be present. Given that, the directory qualifies if it *looks* like a Nera project — it contains both `config/app.yaml` and `pages/` — or if its `package.json` name matches `expectedPackageName` or starts with `nera`. The shape check means a project can be named anything, but it does not remove the `package.json` requirement.
40
79
 
41
80
  ```js
42
81
  import { validateNeraProject } from '@nera-static/plugin-utils'
@@ -81,6 +120,10 @@ const result = publishAllTemplates({
81
120
  })
82
121
  ```
83
122
 
123
+ Returns `true` and logs a warning if `sourceDir` contains no `.pug` files; an unreadable `sourceDir` returns `false`. A publish script following the `process.exit(result ? 0 : 1)` pattern below therefore exits `0` having copied nothing — check your `sourceDir` if a plugin's templates never appear.
124
+
125
+ > The `__dirname` used in these option examples is not defined in ESM. See the CLI Integration example below for the two lines that define it.
126
+
84
127
  ### Template Publishing CLI Integration
85
128
 
86
129
  These functions are designed to be used in `bin/publish-template.js` scripts that can be executed via npm scripts:
@@ -119,12 +162,18 @@ This package is intended for use inside Nera plugins to:
119
162
 
120
163
  ```js
121
164
  // index.js - Main plugin file
165
+ import path from 'path'
122
166
  import { getConfig } from '@nera-static/plugin-utils'
123
167
 
124
- export function getAppData(app) {
125
- const config = getConfig(`${__dirname}/config/my-plugin.yaml`)
168
+ export function getAppData(data) {
169
+ const config = getConfig(
170
+ path.resolve(process.cwd(), 'config/my-plugin.yaml')
171
+ )
126
172
 
173
+ // Spread the incoming app. The return value REPLACES `app` wholesale --
174
+ // returning a bare object discards every other plugin's data, silently.
127
175
  return {
176
+ ...data.app,
128
177
  myPlugin: {
129
178
  // ... plugin logic using config
130
179
  },
@@ -132,6 +181,10 @@ export function getAppData(app) {
132
181
  }
133
182
  ```
134
183
 
184
+ A plugin exports `getAppData` and/or `getMetaData`. Both receive a single object — `{ app, pagesData }` — and both must be **synchronous**. `getAppData` must return a plain object, `getMetaData` an array; a wrong return type is skipped with a console warning while the build still succeeds, so a plugin that appears to "do nothing" is usually a return-type problem.
185
+
186
+ See the [Nera contributing guide](https://github.com/seebaermichi/nera/blob/main/CONTRIBUTING.md) for the full plugin contract.
187
+
135
188
  ```js
136
189
  // bin/publish-template.js - Template publishing script
137
190
  #!/usr/bin/env node
@@ -152,12 +205,46 @@ process.exit(result ? 0 : 1)
152
205
 
153
206
  ---
154
207
 
208
+ ## 🧪 Development
209
+
210
+ ```bash
211
+ npx vitest run # single pass -- `npm test` is watch mode
212
+ npm run lint
213
+ ```
214
+
215
+ ---
216
+
217
+ ## 🤝 Contributing
218
+
219
+ Issues and pull requests are welcome. See the
220
+ [Nera contributing guide](https://github.com/seebaermichi/nera/blob/main/CONTRIBUTING.md)
221
+ for plugin development, the hook contract, and local setup.
222
+
223
+ For this repo specifically:
224
+
225
+ - `npx vitest run` and `npm run lint` must pass (`npm test` is watch mode).
226
+ - Bump the version and update `CHANGELOG.md` **in the same commit** as the change.
227
+ - Every plugin in the fleet depends on this package, so treat the exported
228
+ function signatures as a public contract — changing one is a **major** bump.
229
+ - Releases publish from CI on a pushed `v*` tag. Never run `npm publish`.
230
+
231
+ ---
232
+
155
233
  ## 🧑‍💻 Maintainers
156
234
 
157
235
  Created and maintained by [@seebaermichi](https://github.com/seebaermichi)
158
236
 
159
237
  ---
160
238
 
161
- ## 📄 License
239
+ ## 🧩 Compatibility
240
+
241
+ - **Node.js**: >= 20.0.0
242
+ - **Runtime dependency**: `js-yaml ^4.1.0` only
243
+ - **Nera**: no direct dependency on the generator — this package is used by
244
+ plugins, not by the generator itself
245
+
246
+ ---
247
+
248
+ ## 📦 License
162
249
 
163
250
  MIT
package/index.js CHANGED
@@ -10,6 +10,43 @@ export function getConfig(filePath) {
10
10
  return {}
11
11
  }
12
12
 
13
+ /**
14
+ * Turns a user-authored string into a URL-safe slug, for anything that becomes
15
+ * a path segment, an HTML `id`, or a URL fragment.
16
+ *
17
+ * Lives here because plugins kept reinventing it and getting it wrong. The
18
+ * naive rule — `text.toLowerCase().replace(/[^\w]+/g, '-')` — is ASCII-only:
19
+ * `\w` is `[A-Za-z0-9_]`, so `Über uns` becomes `-ber-uns` and `Straße`
20
+ * becomes `stra-e`. A leading hyphen is legal in an HTML `id` but is **not** a
21
+ * valid CSS identifier, so `#-ber-uns` matches nothing in a stylesheet and
22
+ * `document.querySelector('#-ber-uns')` throws. That shipped in
23
+ * `plugin-one-page` until v3.0.0.
24
+ *
25
+ * The rule:
26
+ * 1. `ß` → `ss` first, because it has no NFKD decomposition and would
27
+ * otherwise become a hyphen mid-word. The expansion is the same in every
28
+ * language that uses it.
29
+ * 2. NFKD, then drop combining marks, so `é` → `e` rather than a hyphen.
30
+ * 3. Lowercase, collapse every remaining run of non-alphanumerics to a
31
+ * single `-`, and trim leading and trailing hyphens.
32
+ *
33
+ * A string with no Latin letters or digits (`日本語`, `!!!`) yields `''`.
34
+ * Callers must decide what that means — omitting the element is usually better
35
+ * than emitting an empty `id`.
36
+ *
37
+ * @param {string} text - The string to slugify
38
+ * @returns {string} - URL-safe slug, or `''` if nothing usable remains
39
+ */
40
+ export function slugify(text) {
41
+ return String(text)
42
+ .replace(/ß/g, 'ss')
43
+ .normalize('NFKD')
44
+ .replace(/[\u0300-\u036f]/g, '')
45
+ .toLowerCase()
46
+ .replace(/[^a-z0-9]+/g, '-')
47
+ .replace(/^-+|-+$/g, '')
48
+ }
49
+
13
50
  /**
14
51
  * Validates if the current working directory is a valid Nera project
15
52
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nera-static/plugin-utils",
3
- "version": "1.3.0",
3
+ "version": "1.4.0",
4
4
  "description": "Shared utility functions for Nera plugins (config loading, template publishing)",
5
5
  "main": "index.js",
6
6
  "type": "module",