@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.
Files changed (164) hide show
  1. package/README.md +65 -5
  2. package/dist/collections-Bd7EME4b.mjs +129 -0
  3. package/dist/collections-Bd7EME4b.mjs.map +1 -0
  4. package/dist/collections.d.mts +3 -3
  5. package/dist/collections.d.mts.map +1 -1
  6. package/dist/collections.mjs +2 -56
  7. package/dist/components.d.mts +6 -31
  8. package/dist/components.d.mts.map +1 -1
  9. package/dist/components.mjs +100 -45
  10. package/dist/components.mjs.map +1 -1
  11. package/dist/hydration.d.mts +2 -2
  12. package/dist/hydration.mjs +2 -3
  13. package/dist/index.d.mts +12 -88
  14. package/dist/index.d.mts.map +1 -1
  15. package/dist/index.mjs +19 -797
  16. package/dist/index.mjs.map +1 -1
  17. package/dist/internal/acorn-default.mjs +29 -0
  18. package/dist/{cms-handler.d.mts → internal/cms-handler.d.mts} +0 -1
  19. package/dist/internal/cms-handler.d.mts.map +1 -0
  20. package/dist/{cms-handler.mjs → internal/cms-handler.mjs} +57 -8
  21. package/dist/internal/cms-handler.mjs.map +1 -0
  22. package/dist/internal/page-handler.astro +541 -0
  23. package/dist/lib/hydration-script.d.mts +1 -2
  24. package/dist/lib/hydration-script.d.mts.map +1 -1
  25. package/dist/lib/hydration-script.mjs +1 -2
  26. package/dist/lib/hydration-script.mjs.map +1 -1
  27. package/dist/locales-helpers-BvYYqnJ5.d.mts +64 -0
  28. package/dist/locales-helpers-BvYYqnJ5.d.mts.map +1 -0
  29. package/dist/locales-helpers-qiwh7nxS.mjs +54 -0
  30. package/dist/locales-helpers-qiwh7nxS.mjs.map +1 -0
  31. package/dist/node/content.d.mts +18 -0
  32. package/dist/node/content.d.mts.map +1 -0
  33. package/dist/node/content.d.ts +3 -0
  34. package/dist/node/content.d.ts.map +1 -0
  35. package/dist/node/content.js +2 -0
  36. package/dist/node/content.mjs +2 -0
  37. package/dist/node/index.d.mts +200 -0
  38. package/dist/node/index.d.mts.map +1 -0
  39. package/dist/node/index.d.ts +4 -0
  40. package/dist/node/index.d.ts.map +1 -0
  41. package/dist/node/index.js +3 -0
  42. package/dist/node/index.mjs +2513 -0
  43. package/dist/node/index.mjs.map +1 -0
  44. package/dist/{serialize-props-DqfDQeFl.mjs → serialize-props-BHoNpzH6.mjs} +4 -5
  45. package/dist/{serialize-props-DqfDQeFl.mjs.map → serialize-props-BHoNpzH6.mjs.map} +1 -1
  46. package/dist/src/cms-handler.d.ts +19 -0
  47. package/dist/src/cms-handler.d.ts.map +1 -0
  48. package/dist/src/cms-handler.js +366 -0
  49. package/dist/src/collections.d.ts +32 -0
  50. package/dist/src/collections.d.ts.map +1 -0
  51. package/dist/src/collections.js +50 -0
  52. package/dist/src/components/HydrationWrapper.d.ts +18 -0
  53. package/dist/src/components/HydrationWrapper.d.ts.map +1 -0
  54. package/dist/src/components/HydrationWrapper.js +21 -0
  55. package/dist/src/components/RenderWithBlocks.d.ts +23 -0
  56. package/dist/src/components/RenderWithBlocks.d.ts.map +1 -0
  57. package/dist/src/components/RenderWithBlocks.js +129 -0
  58. package/dist/src/components/RenderWithHydration.d.ts +17 -0
  59. package/dist/src/components/RenderWithHydration.d.ts.map +1 -0
  60. package/dist/src/components/RenderWithHydration.js +36 -0
  61. package/dist/src/components.d.ts +4 -0
  62. package/dist/src/components.d.ts.map +1 -0
  63. package/dist/src/components.js +3 -0
  64. package/dist/src/discovery/astro-frontmatter.d.ts +18 -0
  65. package/dist/src/discovery/astro-frontmatter.d.ts.map +1 -0
  66. package/dist/src/discovery/astro-frontmatter.js +24 -0
  67. package/dist/src/discovery/extract-props-ts.d.ts +41 -0
  68. package/dist/src/discovery/extract-props-ts.d.ts.map +1 -0
  69. package/dist/src/discovery/extract-props-ts.js +231 -0
  70. package/dist/src/discovery/merge-registry.d.ts +58 -0
  71. package/dist/src/discovery/merge-registry.d.ts.map +1 -0
  72. package/dist/src/discovery/merge-registry.js +332 -0
  73. package/dist/src/discovery/registry-endpoint.d.ts +42 -0
  74. package/dist/src/discovery/registry-endpoint.d.ts.map +1 -0
  75. package/dist/src/discovery/registry-endpoint.js +81 -0
  76. package/dist/src/discovery/scan-components.d.ts +98 -0
  77. package/dist/src/discovery/scan-components.d.ts.map +1 -0
  78. package/dist/src/discovery/scan-components.js +399 -0
  79. package/dist/src/discovery/scan-mdx.d.ts +63 -0
  80. package/dist/src/discovery/scan-mdx.d.ts.map +1 -0
  81. package/dist/src/discovery/scan-mdx.js +112 -0
  82. package/dist/src/discovery/scan-overrides.d.ts +71 -0
  83. package/dist/src/discovery/scan-overrides.d.ts.map +1 -0
  84. package/dist/src/discovery/scan-overrides.js +33 -0
  85. package/dist/src/hydration.d.ts +4 -0
  86. package/dist/src/hydration.d.ts.map +1 -0
  87. package/dist/src/hydration.js +4 -0
  88. package/dist/src/index.d.ts +11 -0
  89. package/dist/src/index.d.ts.map +1 -0
  90. package/dist/src/index.js +25 -0
  91. package/dist/src/lib/hydration-scanner.d.ts +47 -0
  92. package/dist/src/lib/hydration-scanner.d.ts.map +1 -0
  93. package/dist/src/lib/hydration-scanner.js +135 -0
  94. package/dist/src/lib/hydration-script.d.ts +28 -0
  95. package/dist/src/lib/hydration-script.d.ts.map +1 -0
  96. package/dist/src/lib/hydration-script.js +88 -0
  97. package/dist/src/lib/hydration-utils.d.ts +51 -0
  98. package/dist/src/lib/hydration-utils.d.ts.map +1 -0
  99. package/dist/src/lib/hydration-utils.js +83 -0
  100. package/dist/src/lib/page-api.d.ts +77 -0
  101. package/dist/src/lib/page-api.d.ts.map +1 -0
  102. package/dist/src/lib/page-api.js +121 -0
  103. package/dist/src/lib/route-utils.d.ts +41 -0
  104. package/dist/src/lib/route-utils.d.ts.map +1 -0
  105. package/dist/src/lib/route-utils.js +78 -0
  106. package/dist/src/lib/routing-config.d.ts +32 -0
  107. package/dist/src/lib/routing-config.d.ts.map +1 -0
  108. package/dist/src/lib/routing-config.js +93 -0
  109. package/dist/src/lib/serialize-props.d.ts +6 -0
  110. package/dist/src/lib/serialize-props.d.ts.map +1 -0
  111. package/dist/src/lib/serialize-props.js +7 -0
  112. package/dist/src/lib/virtual-module-generators.d.ts +57 -0
  113. package/dist/src/lib/virtual-module-generators.d.ts.map +1 -0
  114. package/dist/src/lib/virtual-module-generators.js +263 -0
  115. package/dist/src/lib/withHydration.d.ts +51 -0
  116. package/dist/src/lib/withHydration.d.ts.map +1 -0
  117. package/dist/src/lib/withHydration.js +49 -0
  118. package/dist/src/loader.d.ts +14 -0
  119. package/dist/src/loader.d.ts.map +1 -0
  120. package/dist/src/loader.js +89 -0
  121. package/dist/src/locales-helpers.d.ts +61 -0
  122. package/dist/src/locales-helpers.d.ts.map +1 -0
  123. package/dist/src/locales-helpers.js +49 -0
  124. package/dist/src/mime-types.d.ts +3 -0
  125. package/dist/src/mime-types.d.ts.map +1 -0
  126. package/dist/src/mime-types.js +19 -0
  127. package/dist/src/path-validation.d.ts +8 -0
  128. package/dist/src/path-validation.d.ts.map +1 -0
  129. package/dist/src/path-validation.js +12 -0
  130. package/dist/src/plugin-spa.d.ts +164 -0
  131. package/dist/src/plugin-spa.d.ts.map +1 -0
  132. package/dist/src/plugin-spa.js +972 -0
  133. package/dist/src/safe-json-stringify.d.ts +13 -0
  134. package/dist/src/safe-json-stringify.d.ts.map +1 -0
  135. package/dist/src/safe-json-stringify.js +6 -0
  136. package/dist/src/site-styles/content-wrapper-endpoint.d.ts +76 -0
  137. package/dist/src/site-styles/content-wrapper-endpoint.d.ts.map +1 -0
  138. package/dist/src/site-styles/content-wrapper-endpoint.js +227 -0
  139. package/dist/src/site-styles/render-endpoint.d.ts +55 -0
  140. package/dist/src/site-styles/render-endpoint.d.ts.map +1 -0
  141. package/dist/src/site-styles/render-endpoint.js +263 -0
  142. package/dist/src/site-styles/styles-endpoint.d.ts +61 -0
  143. package/dist/src/site-styles/styles-endpoint.d.ts.map +1 -0
  144. package/dist/src/site-styles/styles-endpoint.js +158 -0
  145. package/dist/src/types.d.ts +627 -0
  146. package/dist/src/types.d.ts.map +1 -0
  147. package/dist/src/types.js +1 -0
  148. package/dist/{types-Bsd_qENn.d.mts → types-CS9neadI.d.mts} +193 -3
  149. package/dist/types-CS9neadI.d.mts.map +1 -0
  150. package/dist/virtual-modules.d.ts +155 -0
  151. package/dist/{withHydration-692fMySA.mjs → withHydration-C256ox_6.mjs} +6 -2
  152. package/dist/withHydration-C256ox_6.mjs.map +1 -0
  153. package/dist/{withHydration-BGgQDE-H.d.mts → withHydration-DkKpkmzJ.d.mts} +5 -2
  154. package/dist/withHydration-DkKpkmzJ.d.mts.map +1 -0
  155. package/package.json +106 -40
  156. package/dist/cms-handler.d.mts.map +0 -1
  157. package/dist/cms-handler.mjs.map +0 -1
  158. package/dist/collections.mjs.map +0 -1
  159. package/dist/handlers/page-handler.astro +0 -299
  160. package/dist/loader-W3i9kf3p.mjs +0 -61
  161. package/dist/loader-W3i9kf3p.mjs.map +0 -1
  162. package/dist/types-Bsd_qENn.d.mts.map +0 -1
  163. package/dist/withHydration-692fMySA.mjs.map +0 -1
  164. package/dist/withHydration-BGgQDE-H.d.mts.map +0 -1
