@glw907/cairn-cms 0.10.0 → 0.14.0
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/dist/components/ComponentForm.svelte +33 -10
- package/dist/components/ComponentForm.svelte.d.ts.map +1 -1
- package/dist/components/IconPicker.svelte +53 -7
- package/dist/components/IconPicker.svelte.d.ts +7 -3
- package/dist/components/IconPicker.svelte.d.ts.map +1 -1
- package/dist/content/adapter.d.ts +4 -0
- package/dist/content/adapter.d.ts.map +1 -0
- package/dist/content/adapter.js +4 -0
- package/dist/content/concepts.js +2 -2
- package/dist/content/schema.d.ts +75 -0
- package/dist/content/schema.d.ts.map +1 -0
- package/dist/content/schema.js +72 -0
- package/dist/content/types.d.ts +30 -7
- package/dist/content/types.d.ts.map +1 -1
- package/dist/content/validate.d.ts +5 -3
- package/dist/content/validate.d.ts.map +1 -1
- package/dist/content/validate.js +14 -7
- package/dist/delivery/CairnHead.svelte +36 -0
- package/dist/delivery/CairnHead.svelte.d.ts +15 -0
- package/dist/delivery/CairnHead.svelte.d.ts.map +1 -0
- package/dist/delivery/content-index.d.ts +16 -6
- package/dist/delivery/content-index.d.ts.map +1 -1
- package/dist/delivery/content-index.js +17 -8
- package/dist/delivery/index.d.ts +26 -0
- package/dist/delivery/index.d.ts.map +1 -0
- package/dist/delivery/index.js +21 -0
- package/dist/delivery/json-ld.d.ts +2 -0
- package/dist/delivery/json-ld.d.ts.map +1 -0
- package/dist/delivery/json-ld.js +16 -0
- package/dist/delivery/responses.d.ts +14 -0
- package/dist/delivery/responses.d.ts.map +1 -0
- package/dist/delivery/responses.js +30 -0
- package/dist/delivery/seo-fields.d.ts +22 -0
- package/dist/delivery/seo-fields.d.ts.map +1 -0
- package/dist/delivery/seo-fields.js +32 -0
- package/dist/delivery/seo.d.ts +4 -0
- package/dist/delivery/seo.d.ts.map +1 -1
- package/dist/delivery/seo.js +11 -0
- package/dist/delivery/site-descriptors.d.ts +5 -0
- package/dist/delivery/site-descriptors.d.ts.map +1 -0
- package/dist/delivery/site-descriptors.js +9 -0
- package/dist/delivery/site-index.d.ts +8 -2
- package/dist/delivery/site-index.d.ts.map +1 -1
- package/dist/delivery/site-index.js +26 -2
- package/dist/delivery/site-indexes.d.ts +26 -0
- package/dist/delivery/site-indexes.d.ts.map +1 -0
- package/dist/delivery/site-indexes.js +22 -0
- package/dist/index.d.ts +9 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -2
- package/dist/render/component-grammar.d.ts +7 -0
- package/dist/render/component-grammar.d.ts.map +1 -1
- package/dist/render/component-grammar.js +27 -8
- package/dist/render/component-validate.js +3 -3
- package/dist/render/glyph.d.ts +4 -1
- package/dist/render/glyph.d.ts.map +1 -1
- package/dist/render/glyph.js +6 -2
- package/dist/render/registry.d.ts +23 -5
- package/dist/render/registry.d.ts.map +1 -1
- package/dist/render/registry.js +6 -0
- package/dist/render/rehype-dispatch.d.ts +1 -5
- package/dist/render/rehype-dispatch.d.ts.map +1 -1
- package/dist/render/rehype-dispatch.js +71 -19
- package/dist/render/remark-directives.d.ts +1 -1
- package/dist/render/remark-directives.d.ts.map +1 -1
- package/dist/render/remark-directives.js +37 -0
- package/dist/sveltekit/public-routes.d.ts +14 -0
- package/dist/sveltekit/public-routes.d.ts.map +1 -1
- package/dist/sveltekit/public-routes.js +22 -2
- package/package.json +6 -1
- package/src/lib/components/ComponentForm.svelte +33 -10
- package/src/lib/components/IconPicker.svelte +53 -7
- package/src/lib/content/adapter.ts +10 -0
- package/src/lib/content/concepts.ts +2 -2
- package/src/lib/content/schema.ts +133 -0
- package/src/lib/content/types.ts +30 -7
- package/src/lib/content/validate.ts +10 -7
- package/src/lib/delivery/CairnHead.svelte +36 -0
- package/src/lib/delivery/content-index.ts +39 -17
- package/src/lib/delivery/index.ts +36 -0
- package/src/lib/delivery/json-ld.ts +16 -0
- package/src/lib/delivery/responses.ts +34 -0
- package/src/lib/delivery/seo-fields.ts +43 -0
- package/src/lib/delivery/seo.ts +13 -0
- package/src/lib/delivery/site-descriptors.ts +12 -0
- package/src/lib/delivery/site-index.ts +26 -2
- package/src/lib/delivery/site-indexes.ts +52 -0
- package/src/lib/index.ts +8 -2
- package/src/lib/render/component-grammar.ts +34 -10
- package/src/lib/render/component-validate.ts +3 -3
- package/src/lib/render/glyph.ts +6 -2
- package/src/lib/render/registry.ts +27 -5
- package/src/lib/render/rehype-dispatch.ts +67 -20
- package/src/lib/render/remark-directives.ts +39 -1
- package/src/lib/sveltekit/public-routes.ts +33 -2
package/src/lib/delivery/seo.ts
CHANGED
|
@@ -13,6 +13,10 @@ export interface SeoInput {
|
|
|
13
13
|
modified?: string;
|
|
14
14
|
feeds?: { rss?: string; json?: string };
|
|
15
15
|
image?: string;
|
|
16
|
+
/** A robots meta directive, e.g. "noindex, nofollow". Omitted from the head when absent. */
|
|
17
|
+
robots?: string;
|
|
18
|
+
/** Author name, emitted as article:author for the article type. */
|
|
19
|
+
author?: string;
|
|
16
20
|
}
|
|
17
21
|
|
|
18
22
|
/** Plain-data head: a title, meta tags, link tags, and one JSON-LD object. */
|
|
@@ -40,6 +44,15 @@ export function buildSeoMeta(input: SeoInput): SeoMeta {
|
|
|
40
44
|
meta.push({ name: 'twitter:image', content: input.image });
|
|
41
45
|
}
|
|
42
46
|
|
|
47
|
+
if (input.robots) {
|
|
48
|
+
meta.push({ name: 'robots', content: input.robots });
|
|
49
|
+
}
|
|
50
|
+
if (type === 'article') {
|
|
51
|
+
if (input.published) meta.push({ property: 'article:published_time', content: input.published });
|
|
52
|
+
if (input.modified) meta.push({ property: 'article:modified_time', content: input.modified });
|
|
53
|
+
if (input.author) meta.push({ property: 'article:author', content: input.author });
|
|
54
|
+
}
|
|
55
|
+
|
|
43
56
|
const links: SeoMeta['links'] = [{ rel: 'canonical', href: input.canonicalUrl }];
|
|
44
57
|
if (input.feeds?.rss) {
|
|
45
58
|
links.push({ rel: 'alternate', type: 'application/rss+xml', href: input.feeds.rss, title: input.siteName });
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// cairn-cms: the one-call descriptor helper. A delivery site needs the same per-concept
|
|
2
|
+
// descriptors the admin runtime uses; this wraps the two calls that derive them so the
|
|
3
|
+
// pairing is not tribal knowledge. The YAML URL policy stays the single source of truth.
|
|
4
|
+
import { normalizeConcepts } from '../content/concepts.js';
|
|
5
|
+
import { urlPolicyFrom } from '../nav/site-config.js';
|
|
6
|
+
import type { CairnAdapter, ConceptDescriptor } from '../content/types.js';
|
|
7
|
+
import type { SiteConfig } from '../nav/site-config.js';
|
|
8
|
+
|
|
9
|
+
/** Per-concept descriptors for a site, from its adapter content and its parsed site config. */
|
|
10
|
+
export function siteDescriptors(adapter: CairnAdapter, siteConfig: SiteConfig): ConceptDescriptor[] {
|
|
11
|
+
return normalizeConcepts(adapter.content, urlPolicyFrom(siteConfig));
|
|
12
|
+
}
|
|
@@ -30,8 +30,32 @@ function normalizePath(path: string): string {
|
|
|
30
30
|
return path.length > 1 ? path.replace(/\/+$/, '') : path;
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
-
/**
|
|
34
|
-
|
|
33
|
+
/** Collect non-draft validation failures across concepts from each index's recorded verdicts. */
|
|
34
|
+
function siteProblems(concepts: ConceptIndex[]): string[] {
|
|
35
|
+
const problems: string[] = [];
|
|
36
|
+
for (const { descriptor, index } of concepts) {
|
|
37
|
+
for (const problem of index.problems()) {
|
|
38
|
+
if (problem.draft) continue; // a half-finished draft never ships, so it does not fail the build
|
|
39
|
+
for (const [field, message] of Object.entries(problem.errors)) {
|
|
40
|
+
problems.push(`${descriptor.dir}/${problem.id}: ${field}: ${message}`);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return problems;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Union per-concept indexes into a site-level resolver. Throws on a duplicate permalink and,
|
|
49
|
+
* unless `validate` is `false`, on any non-draft entry whose frontmatter fails its concept's
|
|
50
|
+
* validator, so malformed content fails the build instead of shipping.
|
|
51
|
+
*/
|
|
52
|
+
export function createSiteIndex(concepts: ConceptIndex[], opts: { validate?: boolean } = {}): SiteIndex {
|
|
53
|
+
if (opts.validate !== false) {
|
|
54
|
+
const problems = siteProblems(concepts);
|
|
55
|
+
if (problems.length > 0) {
|
|
56
|
+
throw new Error(`site index: ${problems.length} invalid frontmatter field(s):\n ${problems.join('\n ')}`);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
35
59
|
const byPath = new Map<string, { index: ContentIndex; id: string }>();
|
|
36
60
|
const byId = new Map<string, ContentIndex>();
|
|
37
61
|
for (const { descriptor, index } of concepts) {
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// cairn-cms: the full-auto typed site index (schema-source-of-truth design). It maps over a
|
|
2
|
+
// defineAdapter-typed adapter to give one typed per-concept index, with frontmatter typed as the
|
|
3
|
+
// concept's inferred schema type, plus a site resolver for the catch-all route. It is the typed
|
|
4
|
+
// convenience over createContentIndex and createSiteIndex, not a replacement: both stay the
|
|
5
|
+
// lower-level escape hatch. It imports only pure content and delivery code, so the delivery
|
|
6
|
+
// bundle stays backend-free.
|
|
7
|
+
import type { CairnAdapter, ConceptConfig } from '../content/types.js';
|
|
8
|
+
import type { Infer } from '../content/schema.js';
|
|
9
|
+
import type { SiteConfig } from '../nav/site-config.js';
|
|
10
|
+
import { siteDescriptors } from './site-descriptors.js';
|
|
11
|
+
import { createContentIndex, fromGlob } from './content-index.js';
|
|
12
|
+
import { createSiteIndex } from './site-index.js';
|
|
13
|
+
import type { ContentIndex } from './content-index.js';
|
|
14
|
+
import type { ConceptIndex, SiteIndex } from './site-index.js';
|
|
15
|
+
|
|
16
|
+
/** A per-concept raw glob record (`{ path: raw }`) keyed by concept id, from `import.meta.glob`. */
|
|
17
|
+
export type SiteGlobs<A extends CairnAdapter> = {
|
|
18
|
+
[K in keyof A['content']]?: Record<string, string>;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/** The typed per-concept indexes plus the cross-concept `site` resolver. A concept literally named
|
|
22
|
+
* `site` is not supported, since `site` is the reserved resolver key. */
|
|
23
|
+
export type SiteIndexes<A extends CairnAdapter> = {
|
|
24
|
+
[K in keyof A['content']]: ContentIndex<
|
|
25
|
+
NonNullable<A['content'][K]> extends ConceptConfig<infer S> ? Infer<S> : Record<string, unknown>
|
|
26
|
+
>;
|
|
27
|
+
} & { readonly site: SiteIndex };
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Build typed per-concept indexes and a site resolver from one adapter. Pass the per-concept raw
|
|
31
|
+
* globs as `{ posts: import.meta.glob('...?raw', { eager: true }), ... }`; Vite needs the literal
|
|
32
|
+
* glob at the call site, so the engine cannot glob on the site's behalf. `validate: false` opts out
|
|
33
|
+
* of the build gate, exactly as on `createSiteIndex`.
|
|
34
|
+
*/
|
|
35
|
+
export function createSiteIndexes<const A extends CairnAdapter>(
|
|
36
|
+
adapter: A,
|
|
37
|
+
config: SiteConfig,
|
|
38
|
+
globs: SiteGlobs<A>,
|
|
39
|
+
opts: { validate?: boolean } = {},
|
|
40
|
+
): SiteIndexes<A> {
|
|
41
|
+
const descriptors = siteDescriptors(adapter, config);
|
|
42
|
+
const byConcept: Record<string, ContentIndex> = {};
|
|
43
|
+
const conceptIndexes: ConceptIndex[] = [];
|
|
44
|
+
for (const descriptor of descriptors) {
|
|
45
|
+
const record = (globs as Record<string, Record<string, string> | undefined>)[descriptor.id] ?? {};
|
|
46
|
+
const index = createContentIndex(fromGlob(record), descriptor);
|
|
47
|
+
byConcept[descriptor.id] = index;
|
|
48
|
+
conceptIndexes.push({ descriptor, index });
|
|
49
|
+
}
|
|
50
|
+
const site = createSiteIndex(conceptIndexes, opts);
|
|
51
|
+
return { ...byConcept, site } as SiteIndexes<A>;
|
|
52
|
+
}
|
package/src/lib/index.ts
CHANGED
|
@@ -37,7 +37,9 @@ export {
|
|
|
37
37
|
serializeMarkdown,
|
|
38
38
|
parseMarkdown,
|
|
39
39
|
} from './content/frontmatter.js';
|
|
40
|
-
export {
|
|
40
|
+
export { defineFields } from './content/schema.js';
|
|
41
|
+
export { defineAdapter } from './content/adapter.js';
|
|
42
|
+
export type { ConceptSchema, Infer, InferFields, DefineFieldsOptions, StandardInput, StandardSchemaV1 } from './content/schema.js';
|
|
41
43
|
export {
|
|
42
44
|
isValidId,
|
|
43
45
|
idFromFilename,
|
|
@@ -72,7 +74,6 @@ export {
|
|
|
72
74
|
isElement,
|
|
73
75
|
strProp,
|
|
74
76
|
iconSpan,
|
|
75
|
-
splitHead,
|
|
76
77
|
cardShell,
|
|
77
78
|
markFirstList,
|
|
78
79
|
} from './render/rehype-dispatch.js';
|
|
@@ -119,9 +120,12 @@ export type {
|
|
|
119
120
|
ContentSummary,
|
|
120
121
|
ContentEntry,
|
|
121
122
|
ContentIndex,
|
|
123
|
+
ContentProblem,
|
|
122
124
|
} from './delivery/content-index.js';
|
|
123
125
|
export { createSiteIndex } from './delivery/site-index.js';
|
|
124
126
|
export type { SiteIndex, ConceptIndex } from './delivery/site-index.js';
|
|
127
|
+
export { createSiteIndexes } from './delivery/site-indexes.js';
|
|
128
|
+
export type { SiteIndexes, SiteGlobs } from './delivery/site-indexes.js';
|
|
125
129
|
export { deriveExcerpt, wordCount } from './delivery/excerpt.js';
|
|
126
130
|
export { buildRssFeed, buildJsonFeed } from './delivery/feeds.js';
|
|
127
131
|
export type { FeedChannel, FeedItem } from './delivery/feeds.js';
|
|
@@ -130,5 +134,7 @@ export type { SitemapUrl } from './delivery/sitemap.js';
|
|
|
130
134
|
export { buildRobots } from './delivery/robots.js';
|
|
131
135
|
export { buildSeoMeta } from './delivery/seo.js';
|
|
132
136
|
export type { SeoInput, SeoMeta } from './delivery/seo.js';
|
|
137
|
+
export { readSeoFields, resolveImageUrl } from './delivery/seo-fields.js';
|
|
138
|
+
export type { SeoFields } from './delivery/seo-fields.js';
|
|
133
139
|
export { paginate } from './delivery/paginate.js';
|
|
134
140
|
export type { Page } from './delivery/paginate.js';
|
|
@@ -84,14 +84,19 @@ function childrenToText(children: RootContent[]): string {
|
|
|
84
84
|
return String(toMd.stringify(root)).trim();
|
|
85
85
|
}
|
|
86
86
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
87
|
+
// Parse the markdown and find the component's opening container directive. The single seam both
|
|
88
|
+
// parseComponent and parseRawAttributeKeys (and the combined validator helper) build on, so one
|
|
89
|
+
// parse derives both the form values and the raw attribute keys.
|
|
90
|
+
function findComponentRoot(markdown: string, def: ComponentDef): (RootContent & DirectiveNode) | undefined {
|
|
91
91
|
const tree = unified().use(remarkParse).use(remarkDirective).parse(markdown) as Root;
|
|
92
|
-
|
|
92
|
+
return tree.children.find(
|
|
93
93
|
(c): c is RootContent & DirectiveNode => isContainer(c) && (c as DirectiveNode).name === def.name,
|
|
94
94
|
);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Build guided-form values from an already-found component root. Returns the empty base when the
|
|
98
|
+
// root is absent.
|
|
99
|
+
function valuesFromRoot(root: (RootContent & DirectiveNode) | undefined, def: ComponentDef): ComponentValues {
|
|
95
100
|
const values = emptyComponentValues(def);
|
|
96
101
|
if (!root) return values;
|
|
97
102
|
|
|
@@ -126,14 +131,33 @@ export async function parseComponent(markdown: string, def: ComponentDef): Promi
|
|
|
126
131
|
return values;
|
|
127
132
|
}
|
|
128
133
|
|
|
134
|
+
// The raw attribute keys on an already-found component root.
|
|
135
|
+
function rawKeysFromRoot(root: (RootContent & DirectiveNode) | undefined): string[] {
|
|
136
|
+
return Object.keys(root?.attributes ?? {});
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Parse a serialized component directive back into guided-form values, the inverse of
|
|
140
|
+
* {@link serializeComponent}. The grammar is reversible, so the editor can round-trip a
|
|
141
|
+
* saved directive through the form. */
|
|
142
|
+
export async function parseComponent(markdown: string, def: ComponentDef): Promise<ComponentValues> {
|
|
143
|
+
return valuesFromRoot(findComponentRoot(markdown, def), def);
|
|
144
|
+
}
|
|
145
|
+
|
|
129
146
|
/** The raw attribute keys present on the component's opening directive, read from the parsed tree
|
|
130
147
|
* (quote-aware, unlike a regex over the source). Used by validation to flag unknown keys. */
|
|
131
148
|
export function parseRawAttributeKeys(markdown: string, def: ComponentDef): string[] {
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
149
|
+
return rawKeysFromRoot(findComponentRoot(markdown, def));
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** Parse the component once and derive both the guided-form values and the raw attribute keys.
|
|
153
|
+
* Validation needs both, so this seam spares it the double parse that calling
|
|
154
|
+
* {@link parseComponent} and {@link parseRawAttributeKeys} separately would cost. */
|
|
155
|
+
export async function parseComponentWithRawKeys(
|
|
156
|
+
markdown: string,
|
|
157
|
+
def: ComponentDef,
|
|
158
|
+
): Promise<{ values: ComponentValues; rawKeys: string[] }> {
|
|
159
|
+
const root = findComponentRoot(markdown, def);
|
|
160
|
+
return { values: valuesFromRoot(root, def), rawKeys: rawKeysFromRoot(root) };
|
|
137
161
|
}
|
|
138
162
|
|
|
139
163
|
// A bare parse base: empty strings, false, and empty lists, with no attribute defaults applied. The
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { parseComponentWithRawKeys } from './component-grammar.js';
|
|
2
2
|
import type { ComponentDef } from './registry.js';
|
|
3
3
|
|
|
4
4
|
/** A validation verdict: ok, or field-keyed error messages. */
|
|
5
5
|
export type ComponentValidation = { ok: true } | { ok: false; errors: Record<string, string> };
|
|
6
6
|
|
|
7
7
|
export async function validateComponent(markdown: string, def: ComponentDef): Promise<ComponentValidation> {
|
|
8
|
-
const values = await
|
|
8
|
+
const { values, rawKeys } = await parseComponentWithRawKeys(markdown, def);
|
|
9
9
|
const errors: Record<string, string> = {};
|
|
10
10
|
const declared = new Set((def.attributes ?? []).map((f) => f.key));
|
|
11
11
|
|
|
@@ -21,7 +21,7 @@ export async function validateComponent(markdown: string, def: ComponentDef): Pr
|
|
|
21
21
|
}
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
for (const key of
|
|
24
|
+
for (const key of rawKeys) {
|
|
25
25
|
if (!declared.has(key)) errors[key] = `Unknown attribute "${key}".`;
|
|
26
26
|
}
|
|
27
27
|
|
package/src/lib/render/glyph.ts
CHANGED
|
@@ -4,11 +4,15 @@ import type { Element } from 'hast';
|
|
|
4
4
|
/** A glyph name to SVG path-data map (the site owns the icon set). */
|
|
5
5
|
export type IconSet = Record<string, string>;
|
|
6
6
|
|
|
7
|
-
/** Inline SVG glyph as a real hast node: class ec-glyph, 256 viewBox, currentColor fill.
|
|
7
|
+
/** Inline SVG glyph as a real hast node: class ec-glyph, 256 viewBox, currentColor fill.
|
|
8
|
+
* An unknown icon name yields the bare svg shell with no path child, so it never serializes
|
|
9
|
+
* a stray empty (or undefined) path. Callers always wrap the returned element, so the shell
|
|
10
|
+
* keeps them safe. */
|
|
8
11
|
export function glyph(name: string, icons: IconSet): Element {
|
|
12
|
+
const d = icons[name];
|
|
9
13
|
return s(
|
|
10
14
|
'svg',
|
|
11
15
|
{ className: ['ec-glyph'], viewBox: '0 0 256 256', fill: 'currentColor', ariaHidden: 'true' },
|
|
12
|
-
[s('path', { d
|
|
16
|
+
d == null ? [] : [s('path', { d })],
|
|
13
17
|
);
|
|
14
18
|
}
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
// (Plan 04) and the future component palette both derive from this single source, so the
|
|
4
4
|
// parser, the render dispatch, and the editor never drift apart. The adapter references
|
|
5
5
|
// `ComponentRegistry` from here.
|
|
6
|
-
import type { Element } from 'hast';
|
|
6
|
+
import type { Element, ElementContent } from 'hast';
|
|
7
7
|
|
|
8
8
|
/** The input types a component attribute or repeatable item field can take. */
|
|
9
9
|
export type FieldType = 'text' | 'select' | 'icon' | 'boolean';
|
|
@@ -38,6 +38,21 @@ export interface SlotDef {
|
|
|
38
38
|
itemFields?: AttributeField[];
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
+
/** The structured input a component's `build` receives. The engine stamps the component's
|
|
42
|
+
* attributes and partitions its slots from the rendered hast, so `build` arranges hast and
|
|
43
|
+
* never walks the tree. `slot(name)` returns a slot's rendered children (title, body, or any
|
|
44
|
+
* named slot); `items(name)` returns a repeatable slot's items, one child list per item. */
|
|
45
|
+
export interface ComponentContext {
|
|
46
|
+
/** Declared attribute values, keyed by attribute key. Booleans are real booleans. */
|
|
47
|
+
attributes: Record<string, string | boolean>;
|
|
48
|
+
/** A named slot's rendered children. Returns `[]` for an absent or empty slot. */
|
|
49
|
+
slot(name: string): ElementContent[];
|
|
50
|
+
/** A repeatable slot's items, each item its own list of rendered children. `[]` when absent. */
|
|
51
|
+
items(name: string): ElementContent[][];
|
|
52
|
+
/** The stamped component element, for an escape hatch. Most builds never need it. */
|
|
53
|
+
node: Element;
|
|
54
|
+
}
|
|
55
|
+
|
|
41
56
|
/** A site component: how it inserts (editor) and how it renders (rehype). */
|
|
42
57
|
export interface ComponentDef {
|
|
43
58
|
/** Directive name, e.g. 'card' (matches `:::card`). */
|
|
@@ -48,10 +63,10 @@ export interface ComponentDef {
|
|
|
48
63
|
description: string;
|
|
49
64
|
/** Markdown scaffold inserted at the cursor by the editor palette. */
|
|
50
65
|
insertTemplate?: string;
|
|
51
|
-
/** Build the final hast element from the
|
|
52
|
-
* stamps the entrance-stagger ordinal (`data-rise`) on the top-level
|
|
53
|
-
* build fn stays free of any motion concern. */
|
|
54
|
-
build: (
|
|
66
|
+
/** Build the final hast element from the component context (attributes plus partitioned
|
|
67
|
+
* slots). The engine stamps the entrance-stagger ordinal (`data-rise`) on the top-level
|
|
68
|
+
* result, so a build fn stays free of any motion concern. */
|
|
69
|
+
build: (ctx: ComponentContext) => Element;
|
|
55
70
|
/** Optional role-to-default-icon, e.g. `{ caution: 'warning' }`. */
|
|
56
71
|
defaultIconByRole?: Record<string, string>;
|
|
57
72
|
/** One line on when to reach for this component; feeds the picker and the reference file. */
|
|
@@ -69,6 +84,13 @@ export interface ComponentRegistry {
|
|
|
69
84
|
defaultIcon(name: string, role?: string): string | undefined;
|
|
70
85
|
}
|
|
71
86
|
|
|
87
|
+
/** The hast property name carrying one declared attribute from stamp to dispatch, e.g. `tone`
|
|
88
|
+
* becomes `dataAttrTone`. The directive stamp writes it and the rehype dispatch reads it, so both
|
|
89
|
+
* sides derive the name from this one helper rather than spelling the capitalize twice. */
|
|
90
|
+
export function dataAttrProp(key: string): string {
|
|
91
|
+
return `dataAttr${key.charAt(0).toUpperCase()}${key.slice(1)}`;
|
|
92
|
+
}
|
|
93
|
+
|
|
72
94
|
/**
|
|
73
95
|
* Build a registry from a site's component definitions. The single source the render
|
|
74
96
|
* pipeline (directive stamp plus rehype dispatch) and the editor palette both read.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Root, Element, ElementContent } from 'hast';
|
|
2
2
|
import { h } from 'hastscript';
|
|
3
|
-
import type
|
|
3
|
+
import { dataAttrProp, type ComponentContext, type ComponentDef, type ComponentRegistry } from './registry.js';
|
|
4
4
|
|
|
5
5
|
export function isElement(node: ElementContent | undefined): node is Element {
|
|
6
6
|
return !!node && node.type === 'element';
|
|
@@ -23,24 +23,6 @@ export function iconSpan(glyphEl: Element, role?: string): Element {
|
|
|
23
23
|
/** A site's icon factory: turn a stamped icon name + role into a hast element. */
|
|
24
24
|
export type MakeIcon = (name: string, role?: string) => Element;
|
|
25
25
|
|
|
26
|
-
// Pull the section's <h2> out, retag it .card-title, and build the .ec-head row
|
|
27
|
-
// (optional icon + heading). Returns the head plus the remaining body children.
|
|
28
|
-
// `makeIcon` (site-supplied) turns the stamped data-icon into an element; omit it
|
|
29
|
-
// for a head with no icon.
|
|
30
|
-
export function splitHead(node: Element, makeIcon?: MakeIcon): { head: Element; rest: ElementContent[] } {
|
|
31
|
-
const children = node.children as ElementContent[];
|
|
32
|
-
const i = children.findIndex((c) => isElement(c) && c.tagName === 'h2');
|
|
33
|
-
const h2 = children[i] as Element;
|
|
34
|
-
h2.properties = { ...h2.properties, className: ['card-title'] };
|
|
35
|
-
const rest = children.filter((_, j) => j !== i);
|
|
36
|
-
const icon = strProp(node, 'dataIcon');
|
|
37
|
-
const role = strProp(node, 'dataRole');
|
|
38
|
-
const headKids: ElementContent[] = [];
|
|
39
|
-
if (makeIcon && icon) headKids.push(makeIcon(icon, role));
|
|
40
|
-
headKids.push(h2);
|
|
41
|
-
return { head: h('div', { className: ['ec-head'] }, headKids), rest };
|
|
42
|
-
}
|
|
43
|
-
|
|
44
26
|
/** Section wrapper: `<section class=…><div class="card-body">…</div></section>`. */
|
|
45
27
|
export function cardShell(classes: string[], body: ElementContent[]): Element {
|
|
46
28
|
return h('section', { className: classes }, [h('div', { className: ['card-body'] }, body)]);
|
|
@@ -70,11 +52,76 @@ function transformChildren(children: ElementContent[], registry: ComponentRegist
|
|
|
70
52
|
});
|
|
71
53
|
}
|
|
72
54
|
|
|
55
|
+
// Read a stamped attribute back into its typed value. Booleans arrive as the strings
|
|
56
|
+
// 'true'/'false'; everything else is the literal string the author wrote.
|
|
57
|
+
function readAttributes(node: Element, def: ComponentDef): Record<string, string | boolean> {
|
|
58
|
+
const out: Record<string, string | boolean> = {};
|
|
59
|
+
for (const field of def.attributes ?? []) {
|
|
60
|
+
const value = strProp(node, dataAttrProp(field.key));
|
|
61
|
+
if (value == null) continue;
|
|
62
|
+
out[field.key] = field.type === 'boolean' ? value === 'true' : value;
|
|
63
|
+
}
|
|
64
|
+
return out;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// The title label paragraph carries data-slot="title"; build() wants its inline children, not
|
|
68
|
+
// the marked paragraph. Return the paragraph's children.
|
|
69
|
+
function stripSlotMarker(child: ElementContent): ElementContent[] {
|
|
70
|
+
return isElement(child) ? (child.children as ElementContent[]) : [child];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Split a component's stamped children into named slots and the default body. A child marked
|
|
74
|
+
// data-slot="title"/<name> routes to that slot; an unmarked child is body. A repeatable slot
|
|
75
|
+
// wraps a <ul>, so its items are that list's <li> children, one child-list per item.
|
|
76
|
+
function partitionSlots(node: Element): {
|
|
77
|
+
slot(name: string): ElementContent[];
|
|
78
|
+
items(name: string): ElementContent[][];
|
|
79
|
+
} {
|
|
80
|
+
const named = new Map<string, ElementContent[]>();
|
|
81
|
+
const body: ElementContent[] = [];
|
|
82
|
+
for (const child of node.children as ElementContent[]) {
|
|
83
|
+
const slotName = isElement(child) ? strProp(child, 'dataSlot') : undefined;
|
|
84
|
+
if (slotName === 'title') named.set('title', stripSlotMarker(child));
|
|
85
|
+
else if (slotName) named.set(slotName, [child]);
|
|
86
|
+
else body.push(child);
|
|
87
|
+
}
|
|
88
|
+
return {
|
|
89
|
+
slot(name: string): ElementContent[] {
|
|
90
|
+
if (name === 'body') return body;
|
|
91
|
+
const wrap = named.get(name);
|
|
92
|
+
if (!wrap) return [];
|
|
93
|
+
// For title we stored the label's own children, so return them as-is. For a markdown or
|
|
94
|
+
// inline named slot the wrapper <div> holds the rendered children; unwrap it.
|
|
95
|
+
if (name === 'title') return wrap;
|
|
96
|
+
const div = wrap[0];
|
|
97
|
+
return isElement(div) ? (div.children as ElementContent[]) : wrap;
|
|
98
|
+
},
|
|
99
|
+
items(name: string): ElementContent[][] {
|
|
100
|
+
const wrap = named.get(name);
|
|
101
|
+
const div = wrap?.[0];
|
|
102
|
+
if (!div || !isElement(div)) return [];
|
|
103
|
+
const ul = (div.children as ElementContent[]).find((c) => isElement(c) && c.tagName === 'ul');
|
|
104
|
+
if (!ul || !isElement(ul)) return [];
|
|
105
|
+
return (ul.children as ElementContent[])
|
|
106
|
+
.filter((li) => isElement(li) && li.tagName === 'li')
|
|
107
|
+
.map((li) => (li as Element).children as ElementContent[]);
|
|
108
|
+
},
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
73
112
|
function transformNode(node: Element, registry: ComponentRegistry): Element {
|
|
74
113
|
node.children = transformChildren(node.children as ElementContent[], registry);
|
|
75
114
|
const name = strProp(node, 'dataPrimitive');
|
|
76
115
|
const def = name ? registry.get(name) : undefined;
|
|
77
|
-
|
|
116
|
+
if (!def) return node;
|
|
117
|
+
const parts = partitionSlots(node);
|
|
118
|
+
const ctx: ComponentContext = {
|
|
119
|
+
attributes: readAttributes(node, def),
|
|
120
|
+
slot: parts.slot,
|
|
121
|
+
items: parts.items,
|
|
122
|
+
node,
|
|
123
|
+
};
|
|
124
|
+
return def.build(ctx);
|
|
78
125
|
}
|
|
79
126
|
|
|
80
127
|
/** Rehype transformer: dispatch each stamped element through its registry `build`
|
|
@@ -1,7 +1,22 @@
|
|
|
1
1
|
import type { Paragraph, PhrasingContent, Root, Text } from 'mdast';
|
|
2
2
|
import type { ContainerDirective, LeafDirective, TextDirective } from 'mdast-util-directive';
|
|
3
3
|
import { visit } from 'unist-util-visit';
|
|
4
|
-
import type
|
|
4
|
+
import { dataAttrProp, type ComponentRegistry } from './registry.js';
|
|
5
|
+
|
|
6
|
+
// mdast-util-directive carries the `[label]` as a paragraph whose `data.directiveLabel` is set.
|
|
7
|
+
function isDirectiveLabel(node: unknown): boolean {
|
|
8
|
+
return Boolean((node as { data?: { directiveLabel?: boolean } }).data?.directiveLabel);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
// Stamp data-slot on a child so the rehype dispatch partitioner can route it. For a nested
|
|
12
|
+
// container directive we also set hName so it renders as a <div> wrapper rather than being
|
|
13
|
+
// dropped as an unknown directive.
|
|
14
|
+
function markSlot(node: unknown, name: string): void {
|
|
15
|
+
const n = node as { type?: string; data?: { hName?: string; hProperties?: Record<string, string> } };
|
|
16
|
+
const data = n.data ?? (n.data = {});
|
|
17
|
+
if (n.type === 'containerDirective') data.hName = 'div';
|
|
18
|
+
data.hProperties = { ...(data.hProperties ?? {}), dataSlot: name };
|
|
19
|
+
}
|
|
5
20
|
|
|
6
21
|
// Reconstruct a directive's authored attribute block (`{#id .class key="value"}`).
|
|
7
22
|
// Accidental prose directives carry none, so this is almost always empty.
|
|
@@ -41,6 +56,7 @@ export function remarkDirectiveStamp(registry: ComponentRegistry) {
|
|
|
41
56
|
return (tree: Root) => {
|
|
42
57
|
visit(tree, 'containerDirective', (node: ContainerDirective) => {
|
|
43
58
|
if (!known.has(node.name)) return;
|
|
59
|
+
const def = registry.get(node.name);
|
|
44
60
|
const attrs = node.attributes ?? {};
|
|
45
61
|
const role = attrs.role || undefined;
|
|
46
62
|
let icon = attrs.icon || undefined;
|
|
@@ -49,10 +65,32 @@ export function remarkDirectiveStamp(registry: ComponentRegistry) {
|
|
|
49
65
|
const properties: Record<string, string> = { dataPrimitive: node.name };
|
|
50
66
|
if (icon) properties.dataIcon = icon;
|
|
51
67
|
if (role) properties.dataRole = role;
|
|
68
|
+
// Carry every declared attribute to hast so the dispatch partitioner can build the
|
|
69
|
+
// component context. data-attr-<key> survives to the element; build() consumes it and
|
|
70
|
+
// returns a fresh element, so the marker never reaches the published DOM.
|
|
71
|
+
for (const field of def?.attributes ?? []) {
|
|
72
|
+
const raw = attrs[field.key];
|
|
73
|
+
if (raw != null) properties[dataAttrProp(field.key)] = raw;
|
|
74
|
+
}
|
|
52
75
|
|
|
53
76
|
const data = node.data ?? (node.data = {});
|
|
54
77
|
data.hName = 'div';
|
|
55
78
|
data.hProperties = properties;
|
|
79
|
+
|
|
80
|
+
// Mark the title label paragraph and the nested slot directives so they survive to hast
|
|
81
|
+
// and the partitioner can find them. A slot named in the component schema (other than the
|
|
82
|
+
// default body) is a nested container directive; the title is the directive [label].
|
|
83
|
+
const slotNames = new Set((def?.slots ?? []).map((s) => s.name));
|
|
84
|
+
for (const child of node.children) {
|
|
85
|
+
if (isDirectiveLabel(child) && slotNames.has('title')) {
|
|
86
|
+
markSlot(child, 'title');
|
|
87
|
+
} else if (
|
|
88
|
+
(child as { type?: string }).type === 'containerDirective' &&
|
|
89
|
+
slotNames.has((child as { name: string }).name)
|
|
90
|
+
) {
|
|
91
|
+
markSlot(child, (child as { name: string }).name);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
56
94
|
});
|
|
57
95
|
|
|
58
96
|
visit(tree, ['textDirective', 'leafDirective'], (node, index, parent) => {
|
|
@@ -6,12 +6,24 @@
|
|
|
6
6
|
import { error } from '@sveltejs/kit';
|
|
7
7
|
import type { ContentSummary, ContentEntry } from '../delivery/content-index.js';
|
|
8
8
|
import type { SiteIndex } from '../delivery/site-index.js';
|
|
9
|
+
import { buildSeoMeta } from '../delivery/seo.js';
|
|
10
|
+
import type { SeoMeta } from '../delivery/seo.js';
|
|
11
|
+
import { readSeoFields, resolveImageUrl } from '../delivery/seo-fields.js';
|
|
9
12
|
|
|
10
13
|
/** Injected dependencies for the public loaders. */
|
|
11
14
|
export interface PublicRoutesDeps {
|
|
12
15
|
site: SiteIndex;
|
|
13
16
|
render: (md: string, opts?: { stagger?: boolean }) => string | Promise<string>;
|
|
14
17
|
origin: string;
|
|
18
|
+
/** Site name for og:site_name and the SEO head. */
|
|
19
|
+
siteName: string;
|
|
20
|
+
/** Default description used when an entry has none. */
|
|
21
|
+
description: string;
|
|
22
|
+
/** Absolute feed URLs for the head's autodiscovery links. */
|
|
23
|
+
feeds?: { rss?: string; json?: string };
|
|
24
|
+
/** A site-wide default OG image, used when an entry declares none. Resolved to absolute like the
|
|
25
|
+
* canonical URL, so a relative path such as "/og/default.png" works. */
|
|
26
|
+
defaultImage?: string;
|
|
15
27
|
}
|
|
16
28
|
|
|
17
29
|
/** The archive and tag list data: summaries the template renders. */
|
|
@@ -34,13 +46,14 @@ export interface EntryData {
|
|
|
34
46
|
entry: ContentEntry;
|
|
35
47
|
html: string;
|
|
36
48
|
canonicalUrl: string;
|
|
49
|
+
seo: SeoMeta;
|
|
37
50
|
newer?: ContentSummary;
|
|
38
51
|
older?: ContentSummary;
|
|
39
52
|
}
|
|
40
53
|
|
|
41
54
|
/** Build the public loaders for a site's unified index. */
|
|
42
55
|
export function createPublicRoutes(deps: PublicRoutesDeps) {
|
|
43
|
-
const { site, render, origin } = deps;
|
|
56
|
+
const { site, render, origin, siteName, description, feeds, defaultImage } = deps;
|
|
44
57
|
|
|
45
58
|
/** Resolve one concept's index by id, or a 404 (the route names an unconfigured concept). */
|
|
46
59
|
function indexOf(conceptId: string) {
|
|
@@ -54,7 +67,25 @@ export function createPublicRoutes(deps: PublicRoutesDeps) {
|
|
|
54
67
|
const entry = site.byPermalink(event.url.pathname);
|
|
55
68
|
if (!entry) throw error(404, `Not found: ${event.url.pathname}`);
|
|
56
69
|
const { newer, older } = site.adjacent(entry);
|
|
57
|
-
|
|
70
|
+
const canonicalUrl = origin + entry.permalink;
|
|
71
|
+
const fields = readSeoFields(entry.frontmatter);
|
|
72
|
+
const rawImage = fields.image ?? defaultImage;
|
|
73
|
+
const image = rawImage ? resolveImageUrl(rawImage, origin) : undefined;
|
|
74
|
+
// A dated entry is an article; an undated one (a page) is a website.
|
|
75
|
+
const seo = buildSeoMeta({
|
|
76
|
+
title: entry.title,
|
|
77
|
+
description: fields.description || entry.excerpt || description,
|
|
78
|
+
canonicalUrl,
|
|
79
|
+
siteName,
|
|
80
|
+
type: entry.date ? 'article' : 'website',
|
|
81
|
+
...(entry.date ? { published: entry.date } : {}),
|
|
82
|
+
...(entry.updated ? { modified: entry.updated } : {}),
|
|
83
|
+
...(image ? { image } : {}),
|
|
84
|
+
...(fields.robots ? { robots: fields.robots } : {}),
|
|
85
|
+
...(fields.author ? { author: fields.author } : {}),
|
|
86
|
+
feeds,
|
|
87
|
+
});
|
|
88
|
+
return { entry, html: await render(entry.body, { stagger: true }), canonicalUrl, seo, newer, older };
|
|
58
89
|
}
|
|
59
90
|
|
|
60
91
|
/** The chronological archive for one concept: every non-draft summary, newest-first. */
|