@conloca/astro-cms 0.1.14 → 0.1.16
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 +65 -5
- package/dist/collections-Bd7EME4b.mjs +129 -0
- package/dist/collections-Bd7EME4b.mjs.map +1 -0
- package/dist/collections.d.mts +3 -3
- package/dist/collections.d.mts.map +1 -1
- package/dist/collections.mjs +2 -56
- package/dist/components.d.mts +6 -31
- package/dist/components.d.mts.map +1 -1
- package/dist/components.mjs +100 -45
- package/dist/components.mjs.map +1 -1
- package/dist/hydration.d.mts +2 -2
- package/dist/hydration.mjs +2 -3
- package/dist/index.d.mts +12 -88
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +19 -797
- package/dist/index.mjs.map +1 -1
- package/dist/internal/acorn-default.mjs +29 -0
- package/dist/{cms-handler.d.mts → internal/cms-handler.d.mts} +0 -1
- package/dist/internal/cms-handler.d.mts.map +1 -0
- package/dist/{cms-handler.mjs → internal/cms-handler.mjs} +57 -8
- package/dist/internal/cms-handler.mjs.map +1 -0
- package/dist/internal/page-handler.astro +541 -0
- package/dist/lib/hydration-script.d.mts +1 -2
- package/dist/lib/hydration-script.d.mts.map +1 -1
- package/dist/lib/hydration-script.mjs +1 -2
- package/dist/lib/hydration-script.mjs.map +1 -1
- package/dist/locales-helpers-BvYYqnJ5.d.mts +64 -0
- package/dist/locales-helpers-BvYYqnJ5.d.mts.map +1 -0
- package/dist/locales-helpers-qiwh7nxS.mjs +54 -0
- package/dist/locales-helpers-qiwh7nxS.mjs.map +1 -0
- package/dist/node/content.d.mts +18 -0
- package/dist/node/content.d.mts.map +1 -0
- package/dist/node/content.d.ts +3 -0
- package/dist/node/content.d.ts.map +1 -0
- package/dist/node/content.js +2 -0
- package/dist/node/content.mjs +2 -0
- package/dist/node/index.d.mts +200 -0
- package/dist/node/index.d.mts.map +1 -0
- package/dist/node/index.d.ts +4 -0
- package/dist/node/index.d.ts.map +1 -0
- package/dist/node/index.js +3 -0
- package/dist/node/index.mjs +2513 -0
- package/dist/node/index.mjs.map +1 -0
- package/dist/{serialize-props-DqfDQeFl.mjs → serialize-props-BHoNpzH6.mjs} +4 -5
- package/dist/{serialize-props-DqfDQeFl.mjs.map → serialize-props-BHoNpzH6.mjs.map} +1 -1
- package/dist/src/cms-handler.d.ts +19 -0
- package/dist/src/cms-handler.d.ts.map +1 -0
- package/dist/src/cms-handler.js +366 -0
- package/dist/src/collections.d.ts +32 -0
- package/dist/src/collections.d.ts.map +1 -0
- package/dist/src/collections.js +50 -0
- package/dist/src/components/HydrationWrapper.d.ts +18 -0
- package/dist/src/components/HydrationWrapper.d.ts.map +1 -0
- package/dist/src/components/HydrationWrapper.js +21 -0
- package/dist/src/components/RenderWithBlocks.d.ts +23 -0
- package/dist/src/components/RenderWithBlocks.d.ts.map +1 -0
- package/dist/src/components/RenderWithBlocks.js +129 -0
- package/dist/src/components/RenderWithHydration.d.ts +17 -0
- package/dist/src/components/RenderWithHydration.d.ts.map +1 -0
- package/dist/src/components/RenderWithHydration.js +36 -0
- package/dist/src/components.d.ts +4 -0
- package/dist/src/components.d.ts.map +1 -0
- package/dist/src/components.js +3 -0
- package/dist/src/discovery/astro-frontmatter.d.ts +18 -0
- package/dist/src/discovery/astro-frontmatter.d.ts.map +1 -0
- package/dist/src/discovery/astro-frontmatter.js +24 -0
- package/dist/src/discovery/extract-props-ts.d.ts +41 -0
- package/dist/src/discovery/extract-props-ts.d.ts.map +1 -0
- package/dist/src/discovery/extract-props-ts.js +231 -0
- package/dist/src/discovery/merge-registry.d.ts +58 -0
- package/dist/src/discovery/merge-registry.d.ts.map +1 -0
- package/dist/src/discovery/merge-registry.js +332 -0
- package/dist/src/discovery/registry-endpoint.d.ts +42 -0
- package/dist/src/discovery/registry-endpoint.d.ts.map +1 -0
- package/dist/src/discovery/registry-endpoint.js +81 -0
- package/dist/src/discovery/scan-components.d.ts +98 -0
- package/dist/src/discovery/scan-components.d.ts.map +1 -0
- package/dist/src/discovery/scan-components.js +399 -0
- package/dist/src/discovery/scan-mdx.d.ts +63 -0
- package/dist/src/discovery/scan-mdx.d.ts.map +1 -0
- package/dist/src/discovery/scan-mdx.js +112 -0
- package/dist/src/discovery/scan-overrides.d.ts +71 -0
- package/dist/src/discovery/scan-overrides.d.ts.map +1 -0
- package/dist/src/discovery/scan-overrides.js +33 -0
- package/dist/src/hydration.d.ts +4 -0
- package/dist/src/hydration.d.ts.map +1 -0
- package/dist/src/hydration.js +4 -0
- package/dist/src/index.d.ts +11 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +25 -0
- package/dist/src/lib/hydration-scanner.d.ts +47 -0
- package/dist/src/lib/hydration-scanner.d.ts.map +1 -0
- package/dist/src/lib/hydration-scanner.js +135 -0
- package/dist/src/lib/hydration-script.d.ts +28 -0
- package/dist/src/lib/hydration-script.d.ts.map +1 -0
- package/dist/src/lib/hydration-script.js +88 -0
- package/dist/src/lib/hydration-utils.d.ts +51 -0
- package/dist/src/lib/hydration-utils.d.ts.map +1 -0
- package/dist/src/lib/hydration-utils.js +83 -0
- package/dist/src/lib/page-api.d.ts +77 -0
- package/dist/src/lib/page-api.d.ts.map +1 -0
- package/dist/src/lib/page-api.js +121 -0
- package/dist/src/lib/route-utils.d.ts +41 -0
- package/dist/src/lib/route-utils.d.ts.map +1 -0
- package/dist/src/lib/route-utils.js +78 -0
- package/dist/src/lib/routing-config.d.ts +32 -0
- package/dist/src/lib/routing-config.d.ts.map +1 -0
- package/dist/src/lib/routing-config.js +93 -0
- package/dist/src/lib/serialize-props.d.ts +6 -0
- package/dist/src/lib/serialize-props.d.ts.map +1 -0
- package/dist/src/lib/serialize-props.js +7 -0
- package/dist/src/lib/virtual-module-generators.d.ts +57 -0
- package/dist/src/lib/virtual-module-generators.d.ts.map +1 -0
- package/dist/src/lib/virtual-module-generators.js +263 -0
- package/dist/src/lib/withHydration.d.ts +51 -0
- package/dist/src/lib/withHydration.d.ts.map +1 -0
- package/dist/src/lib/withHydration.js +49 -0
- package/dist/src/loader.d.ts +14 -0
- package/dist/src/loader.d.ts.map +1 -0
- package/dist/src/loader.js +89 -0
- package/dist/src/locales-helpers.d.ts +61 -0
- package/dist/src/locales-helpers.d.ts.map +1 -0
- package/dist/src/locales-helpers.js +49 -0
- package/dist/src/mime-types.d.ts +3 -0
- package/dist/src/mime-types.d.ts.map +1 -0
- package/dist/src/mime-types.js +19 -0
- package/dist/src/path-validation.d.ts +8 -0
- package/dist/src/path-validation.d.ts.map +1 -0
- package/dist/src/path-validation.js +12 -0
- package/dist/src/plugin-spa.d.ts +164 -0
- package/dist/src/plugin-spa.d.ts.map +1 -0
- package/dist/src/plugin-spa.js +972 -0
- package/dist/src/safe-json-stringify.d.ts +13 -0
- package/dist/src/safe-json-stringify.d.ts.map +1 -0
- package/dist/src/safe-json-stringify.js +6 -0
- package/dist/src/site-styles/content-wrapper-endpoint.d.ts +76 -0
- package/dist/src/site-styles/content-wrapper-endpoint.d.ts.map +1 -0
- package/dist/src/site-styles/content-wrapper-endpoint.js +227 -0
- package/dist/src/site-styles/render-endpoint.d.ts +55 -0
- package/dist/src/site-styles/render-endpoint.d.ts.map +1 -0
- package/dist/src/site-styles/render-endpoint.js +263 -0
- package/dist/src/site-styles/styles-endpoint.d.ts +61 -0
- package/dist/src/site-styles/styles-endpoint.d.ts.map +1 -0
- package/dist/src/site-styles/styles-endpoint.js +158 -0
- package/dist/src/types.d.ts +627 -0
- package/dist/src/types.d.ts.map +1 -0
- package/dist/src/types.js +1 -0
- package/dist/{types-Bsd_qENn.d.mts → types-CS9neadI.d.mts} +193 -3
- package/dist/types-CS9neadI.d.mts.map +1 -0
- package/dist/virtual-modules.d.ts +155 -0
- package/dist/{withHydration-692fMySA.mjs → withHydration-C256ox_6.mjs} +6 -2
- package/dist/withHydration-C256ox_6.mjs.map +1 -0
- package/dist/{withHydration-BGgQDE-H.d.mts → withHydration-DkKpkmzJ.d.mts} +5 -2
- package/dist/withHydration-DkKpkmzJ.d.mts.map +1 -0
- package/package.json +106 -40
- package/dist/cms-handler.d.mts.map +0 -1
- package/dist/cms-handler.mjs.map +0 -1
- package/dist/collections.mjs.map +0 -1
- package/dist/handlers/page-handler.astro +0 -299
- package/dist/loader-W3i9kf3p.mjs +0 -61
- package/dist/loader-W3i9kf3p.mjs.map +0 -1
- package/dist/types-Bsd_qENn.d.mts.map +0 -1
- package/dist/withHydration-692fMySA.mjs.map +0 -1
- package/dist/withHydration-BGgQDE-H.d.mts.map +0 -1
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
export function mergeRegistry(mdxScans, localComponents, overrides) {
|
|
2
|
+
// Key descriptors by `${source}::${exportName}` so we can merge
|
|
3
|
+
// local-folder data with MDX-usage data and so two components
|
|
4
|
+
// with the same name but different sources stay separate.
|
|
5
|
+
const byKey = new Map();
|
|
6
|
+
// Pass 1: seed from local folder scan.
|
|
7
|
+
for (const comp of localComponents) {
|
|
8
|
+
const key = `${comp.source}::${comp.name}`;
|
|
9
|
+
byKey.set(key, new DescriptorBuilder({
|
|
10
|
+
name: comp.name,
|
|
11
|
+
source: comp.source,
|
|
12
|
+
defaultExport: false,
|
|
13
|
+
kind: comp.kind,
|
|
14
|
+
propsFromInterface: comp.props,
|
|
15
|
+
}));
|
|
16
|
+
}
|
|
17
|
+
// Pass 2: seed/enrich from MDX usage scans.
|
|
18
|
+
for (const scan of mdxScans) {
|
|
19
|
+
for (const usage of scan.usages) {
|
|
20
|
+
const imp = scan.imports[usage.name];
|
|
21
|
+
if (!imp)
|
|
22
|
+
continue; // raw HTML tag or unresolved — skip.
|
|
23
|
+
const canonicalName = imp.exportName === 'default' ? usage.name : imp.exportName;
|
|
24
|
+
const key = `${imp.source}::${canonicalName}`;
|
|
25
|
+
let builder = byKey.get(key);
|
|
26
|
+
if (!builder) {
|
|
27
|
+
builder = new DescriptorBuilder({
|
|
28
|
+
name: canonicalName,
|
|
29
|
+
source: imp.source,
|
|
30
|
+
defaultExport: imp.defaultExport,
|
|
31
|
+
kind: usage.kind,
|
|
32
|
+
});
|
|
33
|
+
byKey.set(key, builder);
|
|
34
|
+
}
|
|
35
|
+
builder.observeUsage(usage.attrs, scan.filepath, usage.name, imp, usage.kind);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
// Resolve name collisions: when two (source, name) pairs share a
|
|
39
|
+
// `name`, the editor registry can only key one descriptor by that
|
|
40
|
+
// name. Pick the source that has the most MDX usage; the dropped
|
|
41
|
+
// alternative isn't lost forever — it just doesn't surface in the
|
|
42
|
+
// registry until the disambiguation UI exists.
|
|
43
|
+
const grouped = new Map();
|
|
44
|
+
for (const builder of byKey.values()) {
|
|
45
|
+
const arr = grouped.get(builder.name) ?? [];
|
|
46
|
+
arr.push(builder);
|
|
47
|
+
grouped.set(builder.name, arr);
|
|
48
|
+
}
|
|
49
|
+
const winners = [];
|
|
50
|
+
for (const candidates of grouped.values()) {
|
|
51
|
+
candidates.sort((a, b) => {
|
|
52
|
+
// Higher actual MDX usage wins.
|
|
53
|
+
if (a.usageCount !== b.usageCount)
|
|
54
|
+
return b.usageCount - a.usageCount;
|
|
55
|
+
// Tie: prefer the one with an interface-derived schema (more
|
|
56
|
+
// likely to be the right one).
|
|
57
|
+
const aHasSchema = a.hasInterfaceSchema();
|
|
58
|
+
const bHasSchema = b.hasInterfaceSchema();
|
|
59
|
+
if (aHasSchema !== bHasSchema)
|
|
60
|
+
return aHasSchema ? -1 : 1;
|
|
61
|
+
return 0;
|
|
62
|
+
});
|
|
63
|
+
let descriptor = candidates[0].toDescriptor();
|
|
64
|
+
const override = overrides?.get(descriptor.name);
|
|
65
|
+
if (override)
|
|
66
|
+
descriptor = applyOverride(descriptor, override);
|
|
67
|
+
winners.push(descriptor);
|
|
68
|
+
}
|
|
69
|
+
return winners;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Apply a host-provided override to an auto-discovered descriptor.
|
|
73
|
+
*
|
|
74
|
+
* Three kinds of merges happen:
|
|
75
|
+
*
|
|
76
|
+
* 1. **Shallow object merges** for `insert` and `defaults` — host
|
|
77
|
+
* fields overwrite inferred fields one key at a time, so a host
|
|
78
|
+
* can tweak only the label without losing the inferred category.
|
|
79
|
+
*
|
|
80
|
+
* 2. **Per-prop merges** for the `props` array — match by `name`,
|
|
81
|
+
* override individual fields (`required`, `label`, `help`,
|
|
82
|
+
* `options`) while keeping the rest of the prop's inferred
|
|
83
|
+
* schema (type, defaultValue). Props the override doesn't mention
|
|
84
|
+
* stay untouched.
|
|
85
|
+
*
|
|
86
|
+
* 3. **Scalar overrides** for `hasChildren` and `kind` — host value
|
|
87
|
+
* replaces inferred value directly.
|
|
88
|
+
*
|
|
89
|
+
* The override is INTENTIONALLY a subset of `MdxJsxComponentDescriptor`
|
|
90
|
+
* — fields the host can sensibly customize. Things like `name` and
|
|
91
|
+
* `import` come from the actual file system / MDX scan and can't be
|
|
92
|
+
* meaningfully overridden by a sidecar JSON.
|
|
93
|
+
*/
|
|
94
|
+
function applyOverride(descriptor, override) {
|
|
95
|
+
const next = { ...descriptor };
|
|
96
|
+
if (override.insert) {
|
|
97
|
+
next.insert = { ...(descriptor.insert ?? { label: descriptor.name }), ...override.insert };
|
|
98
|
+
}
|
|
99
|
+
if (override.defaults) {
|
|
100
|
+
next.defaults = {
|
|
101
|
+
...(descriptor.defaults ?? {}),
|
|
102
|
+
...override.defaults,
|
|
103
|
+
...(override.defaults.attributes
|
|
104
|
+
? { attributes: { ...(descriptor.defaults?.attributes ?? {}), ...override.defaults.attributes } }
|
|
105
|
+
: {}),
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
if (override.hasChildren !== undefined) {
|
|
109
|
+
next.hasChildren = override.hasChildren;
|
|
110
|
+
}
|
|
111
|
+
if (override.kind) {
|
|
112
|
+
next.kind = override.kind;
|
|
113
|
+
}
|
|
114
|
+
if (override.props && descriptor.props) {
|
|
115
|
+
const overridesByName = override.props;
|
|
116
|
+
next.props = descriptor.props.map((p) => {
|
|
117
|
+
const o = overridesByName[p.name];
|
|
118
|
+
if (!o)
|
|
119
|
+
return p;
|
|
120
|
+
// Cast through string-type narrowing — `options` and `defaultValue`
|
|
121
|
+
// shape vary per prop type, so we merge field-by-field and
|
|
122
|
+
// preserve the discriminated union by keeping the original
|
|
123
|
+
// `type` value.
|
|
124
|
+
const merged = { ...p };
|
|
125
|
+
if (o.required !== undefined)
|
|
126
|
+
merged.required = o.required;
|
|
127
|
+
if (o.label !== undefined)
|
|
128
|
+
merged.label = o.label;
|
|
129
|
+
if (o.help !== undefined)
|
|
130
|
+
merged.help = o.help;
|
|
131
|
+
if (o.options && merged.type === 'string') {
|
|
132
|
+
merged.options =
|
|
133
|
+
o.options;
|
|
134
|
+
}
|
|
135
|
+
return merged;
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
return next;
|
|
139
|
+
}
|
|
140
|
+
/** Internal accumulator. Combines interface-derived schema with
|
|
141
|
+
* usage-derived observations as scans arrive. */
|
|
142
|
+
class DescriptorBuilder {
|
|
143
|
+
name;
|
|
144
|
+
source;
|
|
145
|
+
defaultExport;
|
|
146
|
+
/**
|
|
147
|
+
* Component flow/text kind. Mutable across `observeUsage` calls
|
|
148
|
+
* because mdast classifies the SAME component differently based on
|
|
149
|
+
* how each instance is authored in source: `<Steps>...</Steps>` on
|
|
150
|
+
* one line parses as `mdxJsxTextElement` (inline), but the same
|
|
151
|
+
* component with blank-line-separated children parses as
|
|
152
|
+
* `mdxJsxFlowElement` (block). The component's intrinsic nature is
|
|
153
|
+
* "block" — so any single flow observation must promote the kind
|
|
154
|
+
* from text to flow. Without this, one stray inline usage in a
|
|
155
|
+
* fixture poisons the registry, MDXEditor mounts the JSX as inline,
|
|
156
|
+
* and saves serialize block components as single-line MDX — which
|
|
157
|
+
* Starlight's strict-slot validators (Steps wants `<ol>`, FileTree
|
|
158
|
+
* wants `<ul>`) then reject at render time.
|
|
159
|
+
*
|
|
160
|
+
* Demotion in the other direction never happens: a component seen
|
|
161
|
+
* as flow even once is intrinsically flow-capable, regardless of
|
|
162
|
+
* other inline observations.
|
|
163
|
+
*/
|
|
164
|
+
kind;
|
|
165
|
+
propsFromInterface;
|
|
166
|
+
/** propName → set of distinct string values observed in MDX. */
|
|
167
|
+
observedValues = new Map();
|
|
168
|
+
/** filepath → local alias used in that file (when ≠ canonical). */
|
|
169
|
+
aliases = {};
|
|
170
|
+
/** Names of props we've seen in usage but that aren't in the
|
|
171
|
+
* interface (or for npm components where there's no interface). */
|
|
172
|
+
observedPropNames = new Set();
|
|
173
|
+
/** How many `<Foo>` usages of this component were seen across all
|
|
174
|
+
* MDX files. Used to pick a winner on name collisions. */
|
|
175
|
+
usageCount = 0;
|
|
176
|
+
constructor(init) {
|
|
177
|
+
this.name = init.name;
|
|
178
|
+
this.source = init.source;
|
|
179
|
+
this.defaultExport = init.defaultExport;
|
|
180
|
+
this.kind = init.kind;
|
|
181
|
+
this.propsFromInterface = init.propsFromInterface ?? [];
|
|
182
|
+
}
|
|
183
|
+
observeUsage(attrs, filepath, localName, imp, observedKind) {
|
|
184
|
+
this.usageCount++;
|
|
185
|
+
// Flow-wins promotion (see kind field doc above).
|
|
186
|
+
if (observedKind === 'flow')
|
|
187
|
+
this.kind = 'flow';
|
|
188
|
+
if (imp.aliasedFrom && localName !== this.name) {
|
|
189
|
+
this.aliases[filepath] = localName;
|
|
190
|
+
}
|
|
191
|
+
for (const [propName, value] of Object.entries(attrs)) {
|
|
192
|
+
this.observedPropNames.add(propName);
|
|
193
|
+
if (!this.observedValues.has(propName))
|
|
194
|
+
this.observedValues.set(propName, new Set());
|
|
195
|
+
this.observedValues.get(propName).add(value);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
hasInterfaceSchema() {
|
|
199
|
+
return this.propsFromInterface.length > 0;
|
|
200
|
+
}
|
|
201
|
+
toDescriptor() {
|
|
202
|
+
const interfacePropNames = new Set(this.propsFromInterface.map((p) => p.name));
|
|
203
|
+
const props = [];
|
|
204
|
+
// Interface props first — full schema with types and (interface-
|
|
205
|
+
// declared) options.
|
|
206
|
+
for (const ip of this.propsFromInterface) {
|
|
207
|
+
props.push(toMdxComponentProp(ip, this.observedValues.get(ip.name)));
|
|
208
|
+
}
|
|
209
|
+
// Usage-observed props that weren't in the interface (or were
|
|
210
|
+
// never in any interface — npm components). Inferred as plain
|
|
211
|
+
// strings; we don't auto-promote observed values to a dropdown
|
|
212
|
+
// because there's no interface declaration telling us the prop
|
|
213
|
+
// is meant to be enum-like. Authors who want a curated picker
|
|
214
|
+
// can declare options via the sidecar `cmsConfig` override
|
|
215
|
+
// (see scan-overrides.ts).
|
|
216
|
+
for (const name of this.observedPropNames) {
|
|
217
|
+
if (interfacePropNames.has(name))
|
|
218
|
+
continue;
|
|
219
|
+
props.push({ name, type: 'string' });
|
|
220
|
+
}
|
|
221
|
+
// Build `defaults.attributes` for every required prop. The insert
|
|
222
|
+
// flow (buildInsertPayload) merges these as starter attributes when
|
|
223
|
+
// a fresh `<Component>` is dropped in, so components like
|
|
224
|
+
// `<TabItem label="...">` never get inserted as `<TabItem />` and
|
|
225
|
+
// immediately fail Astro's component validation at render time.
|
|
226
|
+
//
|
|
227
|
+
// Placeholder priority per prop:
|
|
228
|
+
// 1. First literal-union option if the prop has one (Aside.type
|
|
229
|
+
// → 'note'). The component's own type system says this is a
|
|
230
|
+
// valid value, so it's the safest choice.
|
|
231
|
+
// 2. First observed usage value if any author file already passes
|
|
232
|
+
// something (TabItem.label → 'Bun' from a Starlight fixture).
|
|
233
|
+
// Reuses authored content as the starter so the insert looks
|
|
234
|
+
// stylistically consistent with the rest of the project.
|
|
235
|
+
// 3. Title-cased prop name as final fallback (Card.title →
|
|
236
|
+
// 'Title'). Always editable — the placeholder text becomes the
|
|
237
|
+
// first thing the author clicks and overwrites.
|
|
238
|
+
const requiredAttributes = {};
|
|
239
|
+
for (const p of props) {
|
|
240
|
+
if (!p.required)
|
|
241
|
+
continue;
|
|
242
|
+
requiredAttributes[p.name] = placeholderForProp(p, this.observedValues.get(p.name));
|
|
243
|
+
}
|
|
244
|
+
const descriptor = {
|
|
245
|
+
name: this.name,
|
|
246
|
+
kind: this.kind,
|
|
247
|
+
hasChildren: true, // default — most components accept a body slot.
|
|
248
|
+
props,
|
|
249
|
+
import: { from: this.source, ...(this.defaultExport ? { default: true } : {}) },
|
|
250
|
+
// Default `insert` metadata so the toolbar's "Insert component"
|
|
251
|
+
// dropdown surfaces every auto-discovered component. Without
|
|
252
|
+
// this, the toolbar's filter (`if (!d.insert) return false`)
|
|
253
|
+
// hides the entry and users have no way to add the block from
|
|
254
|
+
// the UI — only typed-out JSX would work. Hosts can still
|
|
255
|
+
// override label/category via sidecar `cmsConfig` on the
|
|
256
|
+
// component file (when that lands), but the sensible default
|
|
257
|
+
// is the component's own name in a single "Components" bucket.
|
|
258
|
+
insert: { label: this.name, category: 'Components' },
|
|
259
|
+
...(Object.keys(requiredAttributes).length > 0 ? { defaults: { attributes: requiredAttributes } } : {}),
|
|
260
|
+
};
|
|
261
|
+
if (Object.keys(this.aliases).length > 0)
|
|
262
|
+
descriptor.aliases = this.aliases;
|
|
263
|
+
return descriptor;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Pick a sensible starter value for a required prop so the insert flow
|
|
268
|
+
* has something to put on the attribute. See the call site in
|
|
269
|
+
* `DescriptorBuilder.toDescriptor` for the priority rationale; this
|
|
270
|
+
* function just executes it.
|
|
271
|
+
*
|
|
272
|
+
* Number and boolean placeholders are intentionally trivial — `0` and
|
|
273
|
+
* `true` — because numeric/boolean props are rare in MDX components and
|
|
274
|
+
* any concrete value beats `undefined`. The interesting work is on the
|
|
275
|
+
* string path, where union options and observed usage give us real
|
|
276
|
+
* authored values to reuse.
|
|
277
|
+
*/
|
|
278
|
+
function placeholderForProp(prop, observed) {
|
|
279
|
+
if (prop.type === 'number')
|
|
280
|
+
return 0;
|
|
281
|
+
if (prop.type === 'boolean')
|
|
282
|
+
return true;
|
|
283
|
+
// string
|
|
284
|
+
if (prop.options && prop.options.length > 0)
|
|
285
|
+
return prop.options[0].value;
|
|
286
|
+
if (observed && observed.size > 0) {
|
|
287
|
+
const first = observed.values().next();
|
|
288
|
+
if (!first.done)
|
|
289
|
+
return first.value;
|
|
290
|
+
}
|
|
291
|
+
// Title-case the prop name (`label` → `Label`). The author sees this
|
|
292
|
+
// as the placeholder text on first insert; one click selects it for
|
|
293
|
+
// overwrite.
|
|
294
|
+
return prop.name.charAt(0).toUpperCase() + prop.name.slice(1);
|
|
295
|
+
}
|
|
296
|
+
function toMdxComponentProp(parsed, observed) {
|
|
297
|
+
// Only emit dropdown `options` when the interface itself declared a
|
|
298
|
+
// literal union (eg `type?: 'note' | 'tip' | 'caution' | 'danger'`).
|
|
299
|
+
// Free-form string props like `title`, `description`, `href` have no
|
|
300
|
+
// such union — emitting an `options` array based purely on what we
|
|
301
|
+
// observed in usage would lock authors to past values when the field
|
|
302
|
+
// is meant to accept any text. The side panel renders prop fields with
|
|
303
|
+
// options as `<select>` and prop fields without as `<input type=text>`,
|
|
304
|
+
// so this single switch decides which one shows up. Observed values
|
|
305
|
+
// can still enrich a real enum: when `parsed.options` IS set, append
|
|
306
|
+
// any usage-seen values the interface didn't list (capped at 5 distinct).
|
|
307
|
+
let optionStrings = parsed.options;
|
|
308
|
+
if (optionStrings && observed && observed.size > 0) {
|
|
309
|
+
const combined = new Set(optionStrings);
|
|
310
|
+
for (const v of observed)
|
|
311
|
+
combined.add(v);
|
|
312
|
+
if (combined.size <= 5)
|
|
313
|
+
optionStrings = Array.from(combined);
|
|
314
|
+
}
|
|
315
|
+
if (parsed.type === 'number') {
|
|
316
|
+
return { name: parsed.name, type: 'number', required: parsed.required };
|
|
317
|
+
}
|
|
318
|
+
if (parsed.type === 'boolean') {
|
|
319
|
+
return { name: parsed.name, type: 'boolean', required: parsed.required };
|
|
320
|
+
}
|
|
321
|
+
// 'string' and 'expression' both render as a text input by default.
|
|
322
|
+
// Treat 'expression' as a free-form string for editor purposes —
|
|
323
|
+
// it's an escape hatch already.
|
|
324
|
+
return {
|
|
325
|
+
name: parsed.name,
|
|
326
|
+
type: 'string',
|
|
327
|
+
required: parsed.required,
|
|
328
|
+
...(optionStrings && optionStrings.length > 0
|
|
329
|
+
? { options: optionStrings.map((v) => ({ value: v, label: v })) }
|
|
330
|
+
: {}),
|
|
331
|
+
};
|
|
332
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
|
+
/** Connect-compatible middleware handler — see render-endpoint.ts. */
|
|
3
|
+
type NextHandleFunction = (req: IncomingMessage, res: ServerResponse, next: (err?: unknown) => void) => void;
|
|
4
|
+
/**
|
|
5
|
+
* Vite middleware that returns the auto-discovered MDX component
|
|
6
|
+
* registry as JSON. The SPA fetches this on mount and seeds its
|
|
7
|
+
* `setMdxComponents` registry — replacing the hand-written
|
|
8
|
+
* `mdx-components.tsx` flow.
|
|
9
|
+
*
|
|
10
|
+
* Each call runs the three-stage pipeline (scan-mdx + scan-components
|
|
11
|
+
* + merge-registry), with a memoized result keyed on the configured
|
|
12
|
+
* inputs. The result is invalidated by an external watcher (set up
|
|
13
|
+
* in `plugin-spa.ts`) any time a watched MDX or component file
|
|
14
|
+
* changes — so editing a `.mdx` to use a new component refreshes
|
|
15
|
+
* the registry without a manual reload.
|
|
16
|
+
*
|
|
17
|
+
* Errors are returned as JSON `{ error: "..." }` with 500 rather
|
|
18
|
+
* than thrown — the SPA falls back to host-provided descriptors when
|
|
19
|
+
* the endpoint fails, and a noisy stack trace would just spam the
|
|
20
|
+
* dev console.
|
|
21
|
+
*/
|
|
22
|
+
export interface RegistryEndpointOptions {
|
|
23
|
+
/** Absolute or project-relative path to the content directory. */
|
|
24
|
+
contentRoot: string;
|
|
25
|
+
/** Project-relative paths to scan for local components. */
|
|
26
|
+
componentFolders: string[];
|
|
27
|
+
/** Project-relative folder of `*.cms.json` sidecar override files,
|
|
28
|
+
* keyed by component basename (`Card.cms.json` → overrides `Card`).
|
|
29
|
+
* Defaults to `src/cms-overrides` — host can omit when they don't
|
|
30
|
+
* need any overrides yet. */
|
|
31
|
+
overridesFolder?: string;
|
|
32
|
+
/** Used to resolve `componentFolders` relative to it. Defaults to
|
|
33
|
+
* `process.cwd()`. */
|
|
34
|
+
projectRoot?: string;
|
|
35
|
+
}
|
|
36
|
+
export declare function createRegistryEndpoint(options: RegistryEndpointOptions): {
|
|
37
|
+
handler: NextHandleFunction;
|
|
38
|
+
invalidate: () => void;
|
|
39
|
+
getAllowedSources: () => Promise<Set<string>>;
|
|
40
|
+
};
|
|
41
|
+
export {};
|
|
42
|
+
//# sourceMappingURL=registry-endpoint.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"registry-endpoint.d.ts","sourceRoot":"","sources":["../../../src/discovery/registry-endpoint.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAOjE,sEAAsE;AACtE,KAAK,kBAAkB,GAAG,CAAC,GAAG,EAAE,eAAe,EAAE,GAAG,EAAE,cAAc,EAAE,IAAI,EAAE,CAAC,GAAG,CAAC,EAAE,OAAO,KAAK,IAAI,KAAK,IAAI,CAAC;AAE7G;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,WAAW,uBAAuB;IACtC,kEAAkE;IAClE,WAAW,EAAE,MAAM,CAAC;IACpB,2DAA2D;IAC3D,gBAAgB,EAAE,MAAM,EAAE,CAAC;IAC3B;;;iCAG6B;IAC7B,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB;0BACsB;IACtB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,wBAAgB,sBAAsB,CAAC,OAAO,EAAE,uBAAuB,GAAG;IACxE,OAAO,EAAE,kBAAkB,CAAC;IAC5B,UAAU,EAAE,MAAM,IAAI,CAAC;IACvB,iBAAiB,EAAE,MAAM,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC;CAC/C,CA0EA"}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { resolve } from 'node:path';
|
|
2
|
+
import { mergeRegistry } from './merge-registry';
|
|
3
|
+
import { scanExternalComponents, scanLocalComponents } from './scan-components';
|
|
4
|
+
import { scanMdxFiles } from './scan-mdx';
|
|
5
|
+
import { loadCmsOverrides } from './scan-overrides';
|
|
6
|
+
export function createRegistryEndpoint(options) {
|
|
7
|
+
const projectRoot = options.projectRoot ?? process.cwd();
|
|
8
|
+
const contentRoot = resolve(projectRoot, options.contentRoot);
|
|
9
|
+
let cached = null;
|
|
10
|
+
const compute = () => {
|
|
11
|
+
if (cached)
|
|
12
|
+
return cached;
|
|
13
|
+
cached = (async () => {
|
|
14
|
+
// MDX scan first — its results feed BOTH the merge step AND the
|
|
15
|
+
// external-package scan (which uses the imports to know what
|
|
16
|
+
// packages to walk into).
|
|
17
|
+
const mdxScans = await scanMdxFiles(contentRoot);
|
|
18
|
+
const overridesFolder = options.overridesFolder ?? 'src/cms-overrides';
|
|
19
|
+
const [localComponents, externalComponents, overrides] = await Promise.all([
|
|
20
|
+
scanLocalComponents(options.componentFolders, projectRoot),
|
|
21
|
+
scanExternalComponents(mdxScans, projectRoot),
|
|
22
|
+
loadCmsOverrides(overridesFolder, projectRoot),
|
|
23
|
+
]);
|
|
24
|
+
// External (npm) and local components live in disjoint key
|
|
25
|
+
// spaces — external source strings are npm specifiers, local
|
|
26
|
+
// source strings are absolute paths — so they never collide on
|
|
27
|
+
// the `${source}::${name}` dedup key. The interesting collision
|
|
28
|
+
// case is two sources sharing a `name`; that pass is resolved
|
|
29
|
+
// inside `mergeRegistry` by usage count (see rule 6 in its top
|
|
30
|
+
// doc). Host sidecar overrides apply last as the highest-priority
|
|
31
|
+
// layer (see `applyOverride` in merge-registry).
|
|
32
|
+
return mergeRegistry(mdxScans, [...externalComponents, ...localComponents], overrides);
|
|
33
|
+
})();
|
|
34
|
+
return cached;
|
|
35
|
+
};
|
|
36
|
+
const handler = async (req, res, _next) => {
|
|
37
|
+
try {
|
|
38
|
+
const components = await compute();
|
|
39
|
+
res.statusCode = 200;
|
|
40
|
+
res.setHeader('content-type', 'application/json; charset=utf-8');
|
|
41
|
+
res.setHeader('cache-control', 'no-store');
|
|
42
|
+
res.end(JSON.stringify({ components }));
|
|
43
|
+
}
|
|
44
|
+
catch (err) {
|
|
45
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
46
|
+
console.warn('[conloca:discovery] registry endpoint failed:', message);
|
|
47
|
+
res.statusCode = 500;
|
|
48
|
+
res.setHeader('content-type', 'application/json; charset=utf-8');
|
|
49
|
+
res.end(JSON.stringify({ error: message }));
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
return {
|
|
53
|
+
handler,
|
|
54
|
+
invalidate: () => {
|
|
55
|
+
cached = null;
|
|
56
|
+
},
|
|
57
|
+
/**
|
|
58
|
+
* Set of import sources the render endpoint is allowed to load.
|
|
59
|
+
*
|
|
60
|
+
* Reuses the same memoized scan as `handler`, so the allowlist stays
|
|
61
|
+
* in sync with the registry the SPA sees — invalidation in one place
|
|
62
|
+
* invalidates both. Failing closed (returning an empty set) on scan
|
|
63
|
+
* error is intentional: better to refuse all renders than to fall
|
|
64
|
+
* back to "trust the request body".
|
|
65
|
+
*/
|
|
66
|
+
getAllowedSources: async () => {
|
|
67
|
+
try {
|
|
68
|
+
const components = await compute();
|
|
69
|
+
const sources = new Set();
|
|
70
|
+
for (const c of components) {
|
|
71
|
+
if (c.import?.from)
|
|
72
|
+
sources.add(c.import.from);
|
|
73
|
+
}
|
|
74
|
+
return sources;
|
|
75
|
+
}
|
|
76
|
+
catch {
|
|
77
|
+
return new Set();
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import type { MdxScanResult } from './scan-mdx';
|
|
2
|
+
/**
|
|
3
|
+
* Walks configured folder globs for `.astro` and `.tsx` files,
|
|
4
|
+
* extracts each component's `Props` interface, and returns a
|
|
5
|
+
* descriptor-friendly schema. Used alongside `scan-mdx.ts` to
|
|
6
|
+
* populate the CMS registry without the host hand-writing
|
|
7
|
+
* `mdx-components.tsx`.
|
|
8
|
+
*
|
|
9
|
+
* Type extraction uses the TypeScript compiler API (via ts-morph,
|
|
10
|
+
* see `./extract-props-ts.ts`). Derived types — `(typeof X)[number]`,
|
|
11
|
+
* imported aliases, mapped types, conditional types — all resolve
|
|
12
|
+
* to their concrete shape, which is what makes the registry feel
|
|
13
|
+
* smart for real component libraries (eg Starlight's Aside, whose
|
|
14
|
+
* `type` prop uses indexed access). For `.astro` files we extract
|
|
15
|
+
* the leading `---` frontmatter block and feed it to the same
|
|
16
|
+
* extractor as a virtual `.ts` source.
|
|
17
|
+
*
|
|
18
|
+
* A single ts-morph Project is built per scan run and shared across
|
|
19
|
+
* every file in that run, so the per-file cost is small after the
|
|
20
|
+
* initial type-checker warmup. Sidecar `cmsConfig` overrides still
|
|
21
|
+
* apply (see `./scan-overrides.ts`) for components whose author
|
|
22
|
+
* wants to override or annotate fields beyond what the type says.
|
|
23
|
+
*
|
|
24
|
+
* Component name = basename of the file without extension.
|
|
25
|
+
* Component source = the resolved file path. The render endpoint
|
|
26
|
+
* hands this to `server.ssrLoadModule`, which accepts absolute
|
|
27
|
+
* paths in dev.
|
|
28
|
+
*/
|
|
29
|
+
export interface ParsedProp {
|
|
30
|
+
name: string;
|
|
31
|
+
/** Inferred from the TypeScript type annotation. `'string'` covers
|
|
32
|
+
* literal-string unions too — `options` holds the union values
|
|
33
|
+
* when applicable so the prop panel can render a dropdown. */
|
|
34
|
+
type: 'string' | 'number' | 'boolean' | 'expression';
|
|
35
|
+
/** True when the prop's TS field lacks a `?` modifier. */
|
|
36
|
+
required: boolean;
|
|
37
|
+
/** Literal-string union values, eg `'note' | 'tip' | 'caution'`. */
|
|
38
|
+
options?: string[];
|
|
39
|
+
}
|
|
40
|
+
export interface LocalComponentScanResult {
|
|
41
|
+
/** Absolute file path of the component. */
|
|
42
|
+
filepath: string;
|
|
43
|
+
/** Component name (filename without extension). */
|
|
44
|
+
name: string;
|
|
45
|
+
/** Path to pass to `server.ssrLoadModule` — currently the absolute
|
|
46
|
+
* file path. The path the import statement should use in MDX is
|
|
47
|
+
* the project-relative form (so save serialization stays portable);
|
|
48
|
+
* we keep both. */
|
|
49
|
+
source: string;
|
|
50
|
+
/** Project-relative path used when emitting `import` statements
|
|
51
|
+
* for newly-inserted components. */
|
|
52
|
+
importSpecifier: string;
|
|
53
|
+
/** All components from this scanner are treated as flow (block).
|
|
54
|
+
* Inline-only components are rare in authored libraries; if any
|
|
55
|
+
* surface as text-level usage they get classified at merge time
|
|
56
|
+
* from the MDX scan, not here. */
|
|
57
|
+
kind: 'flow';
|
|
58
|
+
props: ParsedProp[];
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Scan every `.astro` / `.tsx` file under each configured folder.
|
|
62
|
+
* Folder globs are resolved relative to `projectRoot`.
|
|
63
|
+
*/
|
|
64
|
+
export declare function scanLocalComponents(folders: string[], projectRoot: string): Promise<LocalComponentScanResult[]>;
|
|
65
|
+
/**
|
|
66
|
+
* Scan every npm-style import source referenced in the MDX usage scan
|
|
67
|
+
* and treat any `.astro` / `.tsx` file inside the resolved package as a
|
|
68
|
+
* candidate component. Resolves each unique specifier via Node's
|
|
69
|
+
* `require.resolve` (run from `projectRoot` so packages are looked up
|
|
70
|
+
* in the consumer's `node_modules`, not ours), walks up to the package
|
|
71
|
+
* root, then globs the package for component-shaped files.
|
|
72
|
+
*
|
|
73
|
+
* The returned `source` on each result is the ORIGINAL specifier from
|
|
74
|
+
* the MDX import (e.g. `@astrojs/starlight/components`) — not the
|
|
75
|
+
* resolved file path. That's what makes the merge work: `scan-mdx`
|
|
76
|
+
* records imports by their literal source string, so for the merge
|
|
77
|
+
* registry to dedupe local-folder scan ⨯ MDX-usage scan we need both
|
|
78
|
+
* sides to key on the same specifier. Vite's `ssrLoadModule` handles
|
|
79
|
+
* the specifier itself at render time, so we don't need an absolute
|
|
80
|
+
* file path on the descriptor.
|
|
81
|
+
*
|
|
82
|
+
* Caveats:
|
|
83
|
+
* - Specifiers that look like local paths (`./foo`, `/abs/path`) are
|
|
84
|
+
* intentionally skipped here — those are local components, already
|
|
85
|
+
* covered by `scanLocalComponents`.
|
|
86
|
+
* - One specifier per package: if a package is imported via two
|
|
87
|
+
* different specifiers (`@pkg/components` and `@pkg`), we'll scan
|
|
88
|
+
* it twice and emit two descriptor sets keyed on each specifier.
|
|
89
|
+
* The merge layer's collision resolution picks the one with more
|
|
90
|
+
* real usage, so this is self-correcting in practice.
|
|
91
|
+
* - We cap glob depth at 4 to avoid walking into deeply-nested
|
|
92
|
+
* library internals (most component packages put their exports
|
|
93
|
+
* near the root). Components nested deeper won't surface; hosts
|
|
94
|
+
* can either re-export them at a shallower path or add a sidecar
|
|
95
|
+
* `cmsConfig` (when sidecar support lands).
|
|
96
|
+
*/
|
|
97
|
+
export declare function scanExternalComponents(mdxScans: MdxScanResult[], projectRoot: string): Promise<LocalComponentScanResult[]>;
|
|
98
|
+
//# sourceMappingURL=scan-components.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scan-components.d.ts","sourceRoot":"","sources":["../../../src/discovery/scan-components.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAEhD;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAM,WAAW,UAAU;IACzB,IAAI,EAAE,MAAM,CAAC;IACb;;kEAE8D;IAC9D,IAAI,EAAE,QAAQ,GAAG,QAAQ,GAAG,SAAS,GAAG,YAAY,CAAC;IACrD,0DAA0D;IAC1D,QAAQ,EAAE,OAAO,CAAC;IAClB,oEAAoE;IACpE,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;CACpB;AAED,MAAM,WAAW,wBAAwB;IACvC,2CAA2C;IAC3C,QAAQ,EAAE,MAAM,CAAC;IACjB,mDAAmD;IACnD,IAAI,EAAE,MAAM,CAAC;IACb;;;uBAGmB;IACnB,MAAM,EAAE,MAAM,CAAC;IACf;wCACoC;IACpC,eAAe,EAAE,MAAM,CAAC;IACxB;;;sCAGkC;IAClC,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,UAAU,EAAE,CAAC;CACrB;AAED;;;GAGG;AACH,wBAAsB,mBAAmB,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE,WAAW,EAAE,MAAM,GAAG,OAAO,CAAC,wBAAwB,EAAE,CAAC,CA0CrH;AA6ED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,wBAAsB,sBAAsB,CAC1C,QAAQ,EAAE,aAAa,EAAE,EACzB,WAAW,EAAE,MAAM,GAClB,OAAO,CAAC,wBAAwB,EAAE,CAAC,CAoFrC"}
|