@prunellejs/cms 0.0.0-stage → 0.5.3
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/llms.txt +67 -0
- package/package.json +24 -4
- package/src/assets-js.ts +101 -0
- package/src/assets.ts +576 -0
- package/src/form-data.ts +464 -0
- package/src/handlers.ts +397 -0
- package/src/index.ts +99 -0
- package/src/inline-css.ts +621 -0
- package/src/inline-dom-js.ts +166 -0
- package/src/inline-form-js.ts +465 -0
- package/src/inline-handlers.ts +243 -0
- package/src/inline-js.ts +547 -0
- package/src/render.ts +583 -0
- package/src/sanitize.ts +150 -0
- package/src/schema.ts +487 -0
- package/src/shared.ts +119 -0
- package/README.md +0 -3
package/llms.txt
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# @prunellejs/cms Reference
|
|
2
|
+
|
|
3
|
+
> Schema-driven, dev-only CMS plugin for `@prunellejs/content` Markdown collections. Zero client JS on public pages, zero duplicate field declarations, and full no-JS HTML form support.
|
|
4
|
+
|
|
5
|
+
## Imports
|
|
6
|
+
|
|
7
|
+
- `@prunellejs/cms`: `cms`; types `CmsOptions`, `CmsGuard`, `CmsField`, `CmsFieldKind`, `CmsSchemaIssue`.
|
|
8
|
+
|
|
9
|
+
## Setup (`cms(options?)`)
|
|
10
|
+
|
|
11
|
+
Register `cms()` in `prunelle.config.ts` alongside `content()` from `@prunellejs/content`. Collections are discovered automatically via the shared `CONTENT_COLLECTIONS_SERVICE` — never declare fields twice.
|
|
12
|
+
|
|
13
|
+
- `guard?: (ctx: RequestContext) => boolean | Response | Promise<boolean | Response>`: runs on every CMS route and gates in-page overlay injection. Return `false` for `403 Forbidden`, or return a `Response` (e.g. a login redirect).
|
|
14
|
+
- `basePath?: string`: URL prefix where the dev CMS is mounted (default `'/_pru/cms'`).
|
|
15
|
+
- `inline?: boolean`: inject the dev-only Shadow DOM in-page editing overlay on pages that read file-backed collections (default `true`; set `false` for `/_pru/cms`-only editing).
|
|
16
|
+
|
|
17
|
+
## Mounted Routes (dev-only — never mounted when `dev: false` or on `--static`)
|
|
18
|
+
|
|
19
|
+
- `GET /_pru/cms` — list all collections with entry counts and links to create entries.
|
|
20
|
+
- `GET /_pru/cms/:collection` — list entries with optional `?locale=...` switching and `?q=...` filtering.
|
|
21
|
+
- `GET /_pru/cms/:collection/new` & `POST /_pru/cms/:collection/new` — create a new Markdown entry.
|
|
22
|
+
- `GET /_pru/cms/:collection/*slug` & `POST /_pru/cms/:collection/*slug` — edit an existing entry (supports nested slugs and `?locale=...`).
|
|
23
|
+
- `GET /_pru/cms/_entry?collection=...&slug=...&locale=...` — JSON metadata, schema fields, frontmatter, and Markdown body for the in-page overlay.
|
|
24
|
+
- `PATCH /_pru/cms/_inline` — CSRF-protected, Standard Schema-validated JSON patch/save endpoint used by the in-page overlay (`422` returns `{ ok: false, errors }`).
|
|
25
|
+
- `POST /_pru/cms/_preview` — renders sanitized Markdown preview HTML via `Bun.markdown.html()`.
|
|
26
|
+
- `GET /_pru/cms/_assets/cms.css`, `cms.js`, `inline.css`, `inline.js` — self-contained RGAA-accessible stylesheets and scripts (0 B in production/`--static`).
|
|
27
|
+
|
|
28
|
+
## In-Page Editing (dev-only, zero app markup)
|
|
29
|
+
|
|
30
|
+
When `inline` is enabled (the default), `@prunellejs/content` tracks file-backed entries read during each dev request and wraps rendered `entry.html` in zero-impact HTML comment anchors (`<!--pru-cms:...-->`). Pages that render a collection entry automatically receive a Shadow DOM floating toolbar (`Alt+Shift+E` toggles Edit-on-Page mode, `Cmd/Ctrl+S` saves, `Escape` cancels):
|
|
31
|
+
|
|
32
|
+
- **Edit on page**: highlights matched frontmatter string fields (click to edit inline via `contenteditable`, validated against the collection's Standard Schema on save) and the Markdown body region.
|
|
33
|
+
- **Docked slide-over panel**: full schema-driven Frontmatter form and a Markdown body editor that live-updates the actual page prose between the comment anchors as you type.
|
|
34
|
+
- **Zero app code & zero prod bytes**: routes never import `@prunellejs/cms`; non-content pages, unauthorized requests (`guard`), production servers (`dev: false`), and `--static` builds emit nothing.
|
|
35
|
+
|
|
36
|
+
## JSON Schema Field Mapping & `prunelle check`
|
|
37
|
+
|
|
38
|
+
Extracts JSON Schema from each collection's Standard Schema (`~standard.jsonSchema.input`, `toJSONSchema()`, or `.jsonSchema`):
|
|
39
|
+
|
|
40
|
+
- `string` (`text`, `date`, `date-time`, `email`, `uri`/`url`, `contentMediaType: 'text/markdown'` textarea)
|
|
41
|
+
- `enum` (`<select>` for string or number enums)
|
|
42
|
+
- `boolean` (accessible `<input type="checkbox" role="switch">`)
|
|
43
|
+
- `number` / `integer` (`<input type="number">` with `min`, `max`, `step`)
|
|
44
|
+
- `array` of `string` (tags input)
|
|
45
|
+
- nested `object` (`<fieldset>` with dotted field names like `author.name`)
|
|
46
|
+
- `array` of `object` (repeatable rows like `items[0].title` with no-JS Add/Remove/Move buttons)
|
|
47
|
+
- Markdown body (`<textarea name="__body">` with progressive live preview)
|
|
48
|
+
|
|
49
|
+
`prunelle check` reports an error if a file-backed collection has a schema without JSON Schema output or uses an unsupported field construct (naming both collection and field path).
|
|
50
|
+
|
|
51
|
+
## Snippet
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
// prunelle.config.ts
|
|
55
|
+
import { cms } from '@prunellejs/cms'
|
|
56
|
+
import { content } from '@prunellejs/content'
|
|
57
|
+
import { defineConfig } from 'prunelle/config'
|
|
58
|
+
|
|
59
|
+
export default defineConfig({
|
|
60
|
+
plugins: [
|
|
61
|
+
content(),
|
|
62
|
+
cms({
|
|
63
|
+
guard: ctx => Boolean(ctx.locals.user),
|
|
64
|
+
}),
|
|
65
|
+
],
|
|
66
|
+
})
|
|
67
|
+
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prunellejs/cms",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.5.3",
|
|
4
|
+
"description": "Schema-driven dev-only CMS plugin for Prunelle Markdown content collections",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"files": [
|
|
9
|
+
"src",
|
|
10
|
+
"llms.txt"
|
|
11
|
+
],
|
|
12
|
+
"exports": {
|
|
13
|
+
".": "./src/index.ts"
|
|
14
|
+
},
|
|
15
|
+
"engines": {
|
|
16
|
+
"bun": ">=1.4.0"
|
|
17
|
+
},
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "https://github.com/Kayno0o/prunelle",
|
|
21
|
+
"directory": "packages/plugin-cms"
|
|
22
|
+
},
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"prunelle": "^0.5.3"
|
|
25
|
+
}
|
|
26
|
+
}
|
package/src/assets-js.ts
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
// Progressive enhancement script for full-page @prunellejs/cms admin routes (live Markdown preview + row reordering).
|
|
2
|
+
|
|
3
|
+
export const CMS_JS = `(() => {
|
|
4
|
+
const editors = document.querySelectorAll('[data-cms-md-editor]');
|
|
5
|
+
for (const editor of editors) {
|
|
6
|
+
const previewUrl = editor.getAttribute('data-preview-url');
|
|
7
|
+
const toggle = editor.querySelector('[data-cms-md-toggle]');
|
|
8
|
+
const textarea = editor.querySelector('textarea[name="__body"]');
|
|
9
|
+
const split = editor.querySelector('.cms-md-split');
|
|
10
|
+
const pane = editor.querySelector('[data-cms-md-preview]');
|
|
11
|
+
if (!previewUrl || !toggle || !textarea || !split || !pane) continue;
|
|
12
|
+
|
|
13
|
+
toggle.hidden = false;
|
|
14
|
+
let timer = null;
|
|
15
|
+
let seq = 0;
|
|
16
|
+
|
|
17
|
+
const refresh = async () => {
|
|
18
|
+
if (pane.hidden) return;
|
|
19
|
+
const ticket = ++seq;
|
|
20
|
+
try {
|
|
21
|
+
const res = await fetch(previewUrl, {
|
|
22
|
+
method: 'POST',
|
|
23
|
+
headers: { 'content-type': 'application/json', 'accept': 'text/html' },
|
|
24
|
+
body: JSON.stringify({ markdown: textarea.value }),
|
|
25
|
+
});
|
|
26
|
+
if (!res.ok || ticket !== seq) return;
|
|
27
|
+
const html = await res.text();
|
|
28
|
+
pane.innerHTML = html || '<p class="cms-muted">Nothing to preview yet.</p>';
|
|
29
|
+
} catch (_err) {
|
|
30
|
+
if (ticket === seq) pane.textContent = 'Preview unavailable.';
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
toggle.addEventListener('click', () => {
|
|
35
|
+
const nextOpen = pane.hidden;
|
|
36
|
+
pane.hidden = !nextOpen;
|
|
37
|
+
split.setAttribute('data-preview-open', nextOpen ? 'true' : 'false');
|
|
38
|
+
toggle.setAttribute('aria-pressed', nextOpen ? 'true' : 'false');
|
|
39
|
+
toggle.textContent = nextOpen ? 'Hide preview' : 'Live preview';
|
|
40
|
+
if (nextOpen) void refresh();
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
textarea.addEventListener('input', () => {
|
|
44
|
+
if (pane.hidden) return;
|
|
45
|
+
if (timer !== null) clearTimeout(timer);
|
|
46
|
+
timer = setTimeout(() => void refresh(), 180);
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const reindexGroup = (group) => {
|
|
51
|
+
const path = group.getAttribute('data-cms-rows');
|
|
52
|
+
if (!path) return;
|
|
53
|
+
const rows = Array.from(group.querySelectorAll(':scope > [data-cms-row]'));
|
|
54
|
+
const countInput = group.querySelector('input[type="hidden"][name="__rows:' + path + '"]');
|
|
55
|
+
if (countInput) countInput.value = String(rows.length);
|
|
56
|
+
const escPath = path.replace(/[.*+?^\${}()|[\\]\\\\]/g, '\\\\$&');
|
|
57
|
+
const nameRe = new RegExp('^' + escPath + '\\\\[\\\\d+\\\\]');
|
|
58
|
+
rows.forEach((row, idx) => {
|
|
59
|
+
const title = row.querySelector('[data-cms-row-title]');
|
|
60
|
+
if (title) title.textContent = 'Row ' + (idx + 1);
|
|
61
|
+
for (const el of row.querySelectorAll('[name]')) {
|
|
62
|
+
const name = el.getAttribute('name');
|
|
63
|
+
if (name && nameRe.test(name)) {
|
|
64
|
+
el.setAttribute('name', name.replace(nameRe, path + '[' + idx + ']'));
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
for (const btn of row.querySelectorAll('button[name="__cms_action"]')) {
|
|
68
|
+
const val = btn.getAttribute('value') || '';
|
|
69
|
+
const parts = val.split(':');
|
|
70
|
+
if (parts.length === 3 && parts[1] === path) {
|
|
71
|
+
btn.setAttribute('value', parts[0] + ':' + path + ':' + idx);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
const upBtn = row.querySelector('[data-cms-row-move="up"]');
|
|
75
|
+
const downBtn = row.querySelector('[data-cms-row-move="down"]');
|
|
76
|
+
if (upBtn) upBtn.disabled = idx === 0;
|
|
77
|
+
if (downBtn) downBtn.disabled = idx === rows.length - 1;
|
|
78
|
+
});
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
document.addEventListener('click', (event) => {
|
|
82
|
+
const target = event.target;
|
|
83
|
+
if (!(target instanceof Element)) return;
|
|
84
|
+
const btn = target.closest('[data-cms-row-move]');
|
|
85
|
+
if (!btn) return;
|
|
86
|
+
const row = btn.closest('[data-cms-row]');
|
|
87
|
+
const group = row && row.parentElement && row.parentElement.closest('[data-cms-rows]');
|
|
88
|
+
if (!row || !group) return;
|
|
89
|
+
const dir = btn.getAttribute('data-cms-row-move');
|
|
90
|
+
if (dir === 'up' && row.previousElementSibling && row.previousElementSibling.hasAttribute('data-cms-row')) {
|
|
91
|
+
event.preventDefault();
|
|
92
|
+
group.insertBefore(row, row.previousElementSibling);
|
|
93
|
+
reindexGroup(group);
|
|
94
|
+
} else if (dir === 'down' && row.nextElementSibling && row.nextElementSibling.hasAttribute('data-cms-row')) {
|
|
95
|
+
event.preventDefault();
|
|
96
|
+
group.insertBefore(row.nextElementSibling, row);
|
|
97
|
+
reindexGroup(group);
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
})();
|
|
101
|
+
`
|