@conloca/astro-cms 0.2.2 → 0.2.4
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 +12 -0
- package/dist/ts/.build-inputs.json +90 -0
- package/dist/ts/node/content.d.ts +1 -1
- package/dist/ts/node/content.d.ts.map +1 -1
- package/dist/ts/node/content.js +1 -1
- package/dist/ts/node/content.js.map +1 -1
- package/dist/ts/node/index.d.ts +1 -1
- package/dist/ts/node/index.d.ts.map +1 -1
- package/dist/ts/node/index.js +3 -3
- package/dist/ts/node/index.js.map +1 -1
- package/dist/ts/src/checks.d.ts +7 -3
- package/dist/ts/src/checks.d.ts.map +1 -1
- package/dist/ts/src/checks.js +7 -3
- package/dist/ts/src/checks.js.map +1 -1
- package/dist/ts/src/collections.d.ts +1 -1
- package/dist/ts/src/collections.d.ts.map +1 -1
- package/dist/ts/src/collections.js +1 -1
- package/dist/ts/src/collections.js.map +1 -1
- package/dist/ts/src/editor-descriptors.d.ts +23 -5
- package/dist/ts/src/editor-descriptors.d.ts.map +1 -1
- package/dist/ts/src/editor-descriptors.js +30 -5
- package/dist/ts/src/editor-descriptors.js.map +1 -1
- package/dist/ts/src/entries.d.ts +5 -0
- package/dist/ts/src/entries.d.ts.map +1 -1
- package/dist/ts/src/entries.js +20 -0
- package/dist/ts/src/entries.js.map +1 -1
- package/dist/ts/src/lib/static-page-routes.d.ts +34 -0
- package/dist/ts/src/lib/static-page-routes.d.ts.map +1 -0
- package/dist/ts/src/lib/static-page-routes.js +57 -0
- package/dist/ts/src/lib/static-page-routes.js.map +1 -0
- package/dist/ts/src/lib/static-routes-endpoint.d.ts +9 -0
- package/dist/ts/src/lib/static-routes-endpoint.d.ts.map +1 -0
- package/dist/ts/src/lib/static-routes-endpoint.js +27 -0
- package/dist/ts/src/lib/static-routes-endpoint.js.map +1 -0
- package/dist/ts/src/lib/unknown-components.d.ts +8 -0
- package/dist/ts/src/lib/unknown-components.d.ts.map +1 -1
- package/dist/ts/src/lib/unknown-components.js +1 -1
- package/dist/ts/src/lib/unknown-components.js.map +1 -1
- package/dist/ts/src/plugin-spa.d.ts.map +1 -1
- package/dist/ts/src/plugin-spa.js +60 -18
- package/dist/ts/src/plugin-spa.js.map +1 -1
- package/dist/ts/src/sandbox-frame-handler.d.ts +4 -1
- package/dist/ts/src/sandbox-frame-handler.d.ts.map +1 -1
- package/dist/ts/src/sandbox-frame-handler.js +22 -11
- package/dist/ts/src/sandbox-frame-handler.js.map +1 -1
- package/dist/ts/src/site-styles/content-wrapper-endpoint.d.ts +3 -62
- package/dist/ts/src/site-styles/content-wrapper-endpoint.d.ts.map +1 -1
- package/dist/ts/src/site-styles/content-wrapper-endpoint.js +12 -193
- package/dist/ts/src/site-styles/content-wrapper-endpoint.js.map +1 -1
- package/node/content.ts +1 -0
- package/node/index.ts +7 -7
- package/package.json +6 -7
- package/src/checks.ts +13 -3
- package/src/collections.ts +1 -0
- package/src/editor-descriptors.ts +51 -5
- package/src/entries.ts +30 -0
- package/src/lib/static-page-routes.ts +89 -0
- package/src/lib/static-routes-endpoint.ts +28 -0
- package/src/lib/unknown-components.ts +1 -1
- package/src/plugin-spa.ts +68 -18
- package/src/sandbox-frame-handler.ts +16 -5
- package/src/site-styles/content-wrapper-endpoint.ts +4 -285
|
@@ -1,3 +1,15 @@
|
|
|
1
|
+
import { findContentWrapper } from '@conloca/cms-spa/content-wrapper';
|
|
2
|
+
/**
|
|
3
|
+
* Per-route content-wrapper discovery endpoint.
|
|
4
|
+
*
|
|
5
|
+
* The editor mirrors the host's content surface by rendering its
|
|
6
|
+
* contenteditable inside a clone of whatever element the host wraps
|
|
7
|
+
* its page content in. This endpoint fetches the live HTML the dev
|
|
8
|
+
* server serves at `?url=<route>`, locates the relevant wrappers
|
|
9
|
+
* (prose content + code-block chrome) with `findContentWrapper`, and
|
|
10
|
+
* returns them as JSON. An editor page without this endpoint runs the
|
|
11
|
+
* same function on the page its style probe loads.
|
|
12
|
+
*/
|
|
1
13
|
export function createContentWrapperEndpoint() {
|
|
2
14
|
return async (req, res, _next) => {
|
|
3
15
|
try {
|
|
@@ -28,197 +40,4 @@ export function createContentWrapperEndpoint() {
|
|
|
28
40
|
}
|
|
29
41
|
};
|
|
30
42
|
}
|
|
31
|
-
/** Tags whose presence signals "this element holds prose content."
|
|
32
|
-
* We walk up from the first such element we find inside `<main>` to
|
|
33
|
-
* locate the wrapper. Headings + paragraphs cover every prose-styling
|
|
34
|
-
* convention we've seen — every CSS framework scopes typography
|
|
35
|
-
* through descendant selectors on `h1`–`h6` and `p`. */
|
|
36
|
-
const PROSE_TAGS = new Set(['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p']);
|
|
37
|
-
/** Tag that anchors code-block wrapper discovery. The host's code-
|
|
38
|
-
* block library (Starlight's expressive-code, Tailwind's `pre`
|
|
39
|
-
* styling, etc.) always renders a `<pre>` containing the code,
|
|
40
|
-
* often wrapped in additional classed elements. */
|
|
41
|
-
const CODE_BLOCK_ANCHOR = 'pre';
|
|
42
|
-
/** HTML5 void elements — never push onto the open-tag stack since they
|
|
43
|
-
* have no closing tag. */
|
|
44
|
-
const VOID_TAGS = new Set([
|
|
45
|
-
'area',
|
|
46
|
-
'base',
|
|
47
|
-
'br',
|
|
48
|
-
'col',
|
|
49
|
-
'embed',
|
|
50
|
-
'hr',
|
|
51
|
-
'img',
|
|
52
|
-
'input',
|
|
53
|
-
'link',
|
|
54
|
-
'meta',
|
|
55
|
-
'source',
|
|
56
|
-
'track',
|
|
57
|
-
'wbr',
|
|
58
|
-
]);
|
|
59
|
-
/**
|
|
60
|
-
* Locate the host's content + code-block wrappers in raw HTML. Single-
|
|
61
|
-
* pass regex walk with a stack so we can recover ancestor chains for
|
|
62
|
-
* both prose tags and `<pre>` tags without a full DOM parser dep.
|
|
63
|
-
*
|
|
64
|
-
* Exported for unit testing.
|
|
65
|
-
*/
|
|
66
|
-
export function findContentWrapper(html) {
|
|
67
|
-
// Strip comments and scripts so opening-tag-shaped strings inside
|
|
68
|
-
// them don't pollute the walk.
|
|
69
|
-
const cleaned = html
|
|
70
|
-
.replace(/<!--[\s\S]*?-->/g, '')
|
|
71
|
-
.replace(/<script[\s\S]*?<\/script>/gi, '')
|
|
72
|
-
.replace(/<style[\s\S]*?<\/style>/gi, '');
|
|
73
|
-
const tagRe = /<(\/?)([a-zA-Z][a-zA-Z0-9-]*)((?:\s+[^>]*)?)\s*(\/?)>/g;
|
|
74
|
-
const stack = [];
|
|
75
|
-
let explicit = null;
|
|
76
|
-
let mainEl = null;
|
|
77
|
-
const proseAncestorChains = [];
|
|
78
|
-
const preAncestorChains = [];
|
|
79
|
-
for (const match of cleaned.matchAll(tagRe)) {
|
|
80
|
-
const isClose = match[1] === '/';
|
|
81
|
-
const tagName = match[2].toLowerCase();
|
|
82
|
-
const attrBlob = match[3] ?? '';
|
|
83
|
-
const explicitlySelfClosing = match[4] === '/';
|
|
84
|
-
if (isClose) {
|
|
85
|
-
for (let i = stack.length - 1; i >= 0; i--) {
|
|
86
|
-
if (stack[i].tag === tagName) {
|
|
87
|
-
stack.length = i;
|
|
88
|
-
break;
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
continue;
|
|
92
|
-
}
|
|
93
|
-
const attrs = parseAttributes(attrBlob);
|
|
94
|
-
const className = attrs.class ?? '';
|
|
95
|
-
const el = { tag: tagName, className, attrs };
|
|
96
|
-
// Priority 1 (content only): explicit opt-in. Capture and keep
|
|
97
|
-
// walking — we still need to discover the code-block wrapper.
|
|
98
|
-
if (!explicit && 'data-conloca-content-root' in attrs) {
|
|
99
|
-
explicit = { tagName, className };
|
|
100
|
-
}
|
|
101
|
-
if (!mainEl && tagName === 'main') {
|
|
102
|
-
mainEl = el;
|
|
103
|
-
}
|
|
104
|
-
if (mainEl && PROSE_TAGS.has(tagName)) {
|
|
105
|
-
const mainIdx = stack.indexOf(mainEl);
|
|
106
|
-
if (mainIdx >= 0) {
|
|
107
|
-
proseAncestorChains.push(stack.slice(mainIdx + 1));
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
if (mainEl && tagName === CODE_BLOCK_ANCHOR) {
|
|
111
|
-
const mainIdx = stack.indexOf(mainEl);
|
|
112
|
-
if (mainIdx >= 0) {
|
|
113
|
-
preAncestorChains.push(stack.slice(mainIdx + 1));
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
const isVoid = VOID_TAGS.has(tagName);
|
|
117
|
-
if (!isVoid && !explicitlySelfClosing) {
|
|
118
|
-
stack.push(el);
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
const content = resolveContentWrapper(explicit, mainEl, proseAncestorChains);
|
|
122
|
-
const codeBlock = resolveCodeBlockWrapper(content, preAncestorChains);
|
|
123
|
-
return { content, codeBlock };
|
|
124
|
-
}
|
|
125
|
-
/**
|
|
126
|
-
* Pick the content wrapper from the walk's collected data.
|
|
127
|
-
*
|
|
128
|
-
* Priority: explicit opt-in > majority-prose-wrapper-inside-main >
|
|
129
|
-
* `<main>` fallback > null.
|
|
130
|
-
*/
|
|
131
|
-
function resolveContentWrapper(explicit, mainEl, proseAncestorChains) {
|
|
132
|
-
if (explicit)
|
|
133
|
-
return explicit;
|
|
134
|
-
if (!mainEl)
|
|
135
|
-
return null;
|
|
136
|
-
if (proseAncestorChains.length === 0) {
|
|
137
|
-
return { tagName: 'main', className: mainEl.className };
|
|
138
|
-
}
|
|
139
|
-
// Tally classed-ancestor occurrences across all prose chains.
|
|
140
|
-
const scores = new Map();
|
|
141
|
-
for (const chain of proseAncestorChains) {
|
|
142
|
-
for (const ancestor of chain) {
|
|
143
|
-
if (!ancestor.className)
|
|
144
|
-
continue;
|
|
145
|
-
scores.set(ancestor, (scores.get(ancestor) ?? 0) + 1);
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
// Majority threshold rules out partial-coverage classed ancestors
|
|
149
|
-
// (single Cards holding one of many prose blocks).
|
|
150
|
-
const threshold = proseAncestorChains.length / 2;
|
|
151
|
-
const candidates = [...scores.entries()].filter(([, count]) => count > threshold);
|
|
152
|
-
if (candidates.length === 0) {
|
|
153
|
-
return { tagName: 'main', className: mainEl.className };
|
|
154
|
-
}
|
|
155
|
-
// Tie-break by depth: the candidate that appears deepest in any
|
|
156
|
-
// chain is the most specific (closest to the prose tags).
|
|
157
|
-
const maxChainIndex = (el) => Math.max(...proseAncestorChains.map((c) => c.indexOf(el)));
|
|
158
|
-
candidates.sort((a, b) => {
|
|
159
|
-
if (b[1] !== a[1])
|
|
160
|
-
return b[1] - a[1];
|
|
161
|
-
return maxChainIndex(b[0]) - maxChainIndex(a[0]);
|
|
162
|
-
});
|
|
163
|
-
const winner = candidates[0][0];
|
|
164
|
-
return { tagName: winner.tag, className: winner.className };
|
|
165
|
-
}
|
|
166
|
-
/**
|
|
167
|
-
* Derive the code-block wrapper chain from a representative `<pre>`'s
|
|
168
|
-
* ancestor chain. We strip everything at or above the content wrapper
|
|
169
|
-
* (its styles are already handled by the content-wrapper mirror) and
|
|
170
|
-
* return the remaining classed elements in nesting order, outermost
|
|
171
|
-
* first.
|
|
172
|
-
*
|
|
173
|
-
* Returning a chain (instead of one collapsed element) preserves the
|
|
174
|
-
* shape host CSS expects:
|
|
175
|
-
*
|
|
176
|
-
* - Single-class selectors (`.frame { ... }`) hit on whichever link
|
|
177
|
-
* carries that class.
|
|
178
|
-
* - Descendant selectors (`.expressive-code .frame { ... }`,
|
|
179
|
-
* `.frame .header > .title`) hit because the editor's renderer
|
|
180
|
-
* materialises the chain as nested elements with the host's
|
|
181
|
-
* expected tag names.
|
|
182
|
-
*
|
|
183
|
-
* The editor's code-block frame component (`code-block-frame.tsx` in
|
|
184
|
-
* `@conloca/mdx`) treats the LAST link as the "frame" element (where
|
|
185
|
-
* figcaption/pre/copy children render) and wraps it with the earlier
|
|
186
|
-
* links from inside out. This matches the host's authoring shape for
|
|
187
|
-
* libraries like ExpressiveCode (`.expressive-code > figure.frame >
|
|
188
|
-
* figcaption + pre + .copy`).
|
|
189
|
-
*/
|
|
190
|
-
function resolveCodeBlockWrapper(content, preAncestorChains) {
|
|
191
|
-
if (preAncestorChains.length === 0)
|
|
192
|
-
return null;
|
|
193
|
-
// Pick the first <pre>'s chain as representative. Code blocks on a
|
|
194
|
-
// single page are uniformly wrapped, so chains are interchangeable.
|
|
195
|
-
const chain = preAncestorChains[0];
|
|
196
|
-
// Strip everything at-or-above the content wrapper; the rest is
|
|
197
|
-
// code-block-specific chrome that this endpoint is responsible for.
|
|
198
|
-
const contentIdx = content
|
|
199
|
-
? chain.findIndex((el) => el.tag === content.tagName && el.className === content.className)
|
|
200
|
-
: -1;
|
|
201
|
-
const codeBlockChain = chain.slice(contentIdx + 1);
|
|
202
|
-
const classedAncestors = codeBlockChain.filter((el) => el.className);
|
|
203
|
-
if (classedAncestors.length === 0)
|
|
204
|
-
return null;
|
|
205
|
-
return classedAncestors.map((el) => ({ tagName: el.tag, className: el.className }));
|
|
206
|
-
}
|
|
207
|
-
/**
|
|
208
|
-
* Parse the attribute blob inside an opening tag. Handles `name="v"`,
|
|
209
|
-
* `name='v'`, `name=v`, and boolean (no-value) attributes. Doesn't
|
|
210
|
-
* handle entity decoding (`&` → `&`) since the values we read
|
|
211
|
-
* (tag name, class list, the marker attribute) don't carry entities
|
|
212
|
-
* in practice.
|
|
213
|
-
*/
|
|
214
|
-
function parseAttributes(s) {
|
|
215
|
-
const out = {};
|
|
216
|
-
const re = /([a-zA-Z_:][-a-zA-Z0-9_:.]*)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|(\S+)))?/g;
|
|
217
|
-
for (const match of s.matchAll(re)) {
|
|
218
|
-
const name = match[1];
|
|
219
|
-
const value = match[2] ?? match[3] ?? match[4] ?? '';
|
|
220
|
-
out[name] = value;
|
|
221
|
-
}
|
|
222
|
-
return out;
|
|
223
|
-
}
|
|
224
43
|
//# sourceMappingURL=content-wrapper-endpoint.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"content-wrapper-endpoint.js","sourceRoot":"","sources":["../../../../src/site-styles/content-wrapper-endpoint.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"content-wrapper-endpoint.js","sourceRoot":"","sources":["../../../../src/site-styles/content-wrapper-endpoint.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,kCAAkC,CAAC;AAGtE;;;;;;;;;;GAUG;AACH,MAAM,UAAU,4BAA4B;IAC1C,OAAO,KAAK,EAAE,GAAG,EAAE,GAAG,EAAE,KAAK,EAAE,EAAE;QAC/B,IAAI,CAAC;YACH,MAAM,MAAM,GAAG,IAAI,GAAG,CAAC,GAAG,CAAC,GAAG,IAAI,EAAE,EAAE,kBAAkB,CAAC,CAAC;YAC1D,MAAM,QAAQ,GAAG,MAAM,CAAC,YAAY,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;YAChD,IAAI,CAAC,QAAQ,EAAE,CAAC;gBACd,GAAG,CAAC,UAAU,GAAG,GAAG,CAAC;gBACrB,GAAG,CAAC,SAAS,CAAC,cAAc,EAAE,YAAY,CAAC,CAAC;gBAC5C,GAAG,CAAC,GAAG,CAAC,uCAAuC,CAAC,CAAC;gBACjD,OAAO;YACT,CAAC;YAED,MAAM,IAAI,GAAG,GAAG,CAAC,OAAO,CAAC,IAAI,IAAI,gBAAgB,CAAC;YAClD,MAAM,QAAQ,GAAI,GAAG,CAAC,OAAO,CAAC,mBAAmB,CAAY,IAAI,MAAM,CAAC;YACxE,MAAM,OAAO,GAAG,GAAG,QAAQ,MAAM,IAAI,GAAG,QAAQ,EAAE,CAAC;YACnD,MAAM,IAAI,GAAG,MAAM,KAAK,CAAC,OAAO,EAAE;gBAChC,OAAO,EAAE,GAAG,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,GAAG,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,SAAS;aACzE,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC;YAEzB,MAAM,QAAQ,GAAG,kBAAkB,CAAC,IAAI,CAAC,CAAC;YAE1C,GAAG,CAAC,UAAU,GAAG,GAAG,CAAC;YACrB,GAAG,CAAC,SAAS,CAAC,cAAc,EAAE,iCAAiC,CAAC,CAAC;YACjE,GAAG,CAAC,SAAS,CAAC,eAAe,EAAE,UAAU,CAAC,CAAC;YAC3C,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC,CAAC;QACpC,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,GAAG,CAAC,UAAU,GAAG,GAAG,CAAC;YACrB,GAAG,CAAC,SAAS,CAAC,cAAc,EAAE,YAAY,CAAC,CAAC;YAC5C,GAAG,CAAC,GAAG,CAAC,qCAAqC,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;QACnG,CAAC;IACH,CAAC,CAAC;AACJ,CAAC"}
|
package/node/content.ts
CHANGED
package/node/index.ts
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
// Pure HTML→wrapper-shape heuristic, for any build-time caller, so it runs
|
|
2
|
+
// the same find-wrapper implementation as the dev endpoint.
|
|
3
|
+
export {
|
|
4
|
+
type ContentWrapperInfo,
|
|
5
|
+
type DiscoveredWrappers,
|
|
6
|
+
findContentWrapper,
|
|
7
|
+
} from '@conloca/cms-spa/content-wrapper';
|
|
1
8
|
// The MDX component registry scan, for any build-time caller, so it runs the
|
|
2
9
|
// same discovery implementation as the dev endpoint.
|
|
3
10
|
export { discoverMdxComponents, type MdxRegistryScanOptions, toPortableImports } from '../src/discovery/discover.js';
|
|
@@ -11,12 +18,5 @@ export {
|
|
|
11
18
|
export { type HydrationDiscovery, scanForHydratableComponents } from '../src/lib/hydration-scanner.js';
|
|
12
19
|
export { type ConlocaLocales, localesFromAstroI18n, localesFromStarlight } from '../src/locales-helpers.js';
|
|
13
20
|
export { type ConlocaCMSOptions, conlocaCMS } from '../src/plugin-spa.js';
|
|
14
|
-
// Pure HTML→wrapper-shape heuristic, for any build-time caller, so it runs
|
|
15
|
-
// the same find-wrapper implementation as the dev endpoint.
|
|
16
|
-
export {
|
|
17
|
-
type ContentWrapperInfo,
|
|
18
|
-
type DiscoveredWrappers,
|
|
19
|
-
findContentWrapper,
|
|
20
|
-
} from '../src/site-styles/content-wrapper-endpoint.js';
|
|
21
21
|
// Sitemap entries for CMS pages, which a sitemap cannot find by itself when they render on demand.
|
|
22
22
|
export { type AstroUrlSettings, type ConlocaSitemapOptions, conlocaSitemapOptions } from '../src/sitemap-options.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@conloca/astro-cms",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.4",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -92,9 +92,9 @@
|
|
|
92
92
|
"!**/*.tsbuildinfo"
|
|
93
93
|
],
|
|
94
94
|
"dependencies": {
|
|
95
|
-
"@conloca/cms-spa": "0.2.
|
|
96
|
-
"@conloca/content-api": "0.2.
|
|
97
|
-
"@conloca/mdx": "0.2.
|
|
95
|
+
"@conloca/cms-spa": "0.2.4",
|
|
96
|
+
"@conloca/content-api": "0.2.4",
|
|
97
|
+
"@conloca/mdx": "0.2.4",
|
|
98
98
|
"@puckeditor/core": "^0.23.0",
|
|
99
99
|
"@tiptap/core": "^3.31.2",
|
|
100
100
|
"@tiptap/pm": "^3.31.2",
|
|
@@ -137,17 +137,16 @@
|
|
|
137
137
|
"@tiptap/extension-text-align": "3.31.2",
|
|
138
138
|
"@tiptap/extension-underline": "3.31.2",
|
|
139
139
|
"@tiptap/html": "3.31.2",
|
|
140
|
-
"@ttsc/unplugin": "
|
|
140
|
+
"@ttsc/unplugin": "0.34.1",
|
|
141
141
|
"@vitejs/plugin-react": "^5.2.0",
|
|
142
142
|
"astro": "^7.3.1",
|
|
143
143
|
"fast-check": "^4.9.0",
|
|
144
|
-
"ttsc": "0.
|
|
144
|
+
"ttsc": "0.34.1",
|
|
145
145
|
"typia": "^14.0.4",
|
|
146
146
|
"vite": "^7.3.6"
|
|
147
147
|
},
|
|
148
148
|
"nx": {
|
|
149
149
|
"targets": {
|
|
150
|
-
"clean": {},
|
|
151
150
|
"bun-web": {
|
|
152
151
|
"executor": "nx:run-commands",
|
|
153
152
|
"cache": true,
|
package/src/checks.ts
CHANGED
|
@@ -1,11 +1,21 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Pure project-health helpers shared with
|
|
3
|
-
*
|
|
4
|
-
* node:path only, so importing it never pulls in the
|
|
2
|
+
* Pure project-health helpers shared with the conloca CLI (doctor, page create,
|
|
3
|
+
* asset add and routes) and the editor-page build. This entry must stay a leaf:
|
|
4
|
+
* node:fs, node:path and node:url only, so importing it never pulls in the
|
|
5
|
+
* astro integration graph.
|
|
5
6
|
*/
|
|
7
|
+
export { deriveAssetRefBase } from './asset-ref-base.js';
|
|
6
8
|
export {
|
|
7
9
|
CONTENT_CONFIG_SNIPPET,
|
|
8
10
|
type ContentConfigProblem,
|
|
9
11
|
findContentConfig,
|
|
10
12
|
findContentConfigProblem,
|
|
11
13
|
} from './lib/content-config-guard.js';
|
|
14
|
+
export {
|
|
15
|
+
type AstroI18nLocales,
|
|
16
|
+
collectStaticPageRoutes,
|
|
17
|
+
nonDefaultLocalesOf,
|
|
18
|
+
type StaticPageRoute,
|
|
19
|
+
type StaticPageRoutesInput,
|
|
20
|
+
} from './lib/static-page-routes.js';
|
|
21
|
+
export { isSlotContent } from './lib/unknown-components.js';
|
package/src/collections.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { isAbsolute } from 'node:path';
|
|
1
2
|
import { readEditorDataBindings } from '@conloca/content-api';
|
|
2
3
|
import type { MdxRegistryScanOptions } from './discovery/discover.js';
|
|
3
4
|
import type { EditorDataBindings, TemplateConfig } from './types.js';
|
|
@@ -6,23 +7,41 @@ import type { EditorDataBindings, TemplateConfig } from './types.js';
|
|
|
6
7
|
* Env var through which `conlocaCMS` publishes the editor descriptors it
|
|
7
8
|
* resolved during an Astro build, alongside `CONLOCA_CONTENT_ROOT`.
|
|
8
9
|
*
|
|
9
|
-
* The CMS resolves them once — the site's schemas module,
|
|
10
|
-
* scan inputs, the page templates and the routing
|
|
11
|
-
* integration in the same build reads the SAME
|
|
12
|
-
* `schemasPath`, `/api/registry` and its UI config
|
|
13
|
-
* options a second time.
|
|
10
|
+
* The CMS resolves them once — the site's schemas module, its editor-build
|
|
11
|
+
* options, the MDX registry scan inputs, the page templates and the routing
|
|
12
|
+
* data bindings — so another integration in the same build reads the SAME
|
|
13
|
+
* descriptors dev serves from `schemasPath`, `/api/registry` and its UI config
|
|
14
|
+
* instead of taking those options a second time.
|
|
14
15
|
*/
|
|
15
16
|
export const EDITOR_DESCRIPTORS_ENV_VAR = 'CONLOCA_EDITOR_DESCRIPTORS';
|
|
16
17
|
|
|
17
18
|
export interface EditorDescriptorChannel {
|
|
18
19
|
/** Absolute path of the host's schemas module (the `schemasPath` option), when configured. */
|
|
19
20
|
schemasPath?: string;
|
|
21
|
+
/**
|
|
22
|
+
* Absolute path of the puck config module (the `puckConfigPath` option).
|
|
23
|
+
* Optional only because an older `@conloca/astro-cms` wrote no such field;
|
|
24
|
+
* this version always publishes it.
|
|
25
|
+
*/
|
|
26
|
+
puckConfigPath?: string;
|
|
27
|
+
/** Absolute paths of the `siteStyles` option, resolved against the Astro root, one entry per stylesheet. */
|
|
28
|
+
siteStyles?: string[];
|
|
29
|
+
/** Absolute paths of `editorStyles`, each resolved against the Astro root. */
|
|
30
|
+
editorStyles?: string[];
|
|
31
|
+
/** The `editorCSS` option, verbatim. */
|
|
32
|
+
editorCSS?: string;
|
|
20
33
|
/** Resolved inputs for the MDX component registry scan (`mdxComponentFolders` + the MDX content root). */
|
|
21
34
|
registry: MdxRegistryScanOptions;
|
|
22
35
|
/** Page-creation templates (the `templates` option), when configured. */
|
|
23
36
|
templates?: Record<string, TemplateConfig>;
|
|
24
37
|
/** The routing data bindings each page's data context is built from, when the site has content routes. */
|
|
25
38
|
dataBindings?: EditorDataBindings;
|
|
39
|
+
/** Absolute paths of every folder the content API writes to: the content root, the MDX pages root, the media folder. */
|
|
40
|
+
contentPaths?: string[];
|
|
41
|
+
/** Absolute path of the folder uploaded images go to: the `assetsPath` option, else `<publicDir>/assets`. */
|
|
42
|
+
assetsPath?: string;
|
|
43
|
+
/** The URL the site serves that folder at, which every stored image link starts with. */
|
|
44
|
+
assetRefBase?: string;
|
|
26
45
|
}
|
|
27
46
|
|
|
28
47
|
/**
|
|
@@ -44,9 +63,16 @@ export function readEditorDescriptors(
|
|
|
44
63
|
}
|
|
45
64
|
if (typeof parsed !== 'object' || parsed === null) return undefined;
|
|
46
65
|
const schemasPath = 'schemasPath' in parsed ? parsed.schemasPath : undefined;
|
|
66
|
+
const puckConfigPath = 'puckConfigPath' in parsed ? parsed.puckConfigPath : undefined;
|
|
67
|
+
const styleValue = 'siteStyles' in parsed ? parsed.siteStyles : undefined;
|
|
68
|
+
const editorStyleValue = 'editorStyles' in parsed ? parsed.editorStyles : undefined;
|
|
69
|
+
const editorCSS = 'editorCSS' in parsed ? parsed.editorCSS : undefined;
|
|
47
70
|
const registry = 'registry' in parsed ? parsed.registry : undefined;
|
|
48
71
|
const templateValue = 'templates' in parsed ? parsed.templates : undefined;
|
|
49
72
|
const dataBindingValue = 'dataBindings' in parsed ? parsed.dataBindings : undefined;
|
|
73
|
+
const contentPathValue = 'contentPaths' in parsed ? parsed.contentPaths : undefined;
|
|
74
|
+
const assetsPath = 'assetsPath' in parsed ? parsed.assetsPath : undefined;
|
|
75
|
+
const assetRefBase = 'assetRefBase' in parsed ? parsed.assetRefBase : undefined;
|
|
50
76
|
if (typeof registry !== 'object' || registry === null) return undefined;
|
|
51
77
|
const contentRoot = 'contentRoot' in registry ? registry.contentRoot : undefined;
|
|
52
78
|
const componentFolders = 'componentFolders' in registry ? registry.componentFolders : undefined;
|
|
@@ -61,8 +87,25 @@ export function readEditorDescriptors(
|
|
|
61
87
|
}
|
|
62
88
|
const templates = readTemplates(templateValue);
|
|
63
89
|
const dataBindings = readEditorDataBindings(dataBindingValue);
|
|
90
|
+
const siteStyles =
|
|
91
|
+
Array.isArray(styleValue) && styleValue.every((path): path is string => typeof path === 'string')
|
|
92
|
+
? styleValue
|
|
93
|
+
: undefined;
|
|
94
|
+
const editorStyles =
|
|
95
|
+
Array.isArray(editorStyleValue) && editorStyleValue.every((path): path is string => typeof path === 'string')
|
|
96
|
+
? editorStyleValue
|
|
97
|
+
: undefined;
|
|
98
|
+
const contentPaths =
|
|
99
|
+
Array.isArray(contentPathValue) &&
|
|
100
|
+
contentPathValue.every((path): path is string => typeof path === 'string' && isAbsolute(path))
|
|
101
|
+
? contentPathValue
|
|
102
|
+
: undefined;
|
|
64
103
|
return {
|
|
65
104
|
...(typeof schemasPath === 'string' ? { schemasPath } : {}),
|
|
105
|
+
...(typeof puckConfigPath === 'string' ? { puckConfigPath } : {}),
|
|
106
|
+
...(siteStyles === undefined ? {} : { siteStyles }),
|
|
107
|
+
...(editorStyles === undefined ? {} : { editorStyles }),
|
|
108
|
+
...(typeof editorCSS === 'string' ? { editorCSS } : {}),
|
|
66
109
|
registry: {
|
|
67
110
|
contentRoot,
|
|
68
111
|
componentFolders,
|
|
@@ -71,6 +114,9 @@ export function readEditorDescriptors(
|
|
|
71
114
|
},
|
|
72
115
|
...(templates === undefined ? {} : { templates }),
|
|
73
116
|
...(dataBindings === undefined ? {} : { dataBindings }),
|
|
117
|
+
...(contentPaths === undefined ? {} : { contentPaths }),
|
|
118
|
+
...(typeof assetsPath === 'string' && isAbsolute(assetsPath) ? { assetsPath } : {}),
|
|
119
|
+
...(typeof assetRefBase === 'string' ? { assetRefBase } : {}),
|
|
74
120
|
};
|
|
75
121
|
}
|
|
76
122
|
|
package/src/entries.ts
CHANGED
|
@@ -101,6 +101,36 @@ export function conlocaDataEntries<E extends CollectionEntryLike, DS extends Dat
|
|
|
101
101
|
return typedEntries(entries, parse as EntryParser<ConlocaDataEntry<HostSchemaType<DS[K]>>>, collection);
|
|
102
102
|
}
|
|
103
103
|
|
|
104
|
+
/**
|
|
105
|
+
* The validated payload of the one `collection` entry named `name` (in `locale`, when given), so a missing shared
|
|
106
|
+
* entry fails the build instead of rendering empty.
|
|
107
|
+
*/
|
|
108
|
+
export function conlocaDataEntry<DS extends DataSchemas, K extends keyof DS & string>(
|
|
109
|
+
dataSchemas: DS,
|
|
110
|
+
collection: K,
|
|
111
|
+
entries: readonly CollectionEntryLike[],
|
|
112
|
+
name: string,
|
|
113
|
+
locale?: string,
|
|
114
|
+
): HostSchemaType<DS[K]> {
|
|
115
|
+
const matches = conlocaDataEntries(dataSchemas, collection, entries).filter(
|
|
116
|
+
(entry) => entry.data.name === name && (locale === undefined || entry.data.locale === locale),
|
|
117
|
+
);
|
|
118
|
+
const [match] = matches;
|
|
119
|
+
if (!match) {
|
|
120
|
+
const where = locale === undefined ? '' : ` in locale "${locale}"`;
|
|
121
|
+
throw new Error(`[conloca] data collection "${collection}" has no entry named "${name}"${where}`);
|
|
122
|
+
}
|
|
123
|
+
if (matches.length > 1) {
|
|
124
|
+
const locales = matches.map((entry) => entry.data.locale);
|
|
125
|
+
locales.sort();
|
|
126
|
+
throw new Error(
|
|
127
|
+
`[conloca] data collection "${collection}" has ${matches.length} entries named "${name}" (${locales.join(', ')}); pass a locale`,
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
if (match.data.data === undefined) throw new Error(`[conloca] data entry "${name}" in "${collection}" has no data`);
|
|
131
|
+
return match.data.data;
|
|
132
|
+
}
|
|
133
|
+
|
|
104
134
|
/** Fragment collection entries, validated and typed as {@link ConlocaFragmentEntry}. */
|
|
105
135
|
export function conlocaFragmentEntries<E extends CollectionEntryLike>(
|
|
106
136
|
entries: readonly E[],
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { readdir, readFile } from 'node:fs/promises';
|
|
2
|
+
import { extname, join, relative, sep } from 'node:path';
|
|
3
|
+
|
|
4
|
+
export interface StaticPageRoute {
|
|
5
|
+
/** '/', '/pricing', '/team/lead'; never a trailing slash. */
|
|
6
|
+
route: string;
|
|
7
|
+
/** Posix path relative to `pagesDir`, e.g. 'pricing/index.astro'. */
|
|
8
|
+
file: string;
|
|
9
|
+
/** Rendered at build time: under server output only `prerender = true`; otherwise all but `prerender = false`. */
|
|
10
|
+
prerendered: boolean;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface StaticPageRoutesInput {
|
|
14
|
+
/** Absolute path of the site's `src/pages`. */
|
|
15
|
+
pagesDir: string;
|
|
16
|
+
/** Astro `output === 'server'`. */
|
|
17
|
+
serverOutput: boolean;
|
|
18
|
+
/** First path segments to leave out. */
|
|
19
|
+
nonDefaultLocales: readonly string[];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Astro's `config.i18n` locale fields, as Astro gives them. */
|
|
23
|
+
export interface AstroI18nLocales {
|
|
24
|
+
locales: readonly (string | { path: string })[];
|
|
25
|
+
defaultLocale: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const PAGE_EXTENSIONS = new Set(['.astro', '.md', '.mdx', '.html']);
|
|
29
|
+
const PRERENDER_EXPORT = /\bprerender\s*[=:]\s*(true|false)\b/;
|
|
30
|
+
|
|
31
|
+
/** Lists a pages folder's static routes verbatim, leaving out dynamic, underscore, endpoint, error and locale pages. */
|
|
32
|
+
export async function collectStaticPageRoutes(input: StaticPageRoutesInput): Promise<StaticPageRoute[]> {
|
|
33
|
+
const entries = await readdir(input.pagesDir, { recursive: true, withFileTypes: true }).catch(
|
|
34
|
+
(error: NodeJS.ErrnoException) => {
|
|
35
|
+
if (error.code === 'ENOENT') return [];
|
|
36
|
+
throw error;
|
|
37
|
+
},
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
const pages: { route: string; file: string }[] = [];
|
|
41
|
+
for (const entry of entries) {
|
|
42
|
+
if (!entry.isFile() || !PAGE_EXTENSIONS.has(extname(entry.name))) continue;
|
|
43
|
+
const segments = relative(input.pagesDir, join(entry.parentPath, entry.name)).split(sep);
|
|
44
|
+
if (segments.some((s) => s.startsWith('_') || s.startsWith('.') || s.includes('['))) continue;
|
|
45
|
+
|
|
46
|
+
const file = segments.join('/');
|
|
47
|
+
const routeSegments = file.slice(0, -extname(file).length).split('/');
|
|
48
|
+
if (routeSegments[routeSegments.length - 1] === 'index') routeSegments.pop();
|
|
49
|
+
const route = `/${routeSegments.join('/')}`;
|
|
50
|
+
if (route === '/404' || route === '/500' || input.nonDefaultLocales.includes(routeSegments[0] ?? '')) continue;
|
|
51
|
+
pages.push({ route, file });
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const routes = await Promise.all(
|
|
55
|
+
pages.map(async ({ route, file }): Promise<StaticPageRoute> => {
|
|
56
|
+
const source = await readFile(join(input.pagesDir, file), 'utf8').catch(() => '');
|
|
57
|
+
const prerender = PRERENDER_EXPORT.exec(source)?.[1];
|
|
58
|
+
return { route, file, prerendered: input.serverOutput ? prerender === 'true' : prerender !== 'false' };
|
|
59
|
+
}),
|
|
60
|
+
);
|
|
61
|
+
// Code-unit order, not locale order, so the listing is the same on every machine; files are unique per route.
|
|
62
|
+
// oxlint-disable-next-line unicorn/no-array-sort -- the target lib predates toSorted; this sorts a fresh local array.
|
|
63
|
+
routes.sort((a, b) => (a.route === b.route ? (a.file < b.file ? -1 : 1) : a.route < b.route ? -1 : 1));
|
|
64
|
+
return routes;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Locales other than the default: sites.json's global and every site's locales, plus Astro i18n locale paths. */
|
|
68
|
+
export function nonDefaultLocalesOf(
|
|
69
|
+
sites: {
|
|
70
|
+
sites: Record<string, { locales?: readonly string[]; defaultLocale?: string }>;
|
|
71
|
+
globalLocales?: readonly string[];
|
|
72
|
+
},
|
|
73
|
+
site: string,
|
|
74
|
+
i18n?: AstroI18nLocales,
|
|
75
|
+
): string[] {
|
|
76
|
+
const locales = new Set<string>();
|
|
77
|
+
if (Object.keys(sites.sites).includes(site)) {
|
|
78
|
+
const declared = [...(sites.globalLocales ?? []), ...Object.values(sites.sites).flatMap((s) => s.locales ?? [])];
|
|
79
|
+
for (const locale of declared) if (locale !== sites.sites[site]?.defaultLocale) locales.add(locale);
|
|
80
|
+
}
|
|
81
|
+
for (const locale of i18n?.locales ?? []) {
|
|
82
|
+
const path = typeof locale === 'string' ? locale : locale.path;
|
|
83
|
+
if (path !== i18n?.defaultLocale) locales.add(path);
|
|
84
|
+
}
|
|
85
|
+
const sorted = [...locales];
|
|
86
|
+
// oxlint-disable-next-line unicorn/no-array-sort -- the target lib predates toSorted; this sorts a fresh local copy.
|
|
87
|
+
sorted.sort();
|
|
88
|
+
return sorted;
|
|
89
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { isEditorRoute } from '@conloca/content-api/editor-host';
|
|
2
|
+
import type { Connect } from 'vite';
|
|
3
|
+
import { collectStaticPageRoutes, type StaticPageRoutesInput } from './static-page-routes.js';
|
|
4
|
+
|
|
5
|
+
/** Every static page route the editor may offer, prerendered or not: the local list is never published. */
|
|
6
|
+
export async function staticRoutesBody(input: StaticPageRoutesInput): Promise<{ routes: string[] }> {
|
|
7
|
+
const routes = await collectStaticPageRoutes(input);
|
|
8
|
+
return { routes: routes.map((r) => r.route).filter(isEditorRoute) };
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** Dev middleware serving `{ routes }`; scans on every request so added and removed pages show without a restart. */
|
|
12
|
+
export function createStaticRoutesEndpoint(input: () => StaticPageRoutesInput): Connect.NextHandleFunction {
|
|
13
|
+
return async (_req, res) => {
|
|
14
|
+
try {
|
|
15
|
+
const body = await staticRoutesBody(input());
|
|
16
|
+
res.statusCode = 200;
|
|
17
|
+
res.setHeader('content-type', 'application/json; charset=utf-8');
|
|
18
|
+
res.setHeader('cache-control', 'no-store');
|
|
19
|
+
res.end(JSON.stringify(body));
|
|
20
|
+
} catch (err) {
|
|
21
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
22
|
+
console.warn('[conloca] static routes endpoint failed:', message);
|
|
23
|
+
res.statusCode = 500;
|
|
24
|
+
res.setHeader('content-type', 'application/json; charset=utf-8');
|
|
25
|
+
res.end(JSON.stringify({ error: message }));
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
}
|
|
@@ -11,7 +11,7 @@ interface PuckItem {
|
|
|
11
11
|
* config which fields are slots would miss a component the config no longer
|
|
12
12
|
* declares, and that is precisely the case this check exists for.
|
|
13
13
|
*/
|
|
14
|
-
function isSlotContent(value: unknown): value is PuckItem[] {
|
|
14
|
+
export function isSlotContent(value: unknown): value is PuckItem[] {
|
|
15
15
|
return (
|
|
16
16
|
Array.isArray(value) &&
|
|
17
17
|
value.length > 0 &&
|