@glw907/cairn-cms 0.6.0 → 0.8.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/ConceptList.svelte +8 -4
- package/dist/components/ConceptList.svelte.d.ts.map +1 -1
- package/dist/components/EditPage.svelte +5 -5
- package/dist/components/EditPage.svelte.d.ts +3 -1
- package/dist/components/EditPage.svelte.d.ts.map +1 -1
- package/dist/content/compose.d.ts +2 -2
- package/dist/content/compose.d.ts.map +1 -1
- package/dist/content/compose.js +3 -3
- package/dist/content/concepts.d.ts +7 -6
- package/dist/content/concepts.d.ts.map +1 -1
- package/dist/content/concepts.js +13 -5
- package/dist/content/ids.d.ts +14 -0
- package/dist/content/ids.d.ts.map +1 -1
- package/dist/content/ids.js +40 -0
- package/dist/content/permalink.d.ts +12 -0
- package/dist/content/permalink.d.ts.map +1 -0
- package/dist/content/permalink.js +30 -0
- package/dist/content/types.d.ts +23 -3
- package/dist/content/types.d.ts.map +1 -1
- package/dist/delivery/content-index.d.ts +47 -0
- package/dist/delivery/content-index.d.ts.map +1 -0
- package/dist/delivery/content-index.js +86 -0
- package/dist/delivery/excerpt.d.ts +11 -0
- package/dist/delivery/excerpt.d.ts.map +1 -0
- package/dist/delivery/excerpt.js +38 -0
- package/dist/delivery/feeds.d.ts +27 -0
- package/dist/delivery/feeds.d.ts.map +1 -0
- package/dist/delivery/feeds.js +80 -0
- package/dist/delivery/paginate.d.ts +13 -0
- package/dist/delivery/paginate.d.ts.map +1 -0
- package/dist/delivery/paginate.js +20 -0
- package/dist/delivery/robots.d.ts +6 -0
- package/dist/delivery/robots.d.ts.map +1 -0
- package/dist/delivery/robots.js +10 -0
- package/dist/delivery/seo.d.ts +34 -0
- package/dist/delivery/seo.d.ts.map +1 -0
- package/dist/delivery/seo.js +46 -0
- package/dist/delivery/site-index.d.ts +28 -0
- package/dist/delivery/site-index.d.ts.map +1 -0
- package/dist/delivery/site-index.js +38 -0
- package/dist/delivery/sitemap.d.ts +8 -0
- package/dist/delivery/sitemap.d.ts.map +1 -0
- package/dist/delivery/sitemap.js +21 -0
- package/dist/index.d.ts +19 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +14 -2
- package/dist/nav/site-config.d.ts +5 -0
- package/dist/nav/site-config.d.ts.map +1 -1
- package/dist/nav/site-config.js +4 -0
- package/dist/sveltekit/content-routes.d.ts.map +1 -1
- package/dist/sveltekit/content-routes.js +18 -8
- package/dist/sveltekit/index.d.ts +2 -0
- package/dist/sveltekit/index.d.ts.map +1 -1
- package/dist/sveltekit/index.js +1 -0
- package/dist/sveltekit/public-routes.d.ts +50 -0
- package/dist/sveltekit/public-routes.d.ts.map +1 -0
- package/dist/sveltekit/public-routes.js +45 -0
- package/package.json +1 -1
- package/src/lib/components/ConceptList.svelte +8 -4
- package/src/lib/components/EditPage.svelte +5 -5
- package/src/lib/content/compose.ts +4 -3
- package/src/lib/content/concepts.ts +15 -5
- package/src/lib/content/ids.ts +44 -0
- package/src/lib/content/permalink.ts +40 -0
- package/src/lib/content/types.ts +21 -4
- package/src/lib/delivery/content-index.ts +130 -0
- package/src/lib/delivery/excerpt.ts +41 -0
- package/src/lib/delivery/feeds.ts +112 -0
- package/src/lib/delivery/paginate.ts +32 -0
- package/src/lib/delivery/robots.ts +10 -0
- package/src/lib/delivery/seo.ts +72 -0
- package/src/lib/delivery/site-index.ts +68 -0
- package/src/lib/delivery/sitemap.ts +29 -0
- package/src/lib/index.ts +35 -1
- package/src/lib/nav/site-config.ts +8 -0
- package/src/lib/sveltekit/content-routes.ts +17 -7
- package/src/lib/sveltekit/index.ts +8 -0
- package/src/lib/sveltekit/public-routes.ts +83 -0
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// cairn-cms: public route loaders (dated-slug design). The factory closes over the site-level
|
|
2
|
+
// index, the runtime render, and the origin. entryLoad and entries are site-wide: one catch-all
|
|
3
|
+
// `[...path]` route resolves any concept by request path through `byPermalink`. The archive, tag,
|
|
4
|
+
// and tag-index loaders stay concept-scoped, keyed by concept id. The index is built in site code
|
|
5
|
+
// from globs, so it stays in the prerender graph and out of the runtime Worker.
|
|
6
|
+
import { error } from '@sveltejs/kit';
|
|
7
|
+
/** Build the public loaders for a site's unified index. */
|
|
8
|
+
export function createPublicRoutes(deps) {
|
|
9
|
+
const { site, render, origin } = deps;
|
|
10
|
+
/** Resolve one concept's index by id, or a 404 (the route names an unconfigured concept). */
|
|
11
|
+
function indexOf(conceptId) {
|
|
12
|
+
const index = site.concept(conceptId);
|
|
13
|
+
if (!index)
|
|
14
|
+
throw error(404, `Unknown content type: ${conceptId}`);
|
|
15
|
+
return index;
|
|
16
|
+
}
|
|
17
|
+
/** One entry by request path, rendered through the site renderer, or a 404. */
|
|
18
|
+
async function entryLoad(event) {
|
|
19
|
+
const entry = site.byPermalink(event.url.pathname);
|
|
20
|
+
if (!entry)
|
|
21
|
+
throw error(404, `Not found: ${event.url.pathname}`);
|
|
22
|
+
const { newer, older } = site.adjacent(entry);
|
|
23
|
+
return { entry, html: await render(entry.body, { stagger: true }), canonicalUrl: origin + entry.permalink, newer, older };
|
|
24
|
+
}
|
|
25
|
+
/** The chronological archive for one concept: every non-draft summary, newest-first. */
|
|
26
|
+
function archiveLoad(conceptId) {
|
|
27
|
+
return { entries: indexOf(conceptId).all() };
|
|
28
|
+
}
|
|
29
|
+
/** All tags with counts for one concept, for a tag index page. */
|
|
30
|
+
function tagIndexLoad(conceptId) {
|
|
31
|
+
return { tags: indexOf(conceptId).allTags() };
|
|
32
|
+
}
|
|
33
|
+
/** One tag's entries for one concept, or a 404 when the tag has none. */
|
|
34
|
+
function tagLoad(conceptId, event) {
|
|
35
|
+
const entries = indexOf(conceptId).byTag(event.params.tag);
|
|
36
|
+
if (entries.length === 0)
|
|
37
|
+
throw error(404, `No entries tagged "${event.params.tag}"`);
|
|
38
|
+
return { tag: event.params.tag, entries };
|
|
39
|
+
}
|
|
40
|
+
/** Prerender enumeration: one `{ path }` per entry across every concept. */
|
|
41
|
+
function entries() {
|
|
42
|
+
return site.entries();
|
|
43
|
+
}
|
|
44
|
+
return { entryLoad, archiveLoad, tagIndexLoad, tagLoad, entries };
|
|
45
|
+
}
|
package/package.json
CHANGED
|
@@ -17,9 +17,14 @@ plus a new-entry form. The slug auto-derives from the title until the author edi
|
|
|
17
17
|
let title = $state('');
|
|
18
18
|
let slug = $state('');
|
|
19
19
|
let slugEdited = $state(false);
|
|
20
|
+
// Default the date client-side so the SSR pass and hydration agree across UTC midnight.
|
|
21
|
+
let dateDefault = $state('');
|
|
22
|
+
$effect(() => {
|
|
23
|
+
dateDefault = new Date().toISOString().slice(0, 10);
|
|
24
|
+
});
|
|
20
25
|
|
|
21
26
|
const derivedSlug = $derived(slugEdited ? slug : slugify(title));
|
|
22
|
-
const slugPlaceholder = $derived(data.dated ? '
|
|
27
|
+
const slugPlaceholder = $derived(data.dated ? 'my-entry' : 'about-us');
|
|
23
28
|
</script>
|
|
24
29
|
|
|
25
30
|
<header class="mb-4 flex items-center justify-between">
|
|
@@ -58,14 +63,13 @@ plus a new-entry form. The slug auto-derives from the title until the author edi
|
|
|
58
63
|
<h2 class="text-sm font-semibold">New entry</h2>
|
|
59
64
|
<label class="flex flex-col gap-1">
|
|
60
65
|
<span class="text-sm font-medium">Title</span>
|
|
61
|
-
<input class="input" name="title"
|
|
66
|
+
<input class="input" name="title" bind:value={title} required />
|
|
62
67
|
</label>
|
|
63
68
|
<label class="flex flex-col gap-1">
|
|
64
69
|
<span class="text-sm font-medium">Slug</span>
|
|
65
70
|
<input
|
|
66
71
|
class="input"
|
|
67
72
|
name="slug"
|
|
68
|
-
aria-label="Slug"
|
|
69
73
|
placeholder={slugPlaceholder}
|
|
70
74
|
value={derivedSlug}
|
|
71
75
|
oninput={(e) => { slugEdited = true; slug = e.currentTarget.value; }}
|
|
@@ -74,7 +78,7 @@ plus a new-entry form. The slug auto-derives from the title until the author edi
|
|
|
74
78
|
{#if data.dated}
|
|
75
79
|
<label class="flex flex-col gap-1">
|
|
76
80
|
<span class="text-sm font-medium">Date</span>
|
|
77
|
-
<input class="input" type="date" name="date"
|
|
81
|
+
<input class="input" type="date" name="date" value={dateDefault} />
|
|
78
82
|
</label>
|
|
79
83
|
{/if}
|
|
80
84
|
<button type="submit" class="btn btn-primary self-start">Create</button>
|
|
@@ -21,10 +21,10 @@ markdown editor and a live, design-accurate preview. The whole surface is one fo
|
|
|
21
21
|
/** Carta preview plugins from the adapter, for the design-accurate preview. */
|
|
22
22
|
preview?: unknown[];
|
|
23
23
|
/** The site's design-accurate render pipeline; the preview pane sanitizes its output. */
|
|
24
|
-
|
|
24
|
+
render?: (md: string, opts?: { stagger?: boolean }) => string | Promise<string>;
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
let { data, registry, preview = [],
|
|
27
|
+
let { data, registry, preview = [], render }: Props = $props();
|
|
28
28
|
|
|
29
29
|
// `body` is local editor state seeded once from the prop; it diverges as the user types.
|
|
30
30
|
// untrack() captures the initial value without subscribing to future prop changes.
|
|
@@ -48,15 +48,15 @@ markdown editor and a live, design-accurate preview. The whole surface is one fo
|
|
|
48
48
|
// Render the design-accurate preview as the body changes, debounced, and sanitize before the DOM.
|
|
49
49
|
// The sanitize is the one barrier between editor-authored markdown and the page (Carta is unsanitized).
|
|
50
50
|
// previewRun is a plain counter (not reactive state) used as a latest-wins guard: if a slow earlier
|
|
51
|
-
// async
|
|
51
|
+
// async render call resolves after a newer one has started, the stale result is discarded.
|
|
52
52
|
let previewRun = 0;
|
|
53
53
|
$effect(() => {
|
|
54
|
-
if (!showPreview || !
|
|
54
|
+
if (!showPreview || !render) return;
|
|
55
55
|
const md = body;
|
|
56
56
|
const run = ++previewRun;
|
|
57
57
|
const handle = setTimeout(async () => {
|
|
58
58
|
try {
|
|
59
|
-
const html = await
|
|
59
|
+
const html = await render(md);
|
|
60
60
|
const safe = await sanitizePreviewHtml(html);
|
|
61
61
|
if (run === previewRun) previewHtml = safe;
|
|
62
62
|
} catch {
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
// the same way and contributes the same kinds of things: nav entries, route logic,
|
|
4
4
|
// concepts, components, field types, and save hooks. Shaped now so the extension contract
|
|
5
5
|
// is additive later.
|
|
6
|
-
import type { AdminPanel, CairnAdapter, CairnExtension, CairnRuntime, ConceptConfig, FieldTypeDef } from './types.js';
|
|
6
|
+
import type { AdminPanel, CairnAdapter, CairnExtension, CairnRuntime, ConceptConfig, ConceptUrlPolicy, FieldTypeDef } from './types.js';
|
|
7
7
|
import { normalizeConcepts } from './concepts.js';
|
|
8
8
|
|
|
9
9
|
/**
|
|
@@ -13,6 +13,7 @@ import { normalizeConcepts } from './concepts.js';
|
|
|
13
13
|
export function composeRuntime(
|
|
14
14
|
adapter: CairnAdapter,
|
|
15
15
|
extensions: CairnExtension[] = [],
|
|
16
|
+
urlPolicy: Record<string, ConceptUrlPolicy | undefined> = {},
|
|
16
17
|
): CairnRuntime {
|
|
17
18
|
const content: Record<string, ConceptConfig | undefined> = { ...adapter.content };
|
|
18
19
|
const adminPanels: AdminPanel[] = [];
|
|
@@ -26,10 +27,10 @@ export function composeRuntime(
|
|
|
26
27
|
}
|
|
27
28
|
return {
|
|
28
29
|
siteName: adapter.siteName,
|
|
29
|
-
concepts: normalizeConcepts(content),
|
|
30
|
+
concepts: normalizeConcepts(content, urlPolicy),
|
|
30
31
|
backend: adapter.backend,
|
|
31
32
|
sender: adapter.sender,
|
|
32
|
-
|
|
33
|
+
render: adapter.render,
|
|
33
34
|
registry: adapter.registry,
|
|
34
35
|
navMenu: adapter.navMenu,
|
|
35
36
|
assets: adapter.assets,
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
// (id, label, directory, concept-fixed routing, fields, validator) the admin reads. A
|
|
4
4
|
// future Fragments concept attaches by adding one key under `content` and one routing
|
|
5
5
|
// entry, with no reshape here.
|
|
6
|
-
import type { ConceptConfig, ConceptDescriptor, RoutingRule } from './types.js';
|
|
6
|
+
import type { ConceptConfig, ConceptDescriptor, ConceptUrlPolicy, RoutingRule } from './types.js';
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* Concept-fixed routing, keyed by concept id (spec §7.2). Posts are dated feed entries;
|
|
@@ -23,24 +23,34 @@ function defaultLabel(id: string): string {
|
|
|
23
23
|
return id.charAt(0).toUpperCase() + id.slice(1);
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
+
/** The default permalink pattern: Pages live at the root, other concepts under their id. */
|
|
27
|
+
function defaultPermalink(id: string): string {
|
|
28
|
+
return id === 'pages' ? '/:slug' : `/${id}/:slug`;
|
|
29
|
+
}
|
|
30
|
+
|
|
26
31
|
/**
|
|
27
|
-
* Normalize an adapter's declared concepts into uniform descriptors (seam 1).
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
32
|
+
* Normalize an adapter's declared concepts into uniform descriptors (seam 1). URL policy
|
|
33
|
+
* (`permalink`, `datePrefix`) comes from the YAML site-config, passed here as `urlPolicy` keyed by
|
|
34
|
+
* concept id; each value defaults when the YAML omits it (`/:slug` for Pages, `/<id>/:slug`
|
|
35
|
+
* otherwise; `datePrefix` defaults to `day`). `routing` is injectable so a contract test can prove
|
|
36
|
+
* a new concept attaches additively; production passes the default `CONCEPT_ROUTING`.
|
|
31
37
|
*/
|
|
32
38
|
export function normalizeConcepts(
|
|
33
39
|
content: Record<string, ConceptConfig | undefined>,
|
|
40
|
+
urlPolicy: Record<string, ConceptUrlPolicy | undefined> = {},
|
|
34
41
|
routing: Readonly<Record<string, RoutingRule>> = CONCEPT_ROUTING,
|
|
35
42
|
): ConceptDescriptor[] {
|
|
36
43
|
const descriptors: ConceptDescriptor[] = [];
|
|
37
44
|
for (const [id, config] of Object.entries(content)) {
|
|
38
45
|
if (!config) continue;
|
|
46
|
+
const policy = urlPolicy[id] ?? {};
|
|
39
47
|
descriptors.push({
|
|
40
48
|
id,
|
|
41
49
|
label: config.label ?? defaultLabel(id),
|
|
42
50
|
dir: config.dir,
|
|
43
51
|
routing: routing[id] ?? DEFAULT_ROUTING,
|
|
52
|
+
permalink: policy.permalink ?? defaultPermalink(id),
|
|
53
|
+
datePrefix: policy.datePrefix ?? 'day',
|
|
44
54
|
fields: config.fields,
|
|
45
55
|
validate: config.validate,
|
|
46
56
|
});
|
package/src/lib/content/ids.ts
CHANGED
|
@@ -36,3 +36,47 @@ export function slugify(title: string): string {
|
|
|
36
36
|
.replace(/[^a-z0-9]+/g, '-')
|
|
37
37
|
.replace(/^-+|-+$/g, '');
|
|
38
38
|
}
|
|
39
|
+
|
|
40
|
+
/** Filename date-prefix granularity for a dated concept: the leading `YYYY[-MM[-DD]]-` on the stem. */
|
|
41
|
+
export type DatePrefix = 'year' | 'month' | 'day';
|
|
42
|
+
|
|
43
|
+
/** The leading date-prefix shape for each granularity. */
|
|
44
|
+
const DATE_PREFIX_RE: Record<DatePrefix, RegExp> = {
|
|
45
|
+
year: /^\d{4}-/,
|
|
46
|
+
month: /^\d{4}-\d{2}-/,
|
|
47
|
+
day: /^\d{4}-\d{2}-\d{2}-/,
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* The URL slug for an id. A dated concept passes its `datePrefix` and the leading date prefix is
|
|
52
|
+
* stripped when present; a non-dated concept passes `null` and the id is returned verbatim. Only
|
|
53
|
+
* the leading prefix is removed, so a year-like tail (a post titled "2024 Recap") stays in the slug.
|
|
54
|
+
*/
|
|
55
|
+
export function slugFromId(id: string, datePrefix: DatePrefix | null): string {
|
|
56
|
+
if (!datePrefix) return id;
|
|
57
|
+
return id.replace(DATE_PREFIX_RE[datePrefix], '');
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Compose a dated entry's id from a `YYYY-MM-DD` date, a date-free slug, and the concept's
|
|
62
|
+
* granularity: the date truncated to the granularity, a hyphen, then the slug. Throws on a
|
|
63
|
+
* malformed date so a bad create fails before touching git.
|
|
64
|
+
*/
|
|
65
|
+
export function composeDatedId(date: string, slug: string, datePrefix: DatePrefix): string {
|
|
66
|
+
const m = date.match(/^(\d{4})-(\d{2})-(\d{2})$/);
|
|
67
|
+
if (!m) throw new Error(`composeDatedId: malformed date "${date}"`);
|
|
68
|
+
const [, year, month, day] = m;
|
|
69
|
+
let prefix: string;
|
|
70
|
+
switch (datePrefix) {
|
|
71
|
+
case 'year':
|
|
72
|
+
prefix = year;
|
|
73
|
+
break;
|
|
74
|
+
case 'month':
|
|
75
|
+
prefix = `${year}-${month}`;
|
|
76
|
+
break;
|
|
77
|
+
case 'day':
|
|
78
|
+
prefix = `${year}-${month}-${day}`;
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
return `${prefix}-${slug}`;
|
|
82
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// cairn-cms: the one permalink resolver (public-delivery design, decision 3). Feeds, the
|
|
2
|
+
// sitemap, canonical links, list links, and the prerender entries() all call this, so an
|
|
3
|
+
// entry has exactly one canonical URL. The date is read straight from the YYYY-MM-DD string,
|
|
4
|
+
// so a permalink never shifts across a timezone.
|
|
5
|
+
import type { ConceptDescriptor } from './types.js';
|
|
6
|
+
|
|
7
|
+
function pad(n: number): string {
|
|
8
|
+
return String(n).padStart(2, '0');
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function dateParts(date?: string): { year: string; month: string; day: string } | null {
|
|
12
|
+
const match = date?.match(/^(\d{4})-(\d{2})-(\d{2})/);
|
|
13
|
+
return match ? { year: match[1], month: match[2], day: match[3] } : null;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Resolve an entry's canonical path from its concept's permalink pattern. Throws when the
|
|
18
|
+
* pattern uses a date token and the entry has no valid date, or when a token is unknown, so
|
|
19
|
+
* a misconfiguration fails at build rather than emitting a broken path.
|
|
20
|
+
*/
|
|
21
|
+
export function permalink(
|
|
22
|
+
descriptor: ConceptDescriptor,
|
|
23
|
+
entry: { id: string; slug: string; date?: string },
|
|
24
|
+
): string {
|
|
25
|
+
return descriptor.permalink.replace(/:(\w+)/g, (_match, token: string) => {
|
|
26
|
+
if (token === 'slug') return entry.slug;
|
|
27
|
+
if (token === 'year' || token === 'month' || token === 'day') {
|
|
28
|
+
const parts = dateParts(entry.date);
|
|
29
|
+
if (!parts) {
|
|
30
|
+
throw new Error(
|
|
31
|
+
`permalink: concept "${descriptor.id}" pattern uses :${token}, but entry "${entry.id}" has no valid date`,
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
if (token === 'year') return parts.year;
|
|
35
|
+
if (token === 'month') return pad(Number(parts.month));
|
|
36
|
+
return pad(Number(parts.day));
|
|
37
|
+
}
|
|
38
|
+
throw new Error(`permalink: unknown token :${token} in pattern "${descriptor.permalink}"`);
|
|
39
|
+
});
|
|
40
|
+
}
|
package/src/lib/content/types.ts
CHANGED
|
@@ -3,11 +3,12 @@
|
|
|
3
3
|
//
|
|
4
4
|
// The adapter is the single seam the engine consumes (spec §8). A site supplies a
|
|
5
5
|
// `CairnAdapter` at `src/lib/cairn.config.ts` declaring its backend repo, the content
|
|
6
|
-
// concepts it enables, its magic-link sender, and a design-accurate `
|
|
6
|
+
// concepts it enables, its magic-link sender, and a design-accurate `render`. The
|
|
7
7
|
// engine never hard-codes a concept, directory, or field; it reads them here. Field
|
|
8
8
|
// descriptors are plain data so a `load` function can hand them across the server-to-client
|
|
9
9
|
// boundary to the editor form.
|
|
10
10
|
import type { ComponentRegistry } from '../render/registry.js';
|
|
11
|
+
import type { DatePrefix } from './ids.js';
|
|
11
12
|
|
|
12
13
|
/** Common to every frontmatter field: the frontmatter key, the form label, and whether it is required. */
|
|
13
14
|
interface FieldBase {
|
|
@@ -86,6 +87,17 @@ export interface ConceptConfig {
|
|
|
86
87
|
validate(frontmatter: Record<string, unknown>, body: string): ValidationResult;
|
|
87
88
|
}
|
|
88
89
|
|
|
90
|
+
/**
|
|
91
|
+
* A concept's URL policy, set per concept in the YAML site-config (not the adapter). `permalink` is
|
|
92
|
+
* a `/`-prefixed pattern of literal segments and the tokens `:slug`, `:year`, `:month`, `:day`.
|
|
93
|
+
* `datePrefix` is the filename date-prefix granularity for a dated concept. Both default in
|
|
94
|
+
* `normalizeConcepts` when omitted.
|
|
95
|
+
*/
|
|
96
|
+
export interface ConceptUrlPolicy {
|
|
97
|
+
permalink?: string;
|
|
98
|
+
datePrefix?: DatePrefix;
|
|
99
|
+
}
|
|
100
|
+
|
|
89
101
|
/** The GitHub App backend a site reads from and commits to (spec §8). Plain data the GitHub engine (Plan 03) consumes. */
|
|
90
102
|
export interface BackendConfig {
|
|
91
103
|
owner: string;
|
|
@@ -135,8 +147,8 @@ export interface CairnAdapter {
|
|
|
135
147
|
};
|
|
136
148
|
backend: BackendConfig;
|
|
137
149
|
sender: SenderConfig;
|
|
138
|
-
/**
|
|
139
|
-
|
|
150
|
+
/** The site's one renderer: the editor preview and every public page call it (design decision 4). */
|
|
151
|
+
render(md: string, opts?: { stagger?: boolean }): string | Promise<string>;
|
|
140
152
|
/** Directive component registry; the renderer and the future palette derive from it (seam 3). */
|
|
141
153
|
registry?: ComponentRegistry;
|
|
142
154
|
navMenu?: NavMenuConfig;
|
|
@@ -166,6 +178,10 @@ export interface ConceptDescriptor {
|
|
|
166
178
|
label: string;
|
|
167
179
|
dir: string;
|
|
168
180
|
routing: RoutingRule;
|
|
181
|
+
/** The resolved permalink pattern, defaulted by `normalizeConcepts`. */
|
|
182
|
+
permalink: string;
|
|
183
|
+
/** Filename date-prefix granularity for a dated concept; resolved by `normalizeConcepts`. */
|
|
184
|
+
datePrefix: DatePrefix;
|
|
169
185
|
fields: FrontmatterField[];
|
|
170
186
|
validate(frontmatter: Record<string, unknown>, body: string): ValidationResult;
|
|
171
187
|
}
|
|
@@ -224,7 +240,8 @@ export interface CairnRuntime {
|
|
|
224
240
|
concepts: ConceptDescriptor[];
|
|
225
241
|
backend: BackendConfig;
|
|
226
242
|
sender: SenderConfig;
|
|
227
|
-
|
|
243
|
+
/** The site's one renderer: the editor preview and every public page call it (design decision 4). */
|
|
244
|
+
render(md: string, opts?: { stagger?: boolean }): string | Promise<string>;
|
|
228
245
|
registry?: ComponentRegistry;
|
|
229
246
|
navMenu?: NavMenuConfig;
|
|
230
247
|
assets?: AssetConfig;
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
// cairn-cms: the per-concept content index (public-delivery design, decisions 1 and 5). It
|
|
2
|
+
// takes raw files from a site's glob, parses them with the engine's own parseMarkdown, and
|
|
3
|
+
// returns cheap plain-data summaries plus an on-demand detail lookup. It is concept-generic:
|
|
4
|
+
// every operation reads the descriptor and its routing rule, never a hardcoded concept id.
|
|
5
|
+
import { parseMarkdown } from '../content/frontmatter.js';
|
|
6
|
+
import { idFromFilename, slugFromId } from '../content/ids.js';
|
|
7
|
+
import { permalink } from '../content/permalink.js';
|
|
8
|
+
import { deriveExcerpt, wordCount } from './excerpt.js';
|
|
9
|
+
import type { ConceptDescriptor } from '../content/types.js';
|
|
10
|
+
|
|
11
|
+
/** A raw content file before parsing: the glob key and the file's full markdown text. */
|
|
12
|
+
export interface RawFile {
|
|
13
|
+
path: string;
|
|
14
|
+
raw: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** The cheap, plain-data view of one entry, for lists, feeds, and the sitemap. */
|
|
18
|
+
export interface ContentSummary {
|
|
19
|
+
id: string;
|
|
20
|
+
slug: string;
|
|
21
|
+
permalink: string;
|
|
22
|
+
title: string;
|
|
23
|
+
date?: string;
|
|
24
|
+
updated?: string;
|
|
25
|
+
tags: string[];
|
|
26
|
+
excerpt: string;
|
|
27
|
+
wordCount: number;
|
|
28
|
+
draft: boolean;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** The detail view: a summary plus the frontmatter and the body to render. */
|
|
32
|
+
export interface ContentEntry extends ContentSummary {
|
|
33
|
+
frontmatter: Record<string, unknown>;
|
|
34
|
+
body: string;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** The per-concept query surface. */
|
|
38
|
+
export interface ContentIndex {
|
|
39
|
+
all(opts?: { includeDrafts?: boolean }): ContentSummary[];
|
|
40
|
+
byId(id: string): ContentEntry | undefined;
|
|
41
|
+
byTag(tag: string, opts?: { includeDrafts?: boolean }): ContentSummary[];
|
|
42
|
+
allTags(): { tag: string; count: number }[];
|
|
43
|
+
adjacent(id: string): { newer?: ContentSummary; older?: ContentSummary };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Map a Vite eager `?raw` glob record (`{ path: raw }`) to `RawFile[]`. */
|
|
47
|
+
export function fromGlob(record: Record<string, string>): RawFile[] {
|
|
48
|
+
return Object.entries(record).map(([path, raw]) => ({ path, raw }));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function basename(path: string): string {
|
|
52
|
+
const slash = path.lastIndexOf('/');
|
|
53
|
+
return slash >= 0 ? path.slice(slash + 1) : path;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function asString(value: unknown): string | undefined {
|
|
57
|
+
return typeof value === 'string' && value.trim() ? value : undefined;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function asDate(value: unknown): string | undefined {
|
|
61
|
+
if (value instanceof Date) return Number.isNaN(value.getTime()) ? undefined : value.toISOString().slice(0, 10);
|
|
62
|
+
if (typeof value === 'string') return value.match(/^\d{4}-\d{2}-\d{2}/)?.[0];
|
|
63
|
+
return undefined;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function asTags(value: unknown): string[] {
|
|
67
|
+
return Array.isArray(value) ? value.map(String) : [];
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Build a concept's index from its raw files and normalized descriptor. */
|
|
71
|
+
export function createContentIndex(files: RawFile[], descriptor: ConceptDescriptor): ContentIndex {
|
|
72
|
+
const entries: ContentEntry[] = files.map((file) => {
|
|
73
|
+
const id = idFromFilename(basename(file.path));
|
|
74
|
+
const slug = slugFromId(id, descriptor.routing.dated ? descriptor.datePrefix : null);
|
|
75
|
+
const { frontmatter, body } = parseMarkdown(file.raw);
|
|
76
|
+
const date = asDate(frontmatter.date);
|
|
77
|
+
return {
|
|
78
|
+
id,
|
|
79
|
+
slug,
|
|
80
|
+
permalink: permalink(descriptor, { id, slug, date }),
|
|
81
|
+
title: asString(frontmatter.title) ?? id,
|
|
82
|
+
date,
|
|
83
|
+
updated: asDate(frontmatter.updated),
|
|
84
|
+
tags: asTags(frontmatter.tags),
|
|
85
|
+
excerpt: deriveExcerpt(body, { description: asString(frontmatter.description) }),
|
|
86
|
+
wordCount: wordCount(body),
|
|
87
|
+
draft: frontmatter.draft === true,
|
|
88
|
+
frontmatter,
|
|
89
|
+
body,
|
|
90
|
+
};
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
// Dated concepts sort newest-first; undated concepts (Pages) sort by title.
|
|
94
|
+
const sorted = [...entries].sort((a, b) =>
|
|
95
|
+
descriptor.routing.dated ? (b.date ?? '').localeCompare(a.date ?? '') : a.title.localeCompare(b.title),
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
const summarize = (entry: ContentEntry): ContentSummary => {
|
|
99
|
+
const { frontmatter: _frontmatter, body: _body, ...summary } = entry;
|
|
100
|
+
return summary;
|
|
101
|
+
};
|
|
102
|
+
const visible = (list: ContentEntry[], includeDrafts?: boolean): ContentEntry[] =>
|
|
103
|
+
includeDrafts ? list : list.filter((entry) => !entry.draft);
|
|
104
|
+
|
|
105
|
+
return {
|
|
106
|
+
all: (opts = {}) => visible(sorted, opts.includeDrafts).map(summarize),
|
|
107
|
+
byId: (id) => entries.find((entry) => entry.id === id),
|
|
108
|
+
byTag: (tag, opts = {}) =>
|
|
109
|
+
visible(sorted, opts.includeDrafts)
|
|
110
|
+
.filter((entry) => entry.tags.includes(tag))
|
|
111
|
+
.map(summarize),
|
|
112
|
+
allTags: () => {
|
|
113
|
+
const counts = new Map<string, number>();
|
|
114
|
+
for (const entry of sorted) {
|
|
115
|
+
if (entry.draft) continue;
|
|
116
|
+
for (const tag of entry.tags) counts.set(tag, (counts.get(tag) ?? 0) + 1);
|
|
117
|
+
}
|
|
118
|
+
return [...counts].map(([tag, count]) => ({ tag, count })).sort((a, b) => a.tag.localeCompare(b.tag));
|
|
119
|
+
},
|
|
120
|
+
adjacent: (id) => {
|
|
121
|
+
const list = visible(sorted, false);
|
|
122
|
+
const i = list.findIndex((entry) => entry.id === id);
|
|
123
|
+
if (i < 0) return {};
|
|
124
|
+
return {
|
|
125
|
+
newer: i > 0 ? summarize(list[i - 1]) : undefined,
|
|
126
|
+
older: i < list.length - 1 ? summarize(list[i + 1]) : undefined,
|
|
127
|
+
};
|
|
128
|
+
},
|
|
129
|
+
};
|
|
130
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// cairn-cms: excerpt and word count for content summaries (public-delivery design, decision
|
|
2
|
+
// 5). A light markdown strip keeps summaries cheap, so a list card, an og:description, and a
|
|
3
|
+
// summary-mode feed read one derived excerpt without a full render.
|
|
4
|
+
|
|
5
|
+
/** Reduce markdown to readable plain text: drop fenced code, images, and markup; unwrap inline
|
|
6
|
+
* code and links to their text; collapse whitespace. */
|
|
7
|
+
function toPlainText(md: string): string {
|
|
8
|
+
return md
|
|
9
|
+
.replace(/```[\s\S]*?```/g, ' ')
|
|
10
|
+
.replace(/`([^`]*)`/g, '$1')
|
|
11
|
+
.replace(/!\[[^\]]*\]\([^)]*\)/g, ' ')
|
|
12
|
+
.replace(/\[([^\]]*)\]\([^)]*\)/g, '$1')
|
|
13
|
+
.replace(/^\s{0,3}[#>]+\s*/gm, ' ')
|
|
14
|
+
.replace(/^\s{0,3}[-*+]\s+/gm, ' ')
|
|
15
|
+
.replace(/[*_~]/g, '')
|
|
16
|
+
.replace(/\s+/g, ' ')
|
|
17
|
+
.trim();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* A plain-text excerpt. Returns a trimmed frontmatter `description` when present, else the
|
|
22
|
+
* stripped body cut at a word boundary near `maxChars` (default 200) with an ellipsis.
|
|
23
|
+
*/
|
|
24
|
+
export function deriveExcerpt(body: string, opts: { description?: string; maxChars?: number } = {}): string {
|
|
25
|
+
const description = opts.description?.trim();
|
|
26
|
+
if (description) return description;
|
|
27
|
+
|
|
28
|
+
const max = opts.maxChars ?? 200;
|
|
29
|
+
const text = toPlainText(body);
|
|
30
|
+
if (text.length <= max) return text;
|
|
31
|
+
|
|
32
|
+
const cut = text.slice(0, max);
|
|
33
|
+
const lastSpace = cut.lastIndexOf(' ');
|
|
34
|
+
return `${(lastSpace > 0 ? cut.slice(0, lastSpace) : cut).trimEnd()}…`;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Count words in the stripped body. */
|
|
38
|
+
export function wordCount(body: string): number {
|
|
39
|
+
const text = toPlainText(body);
|
|
40
|
+
return text ? text.split(/\s+/).length : 0;
|
|
41
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
// cairn-cms: RSS and JSON Feed builders (public-delivery design). Pure functions over a
|
|
2
|
+
// channel and a list of items, so they unit-test without a render or a network. The caller
|
|
3
|
+
// (a template +server.ts shim) assembles items from the content index and passes absolute
|
|
4
|
+
// URLs built from PUBLIC_ORIGIN.
|
|
5
|
+
|
|
6
|
+
/** Feed channel metadata. URLs are absolute. */
|
|
7
|
+
export interface FeedChannel {
|
|
8
|
+
title: string;
|
|
9
|
+
description: string;
|
|
10
|
+
siteUrl: string;
|
|
11
|
+
feedUrl: string;
|
|
12
|
+
language?: string;
|
|
13
|
+
author?: { name: string; email?: string };
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** One feed entry. `contentHtml` carries the rendered body for a full-content feed. */
|
|
17
|
+
export interface FeedItem {
|
|
18
|
+
title: string;
|
|
19
|
+
url: string;
|
|
20
|
+
date: string;
|
|
21
|
+
updated?: string;
|
|
22
|
+
summary: string;
|
|
23
|
+
contentHtml?: string;
|
|
24
|
+
tags?: string[];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function escapeXml(value: string): string {
|
|
28
|
+
return value
|
|
29
|
+
.replace(/&/g, '&')
|
|
30
|
+
.replace(/</g, '<')
|
|
31
|
+
.replace(/>/g, '>')
|
|
32
|
+
.replace(/"/g, '"');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Make a string safe inside a CDATA section by splitting any `]]>` across two sections. */
|
|
36
|
+
function cdataSafe(value: string): string {
|
|
37
|
+
return value.replace(/]]>/g, ']]]]><![CDATA[>');
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Format a YYYY-MM-DD (or ISO) string as an RFC-822 date in UTC, as RSS wants. */
|
|
41
|
+
function rfc822(date: string): string {
|
|
42
|
+
return new Date(`${date.slice(0, 10)}T00:00:00.000Z`).toUTCString();
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Format a YYYY-MM-DD (or ISO) string as an ISO-8601 instant in UTC. */
|
|
46
|
+
function iso(date: string): string {
|
|
47
|
+
return new Date(`${date.slice(0, 10)}T00:00:00.000Z`).toISOString();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Build an RSS 2.0 document. */
|
|
51
|
+
export function buildRssFeed(channel: FeedChannel, items: FeedItem[]): string {
|
|
52
|
+
const entries = items
|
|
53
|
+
.map((item) => {
|
|
54
|
+
const content = item.contentHtml ?? item.summary;
|
|
55
|
+
return [
|
|
56
|
+
' <item>',
|
|
57
|
+
` <title>${escapeXml(item.title)}</title>`,
|
|
58
|
+
` <link>${escapeXml(item.url)}</link>`,
|
|
59
|
+
` <guid isPermaLink="true">${escapeXml(item.url)}</guid>`,
|
|
60
|
+
` <pubDate>${rfc822(item.date)}</pubDate>`,
|
|
61
|
+
` <description>${escapeXml(item.summary)}</description>`,
|
|
62
|
+
// CDATA cannot contain `]]>`, so split that one sequence rather than escape the body.
|
|
63
|
+
` <content:encoded><![CDATA[${cdataSafe(content)}]]></content:encoded>`,
|
|
64
|
+
' </item>',
|
|
65
|
+
].join('\n');
|
|
66
|
+
})
|
|
67
|
+
.join('\n');
|
|
68
|
+
|
|
69
|
+
return [
|
|
70
|
+
'<?xml version="1.0" encoding="UTF-8"?>',
|
|
71
|
+
'<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom">',
|
|
72
|
+
' <channel>',
|
|
73
|
+
` <title>${escapeXml(channel.title)}</title>`,
|
|
74
|
+
` <link>${escapeXml(channel.siteUrl)}</link>`,
|
|
75
|
+
` <description>${escapeXml(channel.description)}</description>`,
|
|
76
|
+
channel.language ? ` <language>${escapeXml(channel.language)}</language>` : '',
|
|
77
|
+
` <atom:link href="${escapeXml(channel.feedUrl)}" rel="self" type="application/rss+xml" />`,
|
|
78
|
+
entries,
|
|
79
|
+
' </channel>',
|
|
80
|
+
'</rss>',
|
|
81
|
+
'',
|
|
82
|
+
]
|
|
83
|
+
.filter((line) => line !== '')
|
|
84
|
+
.join('\n');
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Build a JSON Feed 1.1 document. */
|
|
88
|
+
export function buildJsonFeed(channel: FeedChannel, items: FeedItem[]): string {
|
|
89
|
+
return JSON.stringify(
|
|
90
|
+
{
|
|
91
|
+
version: 'https://jsonfeed.org/version/1.1',
|
|
92
|
+
title: channel.title,
|
|
93
|
+
description: channel.description,
|
|
94
|
+
home_page_url: channel.siteUrl,
|
|
95
|
+
feed_url: channel.feedUrl,
|
|
96
|
+
...(channel.language ? { language: channel.language } : {}),
|
|
97
|
+
...(channel.author ? { authors: [channel.author] } : {}),
|
|
98
|
+
items: items.map((item) => ({
|
|
99
|
+
id: item.url,
|
|
100
|
+
url: item.url,
|
|
101
|
+
title: item.title,
|
|
102
|
+
summary: item.summary,
|
|
103
|
+
date_published: iso(item.date),
|
|
104
|
+
...(item.updated ? { date_modified: iso(item.updated) } : {}),
|
|
105
|
+
...(item.contentHtml ? { content_html: item.contentHtml } : { content_text: item.summary }),
|
|
106
|
+
...(item.tags && item.tags.length ? { tags: item.tags } : {}),
|
|
107
|
+
})),
|
|
108
|
+
},
|
|
109
|
+
null,
|
|
110
|
+
2,
|
|
111
|
+
);
|
|
112
|
+
}
|