@nera-static/plugin-utils 1.3.1 → 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 +34 -0
  2. package/index.js +37 -0
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -35,6 +35,40 @@ const config = getConfig(path.resolve(process.cwd(), 'config/my-plugin.yaml'))
35
35
  const title = config.title || 'Default title'
36
36
  ```
37
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>` : ''
70
+ ```
71
+
38
72
  ### Template Publishing
39
73
 
40
74
  #### `validateNeraProject(expectedPackageName?: string): boolean`
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.1",
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",