@@ -0,0 +1,61 @@
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 concatenated CSS for a route URL.
6
+ *
7
+ * Used by the CMS editor SPA to fetch the styles that apply to the page
8
+ * being edited so the editor renders content with the same CSS as the
9
+ * published page.
10
+ *
11
+ * Mechanism (host-agnostic):
12
+ *
13
+ * 1. HTTP-fetch the live HTML at the route URL from the running dev
14
+ * server. This is the same HTML a browser would receive — it lists
15
+ * every stylesheet the page actually loads.
16
+ * 2. Pull the content of every `<style>...</style>` block (where dev-
17
+ * mode Astro inlines its component / Tailwind / user CSS).
18
+ * 3. Pull every `<link rel="stylesheet" href="…">` (excluding
19
+ * `media="print"`) and fetch each one with `?direct` so the dev
20
+ * server returns the raw CSS instead of the HMR JS wrapper.
21
+ * 4. Concatenate in source order and return as `text/css`.
22
+ *
23
+ * Why this instead of Astro's `devCSSMap`:
24
+ *
25
+ * - `devCSSMap` is an Astro-internal virtual module. It only surfaces
26
+ * CSS that flows through Astro's `.astro` component graph. Anything
27
+ * emitted by an Astro integration as a STANDALONE asset (the
28
+ * canonical example is expressive-code's `/_astro/ec.*.css`) sits
29
+ * outside that graph and never reaches the editor through devCSSMap.
30
+ * - Scraping the live HTML reads what the published page actually
31
+ * paints with — the same stylesheets the user will see in the
32
+ * browser. No dependency on a specific framework's internal API.
33
+ *
34
+ * Known limitations (rare on Astro sites):
35
+ *
36
+ * - CSS injected by JavaScript at runtime (`document.head.appendChild`)
37
+ * isn't in the initial HTML, so we don't see it. Affects view-
38
+ * transition handlers and third-party widget snippets.
39
+ * - CSS-in-JS libraries (styled-components, emotion) generate styles
40
+ * during client render and have the same blind spot.
41
+ * - Cross-origin stylesheets the dev server can't reach get logged
42
+ * and skipped (graceful degradation, not silent failure).
43
+ */
44
+ export declare function createStylesEndpoint(): NextHandleFunction;
45
+ interface DiscoveredStyle {
46
+ /** Diagnostic label — either an inline `<style>` marker or the link href. */
47
+ url: string;
48
+ /** Raw CSS text. */
49
+ css: string;
50
+ }
51
+ /**
52
+ * Read every `<style>` block and `<link rel="stylesheet">` from the
53
+ * HTML in their original source order. Inline `<style>` blocks are
54
+ * captured verbatim; link tags are fetched separately (raw CSS via
55
+ * `?direct`). Print-media stylesheets are skipped.
56
+ *
57
+ * Exported for unit testing.
58
+ */
59
+ export declare function collectStylesFromHtml(html: string, origin: string, cookie: string | undefined): Promise<DiscoveredStyle[]>;
60
+ export {};
61
+ //# sourceMappingURL=styles-endpoint.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"styles-endpoint.d.ts","sourceRoot":"","sources":["../../../src/site-styles/styles-endpoint.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAEjE,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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AACH,wBAAgB,oBAAoB,IAAI,kBAAkB,CAgDzD;AAED,UAAU,eAAe;IACvB,6EAA6E;IAC7E,GAAG,EAAE,MAAM,CAAC;IACZ,oBAAoB;IACpB,GAAG,EAAE,MAAM,CAAC;CACb;AAED;;;;;;;GAOG;AACH,wBAAsB,qBAAqB,CACzC,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,GAAG,SAAS,GACzB,OAAO,CAAC,eAAe,EAAE,CAAC,CA2C5B"}
@@ -0,0 +1,158 @@
1
+ /**
2
+ * Vite middleware that returns the concatenated CSS for a route URL.
3
+ *
4
+ * Used by the CMS editor SPA to fetch the styles that apply to the page
5
+ * being edited so the editor renders content with the same CSS as the
6
+ * published page.
7
+ *
8
+ * Mechanism (host-agnostic):
9
+ *
10
+ * 1. HTTP-fetch the live HTML at the route URL from the running dev
11
+ * server. This is the same HTML a browser would receive — it lists
12
+ * every stylesheet the page actually loads.
13
+ * 2. Pull the content of every `<style>...</style>` block (where dev-
14
+ * mode Astro inlines its component / Tailwind / user CSS).
15
+ * 3. Pull every `<link rel="stylesheet" href="…">` (excluding
16
+ * `media="print"`) and fetch each one with `?direct` so the dev
17
+ * server returns the raw CSS instead of the HMR JS wrapper.
18
+ * 4. Concatenate in source order and return as `text/css`.
19
+ *
20
+ * Why this instead of Astro's `devCSSMap`:
21
+ *
22
+ * - `devCSSMap` is an Astro-internal virtual module. It only surfaces
23
+ * CSS that flows through Astro's `.astro` component graph. Anything
24
+ * emitted by an Astro integration as a STANDALONE asset (the
25
+ * canonical example is expressive-code's `/_astro/ec.*.css`) sits
26
+ * outside that graph and never reaches the editor through devCSSMap.
27
+ * - Scraping the live HTML reads what the published page actually
28
+ * paints with — the same stylesheets the user will see in the
29
+ * browser. No dependency on a specific framework's internal API.
30
+ *
31
+ * Known limitations (rare on Astro sites):
32
+ *
33
+ * - CSS injected by JavaScript at runtime (`document.head.appendChild`)
34
+ * isn't in the initial HTML, so we don't see it. Affects view-
35
+ * transition handlers and third-party widget snippets.
36
+ * - CSS-in-JS libraries (styled-components, emotion) generate styles
37
+ * during client render and have the same blind spot.
38
+ * - Cross-origin stylesheets the dev server can't reach get logged
39
+ * and skipped (graceful degradation, not silent failure).
40
+ */
41
+ export function createStylesEndpoint() {
42
+ return async (req, res, _next) => {
43
+ try {
44
+ const parsed = new URL(req.url ?? '', 'http://localhost');
45
+ const routeUrl = parsed.searchParams.get('url');
46
+ if (!routeUrl) {
47
+ res.statusCode = 400;
48
+ res.setHeader('content-type', 'text/plain');
49
+ res.end('Missing required query parameter: url');
50
+ return;
51
+ }
52
+ const host = req.headers.host ?? 'localhost:4321';
53
+ const protocol = req.headers['x-forwarded-proto'] ?? 'http';
54
+ const origin = `${protocol}://${host}`;
55
+ const cookie = req.headers.cookie;
56
+ const liveResponse = await fetch(`${origin}${routeUrl}`, {
57
+ headers: cookie ? { cookie } : undefined,
58
+ });
59
+ // Propagate upstream non-200 status (eg 404 for unknown routes)
60
+ // so the editor doesn't accidentally render a 404 page's CSS.
61
+ if (!liveResponse.ok) {
62
+ res.statusCode = liveResponse.status;
63
+ res.setHeader('content-type', 'text/plain');
64
+ res.end(`Upstream route returned ${liveResponse.status}: ${routeUrl}`);
65
+ return;
66
+ }
67
+ const html = await liveResponse.text();
68
+ const styles = await collectStylesFromHtml(html, origin, cookie);
69
+ const body = styles.map((s) => `/* ${s.url} */\n${s.css}`).join('\n\n');
70
+ res.statusCode = 200;
71
+ res.setHeader('content-type', 'text/css; charset=utf-8');
72
+ // No-cache: dev HMR rewrites stylesheet contents in place; the
73
+ // editor refetches on page switch, so cache invalidation is the
74
+ // client's job, not the server's.
75
+ res.setHeader('cache-control', 'no-store');
76
+ res.end(body);
77
+ }
78
+ catch (err) {
79
+ res.statusCode = 500;
80
+ res.setHeader('content-type', 'text/plain');
81
+ res.end(`Styles discovery failed: ${err instanceof Error ? err.message : String(err)}`);
82
+ }
83
+ };
84
+ }
85
+ /**
86
+ * Read every `<style>` block and `<link rel="stylesheet">` from the
87
+ * HTML in their original source order. Inline `<style>` blocks are
88
+ * captured verbatim; link tags are fetched separately (raw CSS via
89
+ * `?direct`). Print-media stylesheets are skipped.
90
+ *
91
+ * Exported for unit testing.
92
+ */
93
+ export async function collectStylesFromHtml(html, origin, cookie) {
94
+ // Strip comments first so opening-tag-shaped strings inside them
95
+ // don't pollute the regex walk.
96
+ const cleaned = html.replace(/<!--[\s\S]*?-->/g, '');
97
+ const entries = [];
98
+ const styleRe = /<style\b([^>]*)>([\s\S]*?)<\/style>/gi;
99
+ let match;
100
+ // biome-ignore lint/suspicious/noAssignInExpressions: regex exec iterator
101
+ while ((match = styleRe.exec(cleaned)) !== null) {
102
+ const attrs = match[1];
103
+ if (/media\s*=\s*["']?print/i.test(attrs))
104
+ continue;
105
+ const css = match[2];
106
+ const idAttr = attrs.match(/data-vite-dev-id="([^"]+)"/);
107
+ const label = idAttr ? idAttr[1] : `<style#${match.index}>`;
108
+ const captured = { url: label, css };
109
+ entries.push({ index: match.index, fetch: () => Promise.resolve(captured) });
110
+ }
111
+ const linkRe = /<link\b([^>]*)\/?>/gi;
112
+ // biome-ignore lint/suspicious/noAssignInExpressions: regex exec iterator
113
+ while ((match = linkRe.exec(cleaned)) !== null) {
114
+ const attrs = match[1];
115
+ if (!/rel\s*=\s*["']?stylesheet["']?/i.test(attrs))
116
+ continue;
117
+ if (/media\s*=\s*["']?print/i.test(attrs))
118
+ continue;
119
+ const hrefMatch = attrs.match(/href\s*=\s*"([^"]+)"|href\s*=\s*'([^']+)'/);
120
+ if (!hrefMatch)
121
+ continue;
122
+ const href = hrefMatch[1] ?? hrefMatch[2];
123
+ const index = match.index;
124
+ entries.push({ index, fetch: () => fetchStylesheet(href, origin, cookie) });
125
+ }
126
+ // Source-order sort (style blocks and link tags interleave in HTML).
127
+ entries.sort((a, b) => a.index - b.index);
128
+ const results = await Promise.all(entries.map((e) => e.fetch()));
129
+ return results.filter((r) => r !== null);
130
+ }
131
+ /**
132
+ * Fetch a stylesheet by href and return its raw CSS. Vite serves CSS
133
+ * files as JS HMR wrappers by default; `?direct` flips that to raw CSS
134
+ * output. Absolute URLs are fetched as-is; root-relative paths are
135
+ * resolved against the dev server origin so cross-page link tags work.
136
+ */
137
+ async function fetchStylesheet(href, origin, cookie) {
138
+ // Absolute URL (`https://…`) or relative — both need `?direct` only
139
+ // when targeting our own dev server. External CDNs serve raw CSS
140
+ // already; appending `?direct` would 404 there.
141
+ const isLocal = !/^https?:\/\//i.test(href) || href.startsWith(origin);
142
+ const target = isLocal
143
+ ? `${origin}${href.startsWith('/') ? href : `/${href}`}${href.includes('?') ? '&direct' : '?direct'}`
144
+ : href;
145
+ try {
146
+ const res = await fetch(target, { headers: cookie ? { cookie } : undefined });
147
+ if (!res.ok) {
148
+ console.warn(`[conloca:css] link fetch returned ${res.status} for ${href}`);
149
+ return null;
150
+ }
151
+ const css = await res.text();
152
+ return { url: href, css };
153
+ }
154
+ catch (err) {
155
+ console.warn(`[conloca:css] link fetch failed for ${href}:`, err);
156
+ return null;
157
+ }
158
+ }