@nixxie-cms/fields-slug 1.0.1
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/LICENSE +23 -0
- package/README.md +13 -0
- package/dist/declarations/src/index.d.ts +17 -0
- package/dist/declarations/src/index.d.ts.map +1 -0
- package/dist/nixxie-cms-fields-slug.cjs.d.ts +2 -0
- package/dist/nixxie-cms-fields-slug.cjs.js +65 -0
- package/dist/nixxie-cms-fields-slug.esm.js +60 -0
- package/package.json +33 -0
- package/src/index.ts +72 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Nixxie International DMCC
|
|
4
|
+
Portions Copyright (c) 2023 Thinkmill Labs Pty Ltd and contributors
|
|
5
|
+
(this software is derived from the KeystoneJS project, https://keystonejs.com)
|
|
6
|
+
|
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
in the Software without restriction, including without limitation the rights
|
|
10
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
furnished to do so, subject to the following conditions:
|
|
13
|
+
|
|
14
|
+
The above copyright notice and this permission notice shall be included in all
|
|
15
|
+
copies or substantial portions of the Software.
|
|
16
|
+
|
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# @nixxie-cms/fields-slug
|
|
2
|
+
|
|
3
|
+
A URL-slug field for Nixxie CMS. Stores a normalised, lowercased, hyphenated string and is unique
|
|
4
|
+
by default. When no value is supplied it derives one from another field (default `title`).
|
|
5
|
+
|
|
6
|
+
```ts
|
|
7
|
+
import { slug } from '@nixxie-cms/fields-slug'
|
|
8
|
+
|
|
9
|
+
fields: {
|
|
10
|
+
title: text(),
|
|
11
|
+
slug: slug({ from: 'title' }),
|
|
12
|
+
}
|
|
13
|
+
```
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { TextFieldConfig } from '@nixxie-cms/core/fields';
|
|
2
|
+
import type { BaseListTypeInfo, FieldTypeFunc } from '@nixxie-cms/core/types';
|
|
3
|
+
/** Lightweight, dependency-free slugifier. */
|
|
4
|
+
export declare function slugify(input: string): string;
|
|
5
|
+
export type SlugFieldConfig<ListTypeInfo extends BaseListTypeInfo> = Omit<TextFieldConfig<ListTypeInfo>, 'isIndexed'> & {
|
|
6
|
+
/** Source field to derive the slug from when none is provided. Default: 'title' */
|
|
7
|
+
from?: string;
|
|
8
|
+
/** Indexing — slugs are unique by default. */
|
|
9
|
+
isIndexed?: boolean | 'unique';
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* A URL slug field. Stores a normalised, lowercased, hyphenated string. When no value is supplied
|
|
13
|
+
* it is derived from the `from` field (default `title`). Built on top of the core `text` field, so
|
|
14
|
+
* it uses the standard text Admin UI.
|
|
15
|
+
*/
|
|
16
|
+
export declare function slug<ListTypeInfo extends BaseListTypeInfo>(config?: SlugFieldConfig<ListTypeInfo>): FieldTypeFunc<ListTypeInfo>;
|
|
17
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"../../../src","sources":["index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAA;AAC9D,OAAO,KAAK,EAAE,gBAAgB,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAA;AAE7E,8CAA8C;AAC9C,wBAAgB,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAS7C;AAED,MAAM,MAAM,eAAe,CAAC,YAAY,SAAS,gBAAgB,IAAI,IAAI,CACvE,eAAe,CAAC,YAAY,CAAC,EAC7B,WAAW,CACZ,GAAG;IACF,mFAAmF;IACnF,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,8CAA8C;IAC9C,SAAS,CAAC,EAAE,OAAO,GAAG,QAAQ,CAAA;CAC/B,CAAA;AAED;;;;GAIG;AACH,wBAAgB,IAAI,CAAC,YAAY,SAAS,gBAAgB,EACxD,MAAM,GAAE,eAAe,CAAC,YAAY,CAAM,GACzC,aAAa,CAAC,YAAY,CAAC,CAsC7B"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export * from "./declarations/src/index.js";
|
|
2
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibml4eGllLWNtcy1maWVsZHMtc2x1Zy5janMuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4vZGVjbGFyYXRpb25zL3NyYy9pbmRleC5kLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBIn0=
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var fields = require('@nixxie-cms/core/fields');
|
|
6
|
+
|
|
7
|
+
/** Lightweight, dependency-free slugifier. */
|
|
8
|
+
function slugify(input) {
|
|
9
|
+
return input.toString().normalize('NFKD').replace(/[\u0300-\u036f]/g, '').toLowerCase().trim().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '');
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* A URL slug field. Stores a normalised, lowercased, hyphenated string. When no value is supplied
|
|
13
|
+
* it is derived from the `from` field (default `title`). Built on top of the core `text` field, so
|
|
14
|
+
* it uses the standard text Admin UI.
|
|
15
|
+
*/
|
|
16
|
+
function slug(config = {}) {
|
|
17
|
+
const {
|
|
18
|
+
from = 'title',
|
|
19
|
+
isIndexed = 'unique',
|
|
20
|
+
hooks,
|
|
21
|
+
...rest
|
|
22
|
+
} = config;
|
|
23
|
+
const userValidate = hooks === null || hooks === void 0 ? void 0 : hooks.validate;
|
|
24
|
+
return fields.text({
|
|
25
|
+
...rest,
|
|
26
|
+
isIndexed,
|
|
27
|
+
hooks: {
|
|
28
|
+
...hooks,
|
|
29
|
+
resolveInput: args => {
|
|
30
|
+
var _resolvedData$from;
|
|
31
|
+
const {
|
|
32
|
+
resolvedData,
|
|
33
|
+
item,
|
|
34
|
+
fieldKey
|
|
35
|
+
} = args;
|
|
36
|
+
const provided = resolvedData[fieldKey];
|
|
37
|
+
if (typeof provided === 'string' && provided.length > 0) return slugify(provided);
|
|
38
|
+
const source = (_resolvedData$from = resolvedData[from]) !== null && _resolvedData$from !== void 0 ? _resolvedData$from : item === null || item === void 0 ? void 0 : item[from];
|
|
39
|
+
if (typeof source === 'string' && source.length > 0) return slugify(source);
|
|
40
|
+
return provided;
|
|
41
|
+
},
|
|
42
|
+
validate: async args => {
|
|
43
|
+
var _userValidate;
|
|
44
|
+
// Preserve any user-supplied validate hook (function or { create, update, delete } form).
|
|
45
|
+
if (typeof userValidate === 'function') await userValidate(args);else await (userValidate === null || userValidate === void 0 || (_userValidate = userValidate[args.operation]) === null || _userValidate === void 0 ? void 0 : _userValidate.call(userValidate, args));
|
|
46
|
+
const {
|
|
47
|
+
resolvedData,
|
|
48
|
+
fieldKey,
|
|
49
|
+
addValidationError,
|
|
50
|
+
operation
|
|
51
|
+
} = args;
|
|
52
|
+
if (operation === 'delete') return;
|
|
53
|
+
// slugify() yields '' for input with no [a-z0-9] characters (e.g. non-Latin scripts). An
|
|
54
|
+
// empty slug would either silently store blank or, with the default unique index, surface a
|
|
55
|
+
// raw DB constraint error on the second one — give a clear validation message instead.
|
|
56
|
+
if (resolvedData[fieldKey] === '') {
|
|
57
|
+
addValidationError(`${fieldKey} could not be generated — provide a value containing letters or numbers`);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
exports.slug = slug;
|
|
65
|
+
exports.slugify = slugify;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { text } from '@nixxie-cms/core/fields';
|
|
2
|
+
|
|
3
|
+
/** Lightweight, dependency-free slugifier. */
|
|
4
|
+
function slugify(input) {
|
|
5
|
+
return input.toString().normalize('NFKD').replace(/[\u0300-\u036f]/g, '').toLowerCase().trim().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '');
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* A URL slug field. Stores a normalised, lowercased, hyphenated string. When no value is supplied
|
|
9
|
+
* it is derived from the `from` field (default `title`). Built on top of the core `text` field, so
|
|
10
|
+
* it uses the standard text Admin UI.
|
|
11
|
+
*/
|
|
12
|
+
function slug(config = {}) {
|
|
13
|
+
const {
|
|
14
|
+
from = 'title',
|
|
15
|
+
isIndexed = 'unique',
|
|
16
|
+
hooks,
|
|
17
|
+
...rest
|
|
18
|
+
} = config;
|
|
19
|
+
const userValidate = hooks === null || hooks === void 0 ? void 0 : hooks.validate;
|
|
20
|
+
return text({
|
|
21
|
+
...rest,
|
|
22
|
+
isIndexed,
|
|
23
|
+
hooks: {
|
|
24
|
+
...hooks,
|
|
25
|
+
resolveInput: args => {
|
|
26
|
+
var _resolvedData$from;
|
|
27
|
+
const {
|
|
28
|
+
resolvedData,
|
|
29
|
+
item,
|
|
30
|
+
fieldKey
|
|
31
|
+
} = args;
|
|
32
|
+
const provided = resolvedData[fieldKey];
|
|
33
|
+
if (typeof provided === 'string' && provided.length > 0) return slugify(provided);
|
|
34
|
+
const source = (_resolvedData$from = resolvedData[from]) !== null && _resolvedData$from !== void 0 ? _resolvedData$from : item === null || item === void 0 ? void 0 : item[from];
|
|
35
|
+
if (typeof source === 'string' && source.length > 0) return slugify(source);
|
|
36
|
+
return provided;
|
|
37
|
+
},
|
|
38
|
+
validate: async args => {
|
|
39
|
+
var _userValidate;
|
|
40
|
+
// Preserve any user-supplied validate hook (function or { create, update, delete } form).
|
|
41
|
+
if (typeof userValidate === 'function') await userValidate(args);else await (userValidate === null || userValidate === void 0 || (_userValidate = userValidate[args.operation]) === null || _userValidate === void 0 ? void 0 : _userValidate.call(userValidate, args));
|
|
42
|
+
const {
|
|
43
|
+
resolvedData,
|
|
44
|
+
fieldKey,
|
|
45
|
+
addValidationError,
|
|
46
|
+
operation
|
|
47
|
+
} = args;
|
|
48
|
+
if (operation === 'delete') return;
|
|
49
|
+
// slugify() yields '' for input with no [a-z0-9] characters (e.g. non-Latin scripts). An
|
|
50
|
+
// empty slug would either silently store blank or, with the default unique index, surface a
|
|
51
|
+
// raw DB constraint error on the second one — give a clear validation message instead.
|
|
52
|
+
if (resolvedData[fieldKey] === '') {
|
|
53
|
+
addValidationError(`${fieldKey} could not be generated — provide a value containing letters or numbers`);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export { slug, slugify };
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nixxie-cms/fields-slug",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"main": "dist/nixxie-cms-fields-slug.cjs.js",
|
|
6
|
+
"module": "dist/nixxie-cms-fields-slug.esm.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/nixxie-cms-fields-slug.cjs.js",
|
|
10
|
+
"module": "./dist/nixxie-cms-fields-slug.esm.js",
|
|
11
|
+
"default": "./dist/nixxie-cms-fields-slug.cjs.js"
|
|
12
|
+
},
|
|
13
|
+
"./package.json": "./package.json"
|
|
14
|
+
},
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"@babel/runtime": "^7.24.7"
|
|
17
|
+
},
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@nixxie-cms/core": "^1.0.1"
|
|
20
|
+
},
|
|
21
|
+
"peerDependencies": {
|
|
22
|
+
"@nixxie-cms/core": "^1.0.1"
|
|
23
|
+
},
|
|
24
|
+
"preconstruct": {
|
|
25
|
+
"entrypoints": [
|
|
26
|
+
"index.ts"
|
|
27
|
+
]
|
|
28
|
+
},
|
|
29
|
+
"repository": {
|
|
30
|
+
"type": "git",
|
|
31
|
+
"url": "https://github.com/nixxiecms/nixxie/tree/main/packages/fields-slug"
|
|
32
|
+
}
|
|
33
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { text } from '@nixxie-cms/core/fields'
|
|
2
|
+
import type { TextFieldConfig } from '@nixxie-cms/core/fields'
|
|
3
|
+
import type { BaseListTypeInfo, FieldTypeFunc } from '@nixxie-cms/core/types'
|
|
4
|
+
|
|
5
|
+
/** Lightweight, dependency-free slugifier. */
|
|
6
|
+
export function slugify(input: string): string {
|
|
7
|
+
return input
|
|
8
|
+
.toString()
|
|
9
|
+
.normalize('NFKD')
|
|
10
|
+
.replace(/[\u0300-\u036f]/g, '')
|
|
11
|
+
.toLowerCase()
|
|
12
|
+
.trim()
|
|
13
|
+
.replace(/[^a-z0-9]+/g, '-')
|
|
14
|
+
.replace(/^-+|-+$/g, '')
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export type SlugFieldConfig<ListTypeInfo extends BaseListTypeInfo> = Omit<
|
|
18
|
+
TextFieldConfig<ListTypeInfo>,
|
|
19
|
+
'isIndexed'
|
|
20
|
+
> & {
|
|
21
|
+
/** Source field to derive the slug from when none is provided. Default: 'title' */
|
|
22
|
+
from?: string
|
|
23
|
+
/** Indexing — slugs are unique by default. */
|
|
24
|
+
isIndexed?: boolean | 'unique'
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* A URL slug field. Stores a normalised, lowercased, hyphenated string. When no value is supplied
|
|
29
|
+
* it is derived from the `from` field (default `title`). Built on top of the core `text` field, so
|
|
30
|
+
* it uses the standard text Admin UI.
|
|
31
|
+
*/
|
|
32
|
+
export function slug<ListTypeInfo extends BaseListTypeInfo>(
|
|
33
|
+
config: SlugFieldConfig<ListTypeInfo> = {}
|
|
34
|
+
): FieldTypeFunc<ListTypeInfo> {
|
|
35
|
+
const { from = 'title', isIndexed = 'unique', hooks, ...rest } = config
|
|
36
|
+
const userValidate = hooks?.validate as
|
|
37
|
+
| ((args: any) => unknown)
|
|
38
|
+
| { create?: (args: any) => unknown; update?: (args: any) => unknown; delete?: (args: any) => unknown }
|
|
39
|
+
| undefined
|
|
40
|
+
|
|
41
|
+
return text<ListTypeInfo>({
|
|
42
|
+
...(rest as TextFieldConfig<ListTypeInfo>),
|
|
43
|
+
isIndexed,
|
|
44
|
+
hooks: {
|
|
45
|
+
...hooks,
|
|
46
|
+
resolveInput: (args: any) => {
|
|
47
|
+
const { resolvedData, item, fieldKey } = args
|
|
48
|
+
const provided = resolvedData[fieldKey]
|
|
49
|
+
if (typeof provided === 'string' && provided.length > 0) return slugify(provided)
|
|
50
|
+
const source = resolvedData[from] ?? item?.[from]
|
|
51
|
+
if (typeof source === 'string' && source.length > 0) return slugify(source)
|
|
52
|
+
return provided
|
|
53
|
+
},
|
|
54
|
+
validate: async (args: any) => {
|
|
55
|
+
// Preserve any user-supplied validate hook (function or { create, update, delete } form).
|
|
56
|
+
if (typeof userValidate === 'function') await userValidate(args)
|
|
57
|
+
else await userValidate?.[args.operation as 'create' | 'update' | 'delete']?.(args)
|
|
58
|
+
|
|
59
|
+
const { resolvedData, fieldKey, addValidationError, operation } = args
|
|
60
|
+
if (operation === 'delete') return
|
|
61
|
+
// slugify() yields '' for input with no [a-z0-9] characters (e.g. non-Latin scripts). An
|
|
62
|
+
// empty slug would either silently store blank or, with the default unique index, surface a
|
|
63
|
+
// raw DB constraint error on the second one — give a clear validation message instead.
|
|
64
|
+
if (resolvedData[fieldKey] === '') {
|
|
65
|
+
addValidationError(
|
|
66
|
+
`${fieldKey} could not be generated — provide a value containing letters or numbers`
|
|
67
|
+
)
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
})
|
|
72
|
+
}
|