@conloca/astro-cms 0.2.1 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +21 -19
- package/dist/internal/page-handler.astro +8 -2
- package/dist/ts/.build-inputs.json +88 -0
- package/dist/ts/node/content.d.ts +1 -2
- 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/collections.d.ts +31 -53
- package/dist/ts/src/collections.d.ts.map +1 -1
- package/dist/ts/src/collections.js +66 -71
- package/dist/ts/src/collections.js.map +1 -1
- package/dist/ts/src/editor-descriptors.d.ts +19 -5
- package/dist/ts/src/editor-descriptors.d.ts.map +1 -1
- package/dist/ts/src/editor-descriptors.js +26 -5
- package/dist/ts/src/editor-descriptors.js.map +1 -1
- package/dist/ts/src/entries.d.ts +44 -0
- package/dist/ts/src/entries.d.ts.map +1 -0
- package/dist/ts/src/entries.js +48 -0
- package/dist/ts/src/entries.js.map +1 -0
- package/dist/ts/src/handlers/collection-content-api.d.ts.map +1 -1
- package/dist/ts/src/handlers/collection-content-api.js +7 -2
- package/dist/ts/src/handlers/collection-content-api.js.map +1 -1
- package/dist/ts/src/handlers/collection-names.d.ts +9 -0
- package/dist/ts/src/handlers/collection-names.d.ts.map +1 -0
- package/dist/ts/src/handlers/collection-names.js +9 -0
- package/dist/ts/src/handlers/collection-names.js.map +1 -0
- package/dist/ts/src/handlers/page-render-core.d.ts +31 -12
- package/dist/ts/src/handlers/page-render-core.d.ts.map +1 -1
- package/dist/ts/src/handlers/page-render-core.js +38 -17
- package/dist/ts/src/handlers/page-render-core.js.map +1 -1
- package/dist/ts/src/index.d.ts +2 -2
- package/dist/ts/src/index.d.ts.map +1 -1
- package/dist/ts/src/index.js +1 -3
- package/dist/ts/src/index.js.map +1 -1
- package/dist/ts/src/internal/collection-parsers.d.ts +26 -0
- package/dist/ts/src/internal/collection-parsers.d.ts.map +1 -0
- package/dist/ts/src/internal/collection-parsers.js +34 -0
- package/dist/ts/src/internal/collection-parsers.js.map +1 -0
- package/dist/ts/src/internal/entry-issues.d.ts +8 -0
- package/dist/ts/src/internal/entry-issues.d.ts.map +1 -0
- package/dist/ts/src/internal/entry-issues.js +19 -0
- package/dist/ts/src/internal/entry-issues.js.map +1 -0
- package/dist/ts/src/internal/entry-validation.d.ts +93 -0
- package/dist/ts/src/internal/entry-validation.d.ts.map +1 -0
- package/dist/ts/src/internal/entry-validation.js +228 -0
- package/dist/ts/src/internal/entry-validation.js.map +1 -0
- package/dist/ts/src/lib/virtual-module-generators.d.ts +13 -0
- package/dist/ts/src/lib/virtual-module-generators.d.ts.map +1 -1
- package/dist/ts/src/lib/virtual-module-generators.js +18 -0
- package/dist/ts/src/lib/virtual-module-generators.js.map +1 -1
- package/dist/ts/src/loader.d.ts +9 -12
- package/dist/ts/src/loader.d.ts.map +1 -1
- package/dist/ts/src/loader.js +19 -16
- package/dist/ts/src/loader.js.map +1 -1
- package/dist/ts/src/page-schemas.d.ts +1 -1
- package/dist/ts/src/page-schemas.d.ts.map +1 -1
- package/dist/ts/src/page-schemas.js +1 -1
- package/dist/ts/src/page-schemas.js.map +1 -1
- package/dist/ts/src/plugin-spa.d.ts +12 -0
- package/dist/ts/src/plugin-spa.d.ts.map +1 -1
- package/dist/ts/src/plugin-spa.js +39 -14
- 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/dist/virtual-modules.d.ts +14 -0
- package/node/content.ts +10 -1
- package/node/index.ts +7 -7
- package/package.json +12 -9
- package/src/collections.ts +107 -76
- package/src/editor-descriptors.ts +43 -5
- package/src/entries.ts +111 -0
- package/src/handlers/collection-content-api.ts +7 -2
- package/src/handlers/collection-names.ts +8 -0
- package/src/handlers/page-handler.astro +8 -2
- package/src/handlers/page-render-core.ts +65 -17
- package/src/index.ts +6 -7
- package/src/internal/collection-parsers.ts +51 -0
- package/src/internal/entry-issues.ts +23 -0
- package/src/internal/entry-validation.ts +305 -0
- package/src/lib/virtual-module-generators.ts +20 -0
- package/src/loader.ts +33 -17
- package/src/page-schemas.ts +4 -0
- package/src/plugin-spa.ts +58 -13
- package/src/sandbox-frame-handler.ts +16 -5
- package/src/site-styles/content-wrapper-endpoint.ts +4 -285
- package/src/virtual-modules.d.ts +14 -0
- package/dist/ts/src/internal/page-collection-schema.d.ts +0 -44
- package/dist/ts/src/internal/page-collection-schema.d.ts.map +0 -1
- package/dist/ts/src/internal/page-collection-schema.js +0 -152
- package/dist/ts/src/internal/page-collection-schema.js.map +0 -1
- package/src/internal/page-collection-schema.ts +0 -210
package/src/page-schemas.ts
CHANGED
|
@@ -4,11 +4,15 @@
|
|
|
4
4
|
*/
|
|
5
5
|
export {
|
|
6
6
|
type CoreFieldsMode,
|
|
7
|
+
type DataSchemas,
|
|
8
|
+
defineHostSchema,
|
|
7
9
|
definePageSchema,
|
|
8
10
|
type FieldHint,
|
|
9
11
|
type FieldHintCommon,
|
|
10
12
|
type FieldHints,
|
|
11
13
|
type FieldHintVariant,
|
|
14
|
+
type HostSchema,
|
|
15
|
+
type HostSchemaType,
|
|
12
16
|
type PageSchemaDescriptor,
|
|
13
17
|
type PageSchemaEntry,
|
|
14
18
|
type PageSchemaGroup,
|
package/src/plugin-spa.ts
CHANGED
|
@@ -55,6 +55,7 @@ const ACORN_DEFAULT_SHIM_PATH = join(__dirname, '..', '..', 'internal', 'acorn-d
|
|
|
55
55
|
const TIPTAP_HTML_SHIM_PATH = join(__dirname, 'tiptap-html-static.js');
|
|
56
56
|
|
|
57
57
|
import {
|
|
58
|
+
generateContentSchemasModule,
|
|
58
59
|
generateLayoutModule,
|
|
59
60
|
generatePuckConfigModule,
|
|
60
61
|
generateRoutingConfigModule,
|
|
@@ -69,11 +70,13 @@ const RESOLVED_VIRTUAL_CONFIG = `\0${VIRTUAL_CONFIG_MODULE}`;
|
|
|
69
70
|
const VIRTUAL_ROUTING_CONFIG = 'virtual:conloca-routing-config';
|
|
70
71
|
const VIRTUAL_LAYOUT = 'virtual:conloca-layout';
|
|
71
72
|
const VIRTUAL_PUCK_CONFIG = 'virtual:conloca-puck-config';
|
|
73
|
+
const VIRTUAL_CONTENT_SCHEMAS = 'virtual:conloca-content-schemas';
|
|
72
74
|
|
|
73
75
|
// Resolved IDs (with \0 prefix to prevent file resolution)
|
|
74
76
|
const RESOLVED_ROUTING_CONFIG = `\0${VIRTUAL_ROUTING_CONFIG}`;
|
|
75
77
|
const RESOLVED_LAYOUT = `\0${VIRTUAL_LAYOUT}`;
|
|
76
78
|
const RESOLVED_PUCK_CONFIG = `\0${VIRTUAL_PUCK_CONFIG}`;
|
|
79
|
+
const RESOLVED_CONTENT_SCHEMAS = `\0${VIRTUAL_CONTENT_SCHEMAS}`;
|
|
77
80
|
|
|
78
81
|
// Virtual modules for hydration system
|
|
79
82
|
const VIRTUAL_HYDRATION_REGISTRY = 'virtual:conloca-hydration-registry';
|
|
@@ -107,6 +110,19 @@ export interface ConlocaCMSOptions extends Omit<UIConfig, 'basename'> {
|
|
|
107
110
|
puckConfigPath: string; // Path to the puck config module (should be .tsx file with React components)
|
|
108
111
|
schemasPath?: string; // Path to the schemas module (exports { dataSchemas, pageSchemas, mdxComponents })
|
|
109
112
|
|
|
113
|
+
/**
|
|
114
|
+
* Module exporting the `pageSchemas` and/or `dataSchemas` the content config
|
|
115
|
+
* hands conlocaPages() and conlocaData(). A draft preview builds the same
|
|
116
|
+
* entry parsers from them and parses each draft as the build parses an
|
|
117
|
+
* entry, so it gets the stored values and fails where the build would. It
|
|
118
|
+
* loads in the preview server, so it may import schema code only (Typia
|
|
119
|
+
* host schemas, definePageSchema) — the module the content config imports
|
|
120
|
+
* them from, not the editor's `schemasPath` barrel.
|
|
121
|
+
*
|
|
122
|
+
* @example './src/page-schemas.ts'
|
|
123
|
+
*/
|
|
124
|
+
contentSchemasPath?: string;
|
|
125
|
+
|
|
110
126
|
/**
|
|
111
127
|
* Default layout for content pages.
|
|
112
128
|
* When provided without routing config, enables routing with catch-all pattern.
|
|
@@ -653,12 +669,39 @@ export function conlocaCMS(options: ConlocaCMSOptions): AstroIntegration {
|
|
|
653
669
|
// descriptors dev loads from `schemasPath`, `/api/registry` and
|
|
654
670
|
// `virtual:conloca-config` instead of taking these options again.
|
|
655
671
|
// schemasPath is absolutized because a reader may use it as an import
|
|
656
|
-
// specifier.
|
|
672
|
+
// specifier. Readers inherit the puck config, the site and editor
|
|
673
|
+
// stylesheets, the editor CSS and the fragment preview route from here
|
|
674
|
+
// too, so the site writes each of them once. The stylesheets resolve
|
|
675
|
+
// against the Astro root, where the dev editor's Vite imports find
|
|
676
|
+
// them, so a build started from another folder (`astro build --root
|
|
677
|
+
// site`) still hands on the same files.
|
|
678
|
+
const configuredStyles = options.siteStyles;
|
|
679
|
+
const siteStyles = configuredStyles
|
|
680
|
+
? (Array.isArray(configuredStyles) ? configuredStyles : [configuredStyles]).map((path) =>
|
|
681
|
+
resolve(fileURLToPath(config.root), path),
|
|
682
|
+
)
|
|
683
|
+
: undefined;
|
|
684
|
+
const configuredEditorStyles = options.editorStyles;
|
|
685
|
+
const editorStyles = configuredEditorStyles
|
|
686
|
+
? (Array.isArray(configuredEditorStyles) ? configuredEditorStyles : [configuredEditorStyles]).map((path) =>
|
|
687
|
+
resolve(fileURLToPath(config.root), path),
|
|
688
|
+
)
|
|
689
|
+
: undefined;
|
|
657
690
|
const editorDescriptors: EditorDescriptorChannel = {
|
|
658
691
|
...(options.schemasPath ? { schemasPath: resolve(options.schemasPath) } : {}),
|
|
692
|
+
puckConfigPath: resolve(options.puckConfigPath),
|
|
693
|
+
...(siteStyles === undefined ? {} : { siteStyles }),
|
|
694
|
+
...(editorStyles === undefined ? {} : { editorStyles }),
|
|
695
|
+
...(options.editorCSS ? { editorCSS: options.editorCSS } : {}),
|
|
659
696
|
registry: registryScan,
|
|
660
697
|
...(options.templates ? { templates: options.templates } : {}),
|
|
661
698
|
...(spaConfig.dataBindings ? { dataBindings: spaConfig.dataBindings } : {}),
|
|
699
|
+
// Every folder the content API writes to, resolved as it resolves them (the build's working directory).
|
|
700
|
+
contentPaths: [
|
|
701
|
+
resolve(options.contentRoot),
|
|
702
|
+
...(resolvedMdxPages ? [resolve(resolvedMdxPages)] : []),
|
|
703
|
+
...(options.assetsPath ? [resolve(options.assetsPath)] : []),
|
|
704
|
+
],
|
|
662
705
|
};
|
|
663
706
|
process.env[EDITOR_DESCRIPTORS_ENV_VAR] = JSON.stringify(editorDescriptors);
|
|
664
707
|
|
|
@@ -815,6 +858,9 @@ export function conlocaCMS(options: ConlocaCMSOptions): AstroIntegration {
|
|
|
815
858
|
if (id === VIRTUAL_PUCK_CONFIG) {
|
|
816
859
|
return RESOLVED_PUCK_CONFIG;
|
|
817
860
|
}
|
|
861
|
+
if (id === VIRTUAL_CONTENT_SCHEMAS) {
|
|
862
|
+
return RESOLVED_CONTENT_SCHEMAS;
|
|
863
|
+
}
|
|
818
864
|
// Hydration virtual modules
|
|
819
865
|
if (id === VIRTUAL_HYDRATION_REGISTRY) {
|
|
820
866
|
return RESOLVED_HYDRATION_REGISTRY;
|
|
@@ -839,6 +885,11 @@ export function conlocaCMS(options: ConlocaCMSOptions): AstroIntegration {
|
|
|
839
885
|
if (id === RESOLVED_PUCK_CONFIG) {
|
|
840
886
|
return generatePuckConfigModule(options.puckConfigPath);
|
|
841
887
|
}
|
|
888
|
+
if (id === RESOLVED_CONTENT_SCHEMAS) {
|
|
889
|
+
return generateContentSchemasModule(
|
|
890
|
+
options.contentSchemasPath ? resolve(options.contentSchemasPath) : undefined,
|
|
891
|
+
);
|
|
892
|
+
}
|
|
842
893
|
// Hydration virtual modules
|
|
843
894
|
if (id === RESOLVED_HYDRATION_REGISTRY) {
|
|
844
895
|
// Always scan - paths are auto-derived from puckConfigPath
|
|
@@ -889,17 +940,12 @@ initHydration(componentRegistry)
|
|
|
889
940
|
}
|
|
890
941
|
}
|
|
891
942
|
|
|
892
|
-
// The sandbox frame document ContentSandbox loads for MDX previews
|
|
893
|
-
// Root-absolute by contract (SANDBOX_FRAME_PATH in
|
|
894
|
-
//
|
|
895
|
-
//
|
|
896
|
-
//
|
|
897
|
-
|
|
898
|
-
// On-demand by necessity — the response carries the sandbox CSP, which
|
|
899
|
-
// a prerendered file cannot — so injecting it into a fully static site
|
|
900
|
-
// fails that build with NoAdapterInstalled. Such a site has no runtime
|
|
901
|
-
// editor to serve it to; dev serves it whatever the output mode.
|
|
902
|
-
if (command === 'dev' || config.output !== 'static') {
|
|
943
|
+
// The sandbox frame document ContentSandbox loads for MDX previews in
|
|
944
|
+
// the dev editor. Root-absolute by contract (SANDBOX_FRAME_PATH in
|
|
945
|
+
// @conloca/mdx). On-demand by necessity — the response carries the
|
|
946
|
+
// sandbox CSP, which a prerendered file cannot. A built site never
|
|
947
|
+
// gets it: its editor page ships the frame as a static file.
|
|
948
|
+
if (command === 'dev') {
|
|
903
949
|
injectRoute({
|
|
904
950
|
pattern: '/api/v1/mdx/sandbox-frame',
|
|
905
951
|
entrypoint: SANDBOX_FRAME_HANDLER_PATH,
|
|
@@ -960,7 +1006,6 @@ initHydration(componentRegistry)
|
|
|
960
1006
|
'clsx',
|
|
961
1007
|
'tailwind-merge',
|
|
962
1008
|
// Content processing
|
|
963
|
-
'zod',
|
|
964
1009
|
'yaml',
|
|
965
1010
|
'nanoid',
|
|
966
1011
|
'sort-keys',
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { buildSandboxSrcdoc,
|
|
1
|
+
import { buildSandboxSrcdoc, MDX_SANDBOX_FRAME_POLICY } from '@conloca/mdx/sandbox-frame';
|
|
2
2
|
import type { APIRoute } from 'astro';
|
|
3
3
|
|
|
4
4
|
/**
|
|
@@ -11,7 +11,8 @@ import type { APIRoute } from 'astro';
|
|
|
11
11
|
* that serves the editor must serve this path too. astro-cms routes otherwise
|
|
12
12
|
* live under the cmsRoute prefix, so without this route the frame 404s and
|
|
13
13
|
* chips render the site's 404 page. Response headers are the frame's contract
|
|
14
|
-
* (`
|
|
14
|
+
* (`MDX_SANDBOX_FRAME_POLICY`); any other server of the frame must match them,
|
|
15
|
+
* including the 403 for anything but an iframe load.
|
|
15
16
|
*
|
|
16
17
|
* No auth gate: the document is static boilerplate — content only ever enters
|
|
17
18
|
* the frame via postMessage from the editor.
|
|
@@ -19,13 +20,23 @@ import type { APIRoute } from 'astro';
|
|
|
19
20
|
|
|
20
21
|
export const prerender = false;
|
|
21
22
|
|
|
22
|
-
|
|
23
|
-
|
|
23
|
+
/** The frame route's answer to a request with these headers. */
|
|
24
|
+
export function sandboxFrameResponse(request: Pick<Request, 'headers'>): Response {
|
|
25
|
+
if (request.headers.get('sec-fetch-dest') !== 'iframe') {
|
|
26
|
+
return new Response('The MDX preview frame loads only inside an editor frame.', {
|
|
27
|
+
status: 403,
|
|
28
|
+
headers: { 'cache-control': 'no-store' },
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
return new Response(buildSandboxSrcdoc(), {
|
|
24
32
|
status: 200,
|
|
25
33
|
headers: {
|
|
26
34
|
'content-type': 'text/html; charset=utf-8',
|
|
27
|
-
'content-security-policy':
|
|
35
|
+
'content-security-policy': MDX_SANDBOX_FRAME_POLICY,
|
|
28
36
|
'x-content-type-options': 'nosniff',
|
|
29
37
|
'cache-control': 'no-store',
|
|
30
38
|
},
|
|
31
39
|
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export const GET: APIRoute = ({ request }) => sandboxFrameResponse(request);
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { findContentWrapper } from '@conloca/cms-spa/content-wrapper';
|
|
1
2
|
import type { Connect } from 'vite';
|
|
2
3
|
|
|
3
4
|
/**
|
|
@@ -7,63 +8,10 @@ import type { Connect } from 'vite';
|
|
|
7
8
|
* contenteditable inside a clone of whatever element the host wraps
|
|
8
9
|
* its page content in. This endpoint fetches the live HTML the dev
|
|
9
10
|
* server serves at `?url=<route>`, locates the relevant wrappers
|
|
10
|
-
* (prose content + code-block chrome)
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
* {
|
|
15
|
-
* content: { tagName, className } | null,
|
|
16
|
-
* codeBlock: { tagName, className }[] | null,
|
|
17
|
-
* }
|
|
18
|
-
*
|
|
19
|
-
* - `content` is the wrapper for prose (h1-h6, p, lists, etc.). The
|
|
20
|
-
* editor wraps its contenteditable in a clone so the host's prose
|
|
21
|
-
* CSS reaches via the cascade.
|
|
22
|
-
* - `codeBlock` is the ORDERED chain of classed wrappers around the
|
|
23
|
-
* host's `<pre>`, outermost first. For Starlight's expressive-code:
|
|
24
|
-
* `[{ div, 'expressive-code' }, { figure, 'frame has-title …' }]`.
|
|
25
|
-
* The editor's CodeMirror frame renders each link as a nested
|
|
26
|
-
* element (outer→inner) so the host's code-block CSS reaches via
|
|
27
|
-
* the cascade including descendant selectors (`.expressive-code
|
|
28
|
-
* .frame .header`, etc.) — not just single-class targets.
|
|
29
|
-
*
|
|
30
|
-
* Discovery priorities (content):
|
|
31
|
-
*
|
|
32
|
-
* 1. `[data-conloca-content-root]` — explicit opt-in.
|
|
33
|
-
* 2. The smallest classed wrapper inside `<main>` containing the
|
|
34
|
-
* majority of prose tags.
|
|
35
|
-
* 3. `<main>` itself.
|
|
36
|
-
* 4. `null`.
|
|
37
|
-
*
|
|
38
|
-
* Discovery (codeBlock): walk up from a `<pre>` inside `<main>` and
|
|
39
|
-
* collect classed ancestors between the content wrapper and `<pre>`,
|
|
40
|
-
* preserving their nesting order. Returns null when no `<pre>` exists,
|
|
41
|
-
* or when there are no classed wrappers between the prose wrapper and
|
|
42
|
-
* the `<pre>`.
|
|
11
|
+
* (prose content + code-block chrome) with `findContentWrapper`, and
|
|
12
|
+
* returns them as JSON. An editor page without this endpoint runs the
|
|
13
|
+
* same function on the page its style probe loads.
|
|
43
14
|
*/
|
|
44
|
-
export interface ContentWrapperInfo {
|
|
45
|
-
/** Lower-case HTML tag name as it appeared in source, eg `'article'`. */
|
|
46
|
-
tagName: string;
|
|
47
|
-
/** Space-separated class list from the source element. Preserved
|
|
48
|
-
* verbatim so Astro-scoped hashes (`astro-e3flfouy`) survive — host
|
|
49
|
-
* scoped CSS only matches when those hashes are present on the
|
|
50
|
-
* element. */
|
|
51
|
-
className: string;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export interface DiscoveredWrappers {
|
|
55
|
-
/** The element wrapping the page's prose content (h1-h6, p, etc.).
|
|
56
|
-
* For Starlight: `.sl-markdown-content`. For Tailwind Typography:
|
|
57
|
-
* `.prose`. Null when no prose wrapper exists on the page. */
|
|
58
|
-
content: ContentWrapperInfo | null;
|
|
59
|
-
/** Ordered chain of classed wrappers between the content wrapper
|
|
60
|
-
* and the `<pre>`, outermost first. For Starlight:
|
|
61
|
-
* `[{ div, 'expressive-code' }, { figure, 'frame has-title …' }]`.
|
|
62
|
-
* Null when the page has no code blocks or no classed wrappers
|
|
63
|
-
* between the prose wrapper and the `<pre>`. */
|
|
64
|
-
codeBlock: ContentWrapperInfo[] | null;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
15
|
export function createContentWrapperEndpoint(): Connect.NextHandleFunction {
|
|
68
16
|
return async (req, res, _next) => {
|
|
69
17
|
try {
|
|
@@ -96,232 +44,3 @@ export function createContentWrapperEndpoint(): Connect.NextHandleFunction {
|
|
|
96
44
|
}
|
|
97
45
|
};
|
|
98
46
|
}
|
|
99
|
-
|
|
100
|
-
/** Tags whose presence signals "this element holds prose content."
|
|
101
|
-
* We walk up from the first such element we find inside `<main>` to
|
|
102
|
-
* locate the wrapper. Headings + paragraphs cover every prose-styling
|
|
103
|
-
* convention we've seen — every CSS framework scopes typography
|
|
104
|
-
* through descendant selectors on `h1`–`h6` and `p`. */
|
|
105
|
-
const PROSE_TAGS = new Set(['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p']);
|
|
106
|
-
|
|
107
|
-
/** Tag that anchors code-block wrapper discovery. The host's code-
|
|
108
|
-
* block library (Starlight's expressive-code, Tailwind's `pre`
|
|
109
|
-
* styling, etc.) always renders a `<pre>` containing the code,
|
|
110
|
-
* often wrapped in additional classed elements. */
|
|
111
|
-
const CODE_BLOCK_ANCHOR = 'pre';
|
|
112
|
-
|
|
113
|
-
/** HTML5 void elements — never push onto the open-tag stack since they
|
|
114
|
-
* have no closing tag. */
|
|
115
|
-
const VOID_TAGS = new Set([
|
|
116
|
-
'area',
|
|
117
|
-
'base',
|
|
118
|
-
'br',
|
|
119
|
-
'col',
|
|
120
|
-
'embed',
|
|
121
|
-
'hr',
|
|
122
|
-
'img',
|
|
123
|
-
'input',
|
|
124
|
-
'link',
|
|
125
|
-
'meta',
|
|
126
|
-
'source',
|
|
127
|
-
'track',
|
|
128
|
-
'wbr',
|
|
129
|
-
]);
|
|
130
|
-
|
|
131
|
-
interface OpenTag {
|
|
132
|
-
tag: string;
|
|
133
|
-
className: string;
|
|
134
|
-
attrs: Record<string, string>;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* Locate the host's content + code-block wrappers in raw HTML. Single-
|
|
139
|
-
* pass regex walk with a stack so we can recover ancestor chains for
|
|
140
|
-
* both prose tags and `<pre>` tags without a full DOM parser dep.
|
|
141
|
-
*
|
|
142
|
-
* Exported for unit testing.
|
|
143
|
-
*/
|
|
144
|
-
export function findContentWrapper(html: string): DiscoveredWrappers {
|
|
145
|
-
// Strip comments and scripts so opening-tag-shaped strings inside
|
|
146
|
-
// them don't pollute the walk.
|
|
147
|
-
const cleaned = html
|
|
148
|
-
.replace(/<!--[\s\S]*?-->/g, '')
|
|
149
|
-
.replace(/<script[\s\S]*?<\/script>/gi, '')
|
|
150
|
-
.replace(/<style[\s\S]*?<\/style>/gi, '');
|
|
151
|
-
|
|
152
|
-
const tagRe = /<(\/?)([a-zA-Z][a-zA-Z0-9-]*)((?:\s+[^>]*)?)\s*(\/?)>/g;
|
|
153
|
-
const stack: OpenTag[] = [];
|
|
154
|
-
|
|
155
|
-
let explicit: ContentWrapperInfo | null = null;
|
|
156
|
-
let mainEl: OpenTag | null = null;
|
|
157
|
-
const proseAncestorChains: OpenTag[][] = [];
|
|
158
|
-
const preAncestorChains: OpenTag[][] = [];
|
|
159
|
-
|
|
160
|
-
for (const match of cleaned.matchAll(tagRe)) {
|
|
161
|
-
const isClose = match[1] === '/';
|
|
162
|
-
const tagName = match[2].toLowerCase();
|
|
163
|
-
const attrBlob = match[3] ?? '';
|
|
164
|
-
const explicitlySelfClosing = match[4] === '/';
|
|
165
|
-
|
|
166
|
-
if (isClose) {
|
|
167
|
-
for (let i = stack.length - 1; i >= 0; i--) {
|
|
168
|
-
if (stack[i].tag === tagName) {
|
|
169
|
-
stack.length = i;
|
|
170
|
-
break;
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
continue;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
const attrs = parseAttributes(attrBlob);
|
|
177
|
-
const className = attrs.class ?? '';
|
|
178
|
-
const el: OpenTag = { tag: tagName, className, attrs };
|
|
179
|
-
|
|
180
|
-
// Priority 1 (content only): explicit opt-in. Capture and keep
|
|
181
|
-
// walking — we still need to discover the code-block wrapper.
|
|
182
|
-
if (!explicit && 'data-conloca-content-root' in attrs) {
|
|
183
|
-
explicit = { tagName, className };
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
if (!mainEl && tagName === 'main') {
|
|
187
|
-
mainEl = el;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
if (mainEl && PROSE_TAGS.has(tagName)) {
|
|
191
|
-
const mainIdx = stack.indexOf(mainEl);
|
|
192
|
-
if (mainIdx >= 0) {
|
|
193
|
-
proseAncestorChains.push(stack.slice(mainIdx + 1));
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
if (mainEl && tagName === CODE_BLOCK_ANCHOR) {
|
|
198
|
-
const mainIdx = stack.indexOf(mainEl);
|
|
199
|
-
if (mainIdx >= 0) {
|
|
200
|
-
preAncestorChains.push(stack.slice(mainIdx + 1));
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
const isVoid = VOID_TAGS.has(tagName);
|
|
205
|
-
if (!isVoid && !explicitlySelfClosing) {
|
|
206
|
-
stack.push(el);
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
const content = resolveContentWrapper(explicit, mainEl, proseAncestorChains);
|
|
211
|
-
const codeBlock = resolveCodeBlockWrapper(content, preAncestorChains);
|
|
212
|
-
|
|
213
|
-
return { content, codeBlock };
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
/**
|
|
217
|
-
* Pick the content wrapper from the walk's collected data.
|
|
218
|
-
*
|
|
219
|
-
* Priority: explicit opt-in > majority-prose-wrapper-inside-main >
|
|
220
|
-
* `<main>` fallback > null.
|
|
221
|
-
*/
|
|
222
|
-
function resolveContentWrapper(
|
|
223
|
-
explicit: ContentWrapperInfo | null,
|
|
224
|
-
mainEl: OpenTag | null,
|
|
225
|
-
proseAncestorChains: OpenTag[][],
|
|
226
|
-
): ContentWrapperInfo | null {
|
|
227
|
-
if (explicit) return explicit;
|
|
228
|
-
if (!mainEl) return null;
|
|
229
|
-
|
|
230
|
-
if (proseAncestorChains.length === 0) {
|
|
231
|
-
return { tagName: 'main', className: mainEl.className };
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
// Tally classed-ancestor occurrences across all prose chains.
|
|
235
|
-
const scores = new Map<OpenTag, number>();
|
|
236
|
-
for (const chain of proseAncestorChains) {
|
|
237
|
-
for (const ancestor of chain) {
|
|
238
|
-
if (!ancestor.className) continue;
|
|
239
|
-
scores.set(ancestor, (scores.get(ancestor) ?? 0) + 1);
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
// Majority threshold rules out partial-coverage classed ancestors
|
|
244
|
-
// (single Cards holding one of many prose blocks).
|
|
245
|
-
const threshold = proseAncestorChains.length / 2;
|
|
246
|
-
const candidates = [...scores.entries()].filter(([, count]) => count > threshold);
|
|
247
|
-
|
|
248
|
-
if (candidates.length === 0) {
|
|
249
|
-
return { tagName: 'main', className: mainEl.className };
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
// Tie-break by depth: the candidate that appears deepest in any
|
|
253
|
-
// chain is the most specific (closest to the prose tags).
|
|
254
|
-
const maxChainIndex = (el: OpenTag) => Math.max(...proseAncestorChains.map((c) => c.indexOf(el)));
|
|
255
|
-
candidates.sort((a, b) => {
|
|
256
|
-
if (b[1] !== a[1]) return b[1] - a[1];
|
|
257
|
-
return maxChainIndex(b[0]) - maxChainIndex(a[0]);
|
|
258
|
-
});
|
|
259
|
-
|
|
260
|
-
const winner = candidates[0][0];
|
|
261
|
-
return { tagName: winner.tag, className: winner.className };
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
/**
|
|
265
|
-
* Derive the code-block wrapper chain from a representative `<pre>`'s
|
|
266
|
-
* ancestor chain. We strip everything at or above the content wrapper
|
|
267
|
-
* (its styles are already handled by the content-wrapper mirror) and
|
|
268
|
-
* return the remaining classed elements in nesting order, outermost
|
|
269
|
-
* first.
|
|
270
|
-
*
|
|
271
|
-
* Returning a chain (instead of one collapsed element) preserves the
|
|
272
|
-
* shape host CSS expects:
|
|
273
|
-
*
|
|
274
|
-
* - Single-class selectors (`.frame { ... }`) hit on whichever link
|
|
275
|
-
* carries that class.
|
|
276
|
-
* - Descendant selectors (`.expressive-code .frame { ... }`,
|
|
277
|
-
* `.frame .header > .title`) hit because the editor's renderer
|
|
278
|
-
* materialises the chain as nested elements with the host's
|
|
279
|
-
* expected tag names.
|
|
280
|
-
*
|
|
281
|
-
* The editor's code-block frame component (`code-block-frame.tsx` in
|
|
282
|
-
* `@conloca/mdx`) treats the LAST link as the "frame" element (where
|
|
283
|
-
* figcaption/pre/copy children render) and wraps it with the earlier
|
|
284
|
-
* links from inside out. This matches the host's authoring shape for
|
|
285
|
-
* libraries like ExpressiveCode (`.expressive-code > figure.frame >
|
|
286
|
-
* figcaption + pre + .copy`).
|
|
287
|
-
*/
|
|
288
|
-
function resolveCodeBlockWrapper(
|
|
289
|
-
content: ContentWrapperInfo | null,
|
|
290
|
-
preAncestorChains: OpenTag[][],
|
|
291
|
-
): ContentWrapperInfo[] | null {
|
|
292
|
-
if (preAncestorChains.length === 0) return null;
|
|
293
|
-
|
|
294
|
-
// Pick the first <pre>'s chain as representative. Code blocks on a
|
|
295
|
-
// single page are uniformly wrapped, so chains are interchangeable.
|
|
296
|
-
const chain = preAncestorChains[0];
|
|
297
|
-
|
|
298
|
-
// Strip everything at-or-above the content wrapper; the rest is
|
|
299
|
-
// code-block-specific chrome that this endpoint is responsible for.
|
|
300
|
-
const contentIdx = content
|
|
301
|
-
? chain.findIndex((el) => el.tag === content.tagName && el.className === content.className)
|
|
302
|
-
: -1;
|
|
303
|
-
const codeBlockChain = chain.slice(contentIdx + 1);
|
|
304
|
-
|
|
305
|
-
const classedAncestors = codeBlockChain.filter((el) => el.className);
|
|
306
|
-
if (classedAncestors.length === 0) return null;
|
|
307
|
-
|
|
308
|
-
return classedAncestors.map((el) => ({ tagName: el.tag, className: el.className }));
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
/**
|
|
312
|
-
* Parse the attribute blob inside an opening tag. Handles `name="v"`,
|
|
313
|
-
* `name='v'`, `name=v`, and boolean (no-value) attributes. Doesn't
|
|
314
|
-
* handle entity decoding (`&` → `&`) since the values we read
|
|
315
|
-
* (tag name, class list, the marker attribute) don't carry entities
|
|
316
|
-
* in practice.
|
|
317
|
-
*/
|
|
318
|
-
function parseAttributes(s: string): Record<string, string> {
|
|
319
|
-
const out: Record<string, string> = {};
|
|
320
|
-
const re = /([a-zA-Z_:][-a-zA-Z0-9_:.]*)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|(\S+)))?/g;
|
|
321
|
-
for (const match of s.matchAll(re)) {
|
|
322
|
-
const name = match[1];
|
|
323
|
-
const value = match[2] ?? match[3] ?? match[4] ?? '';
|
|
324
|
-
out[name] = value;
|
|
325
|
-
}
|
|
326
|
-
return out;
|
|
327
|
-
}
|
package/src/virtual-modules.d.ts
CHANGED
|
@@ -57,6 +57,20 @@ declare module 'virtual:conloca-puck-config' {
|
|
|
57
57
|
export default config;
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
+
/**
|
|
61
|
+
* Virtual module: virtual:conloca-content-schemas
|
|
62
|
+
*
|
|
63
|
+
* The site's `contentSchemasPath` module (the `pageSchemas` / `dataSchemas`
|
|
64
|
+
* its content config hands the collection factories), imported on the first
|
|
65
|
+
* call only; an empty object when the site names none. A preview render
|
|
66
|
+
* builds its collections' entry parsers from it.
|
|
67
|
+
*/
|
|
68
|
+
declare module 'virtual:conloca-content-schemas' {
|
|
69
|
+
import type { DataSchemas, PageSchemas } from '@conloca/cms-spa/page-schema-define';
|
|
70
|
+
|
|
71
|
+
export function loadContentSchemas(): Promise<{ pageSchemas?: PageSchemas; dataSchemas?: DataSchemas }>;
|
|
72
|
+
}
|
|
73
|
+
|
|
60
74
|
/**
|
|
61
75
|
* Virtual module: virtual:conloca-hydration-entry
|
|
62
76
|
*
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { type PageSchemas } from '@conloca/cms-spa/page-schema-define';
|
|
2
|
-
import type { ContentMeta } from '@conloca/content-api';
|
|
3
|
-
import { type PageMeta } from '@conloca/content-api/schemas';
|
|
4
|
-
import { z } from 'zod';
|
|
5
|
-
/**
|
|
6
|
-
* Fold every page-schema descriptor into one Zod object of host meta fields.
|
|
7
|
-
* Requiredness is not preserved — the caller always `.partial()`s this for the
|
|
8
|
-
* collection, because Astro validates the whole `pages` pile.
|
|
9
|
-
*
|
|
10
|
-
* The merged object has one slot per field name, so two descriptors may only
|
|
11
|
-
* declare the same name by sharing the same field schema instance — last-wins
|
|
12
|
-
* would silently rewrite what the earlier prefix's pages are validated
|
|
13
|
-
* against, failing them with a Zod error that names neither descriptor.
|
|
14
|
-
*/
|
|
15
|
-
export declare function hostFieldsFromPageSchemas(pageSchemas: PageSchemas): z.ZodObject<z.ZodRawShape>;
|
|
16
|
-
/**
|
|
17
|
-
* A page collection entry as the loader delivers it: the validated
|
|
18
|
-
* `pageMetaSchema` fields, the loader-written record fields (`created`,
|
|
19
|
-
* `modified`, `name`, `puckData` — admitted by the loose index at runtime),
|
|
20
|
-
* and the host's meta fields through `ContentMeta`/`HostPageMeta`.
|
|
21
|
-
*/
|
|
22
|
-
export type ConlocaPageEntry = ContentMeta & PageMeta & {
|
|
23
|
-
created?: string | Date;
|
|
24
|
-
modified?: string | Date;
|
|
25
|
-
name?: string;
|
|
26
|
-
puckData?: unknown;
|
|
27
|
-
};
|
|
28
|
-
/**
|
|
29
|
-
* The schema Astro validates page entries against: the host's page-meta fields
|
|
30
|
-
* (all optional) with conloca's structural fields layered over them, loose so
|
|
31
|
-
* undeclared top-level fields still reach the rendered page.
|
|
32
|
-
*
|
|
33
|
-
* Looseness is TOP-LEVEL ONLY. A nested `z.object()` still strips undeclared
|
|
34
|
-
* nested keys; a host that wants them preserved has to write `z.looseObject()`.
|
|
35
|
-
*
|
|
36
|
-
* Rewrite bans (.default / .coerce / .transform / .catch) walk nested objects,
|
|
37
|
-
* arrays, and unions. `.partial()` is still shallow.
|
|
38
|
-
*
|
|
39
|
-
* Internal on purpose: `src/collections.ts` is a published entry (the
|
|
40
|
-
* `./collections` subpath), so anything exported from there is frozen under
|
|
41
|
-
* semver. Nothing outside this package consumes it.
|
|
42
|
-
*/
|
|
43
|
-
export declare function buildPageCollectionSchema(hostFields: z.ZodObject<z.ZodRawShape>): z.ZodType<ConlocaPageEntry>;
|
|
44
|
-
//# sourceMappingURL=page-collection-schema.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"page-collection-schema.d.ts","sourceRoot":"","sources":["../../../../src/internal/page-collection-schema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,WAAW,EAA0B,MAAM,qCAAqC,CAAC;AAC/F,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AACxD,OAAO,EAAE,KAAK,QAAQ,EAAkB,MAAM,8BAA8B,CAAC;AAC7E,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAwGxB;;;;;;;;;GASG;AACH,wBAAgB,yBAAyB,CAAC,WAAW,EAAE,WAAW,GAAG,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,WAAW,CAAC,CA4B9F;AAED;;;;;GAKG;AACH,MAAM,MAAM,gBAAgB,GAAG,WAAW,GACxC,QAAQ,GAAG;IACT,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB,CAAC;AAEJ;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,yBAAyB,CAAC,UAAU,EAAE,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,gBAAgB,CAAC,CAc7G"}
|