@newcms/core 0.0.1 → 0.0.2
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/README.md +106 -0
- package/package.json +3 -2
package/README.md
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
# @newcms/core
|
|
2
|
+
|
|
3
|
+
Core hook engine, types, and constants for [NewCMS](https://github.com/durvs/newcms) — a modern, TypeScript-first content management system.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @newcms/core
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Hook Engine
|
|
12
|
+
|
|
13
|
+
The `HookEngine` is the backbone of NewCMS extensibility. It implements a priority-based action/filter system that allows plugins and themes to modify behavior without touching core code.
|
|
14
|
+
|
|
15
|
+
### Actions
|
|
16
|
+
|
|
17
|
+
Actions are hooks that trigger side effects. Multiple callbacks can be registered at different priorities.
|
|
18
|
+
|
|
19
|
+
```typescript
|
|
20
|
+
import { HookEngine } from '@newcms/core';
|
|
21
|
+
|
|
22
|
+
const hooks = new HookEngine();
|
|
23
|
+
|
|
24
|
+
// Register an action (priority 10 by default)
|
|
25
|
+
hooks.addAction('post_saved', (postId) => {
|
|
26
|
+
console.log(`Post ${postId} was saved`);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
// Register with custom priority (lower = earlier)
|
|
30
|
+
hooks.addAction('post_saved', (postId) => {
|
|
31
|
+
invalidateCache(postId);
|
|
32
|
+
}, { priority: 5 });
|
|
33
|
+
|
|
34
|
+
// Fire the action
|
|
35
|
+
await hooks.doAction('post_saved', 42);
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### Filters
|
|
39
|
+
|
|
40
|
+
Filters transform a value through a chain of callbacks. Each callback receives the (possibly modified) value and returns a new one.
|
|
41
|
+
|
|
42
|
+
```typescript
|
|
43
|
+
// Register filters
|
|
44
|
+
hooks.addFilter('post_title', (title) => title.trim());
|
|
45
|
+
hooks.addFilter('post_title', (title) => `${title} — My Site`, { priority: 20 });
|
|
46
|
+
|
|
47
|
+
// Apply the filter chain
|
|
48
|
+
const title = await hooks.applyFilters('post_title', ' Hello World ');
|
|
49
|
+
// => "Hello World — My Site"
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### Features
|
|
53
|
+
|
|
54
|
+
- **Priority ordering** — lower number executes first (default: 10)
|
|
55
|
+
- **Stable sort** — same-priority callbacks run in registration order
|
|
56
|
+
- **Async support** — `doAction` / `applyFilters` for async, `doActionSync` / `applyFiltersSync` for sync
|
|
57
|
+
- **Recursive hooks** — hooks can fire other hooks safely
|
|
58
|
+
- **Universal "all" hook** — fires before every action and filter
|
|
59
|
+
- **Execution stack** — track which hooks are currently running
|
|
60
|
+
- **Fire counters** — check how many times a hook has been fired
|
|
61
|
+
- **Precise removal** — remove by callback reference + priority
|
|
62
|
+
|
|
63
|
+
### API Reference
|
|
64
|
+
|
|
65
|
+
| Method | Description |
|
|
66
|
+
|--------|-------------|
|
|
67
|
+
| `addAction(name, callback, options?)` | Register an action callback |
|
|
68
|
+
| `addFilter(name, callback, options?)` | Register a filter callback |
|
|
69
|
+
| `doAction(name, ...args)` | Fire an action (async) |
|
|
70
|
+
| `doActionSync(name, ...args)` | Fire an action (sync) |
|
|
71
|
+
| `applyFilters(name, value, ...args)` | Apply filter chain (async) |
|
|
72
|
+
| `applyFiltersSync(name, value, ...args)` | Apply filter chain (sync) |
|
|
73
|
+
| `removeAction(name, callback, priority?)` | Remove a specific action |
|
|
74
|
+
| `removeFilter(name, callback, priority?)` | Remove a specific filter |
|
|
75
|
+
| `removeAllHooks(name, priority?)` | Remove all handlers for a hook |
|
|
76
|
+
| `hasAction(name, callback?)` | Check if action has handlers |
|
|
77
|
+
| `hasFilter(name, callback?)` | Check if filter has handlers |
|
|
78
|
+
| `didAction(name)` | Check if action has ever fired |
|
|
79
|
+
| `isDoingHook(name?)` | Check if a hook is currently executing |
|
|
80
|
+
| `currentHook()` | Get the name of the currently executing hook |
|
|
81
|
+
| `getFireCount(name)` | Get how many times a hook has fired |
|
|
82
|
+
| `getHandlerCount(name)` | Get number of registered handlers |
|
|
83
|
+
|
|
84
|
+
### Options
|
|
85
|
+
|
|
86
|
+
```typescript
|
|
87
|
+
interface AddHookOptions {
|
|
88
|
+
priority?: number; // Default: 10. Lower = earlier execution.
|
|
89
|
+
acceptedArgs?: number; // Default: 1. Number of arguments passed to callback.
|
|
90
|
+
}
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## Types & Constants
|
|
94
|
+
|
|
95
|
+
```typescript
|
|
96
|
+
import { POST_STATUS, USER_ROLES, HOOK_PRIORITY } from '@newcms/core';
|
|
97
|
+
|
|
98
|
+
POST_STATUS.PUBLISH // 'publish'
|
|
99
|
+
POST_STATUS.DRAFT // 'draft'
|
|
100
|
+
USER_ROLES.ADMINISTRATOR // 'administrator'
|
|
101
|
+
HOOK_PRIORITY.DEFAULT // 10
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## License
|
|
105
|
+
|
|
106
|
+
GPL-2.0-or-later
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@newcms/core",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Core hook engine, types, and constants for NewCMS",
|
|
6
6
|
"license": "GPL-2.0-or-later",
|
|
@@ -23,7 +23,8 @@
|
|
|
23
23
|
}
|
|
24
24
|
},
|
|
25
25
|
"files": [
|
|
26
|
-
"dist"
|
|
26
|
+
"dist",
|
|
27
|
+
"README.md"
|
|
27
28
|
],
|
|
28
29
|
"devDependencies": {
|
|
29
30
|
"typescript": "^5.9.3",
|