@glw907/cairn-cms 0.8.0 → 0.10.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 +178 -0
- package/dist/components/ComponentForm.svelte.d.ts +20 -0
- package/dist/components/ComponentForm.svelte.d.ts.map +1 -0
- package/dist/components/ComponentInsertDialog.svelte +92 -0
- package/dist/components/ComponentInsertDialog.svelte.d.ts +20 -0
- package/dist/components/ComponentInsertDialog.svelte.d.ts.map +1 -0
- package/dist/components/EditPage.svelte +9 -8
- package/dist/components/EditPage.svelte.d.ts +4 -3
- package/dist/components/EditPage.svelte.d.ts.map +1 -1
- package/dist/components/EditorToolbar.svelte +61 -0
- package/dist/components/EditorToolbar.svelte.d.ts +15 -0
- package/dist/components/EditorToolbar.svelte.d.ts.map +1 -0
- package/dist/components/IconPicker.svelte +51 -0
- package/dist/components/IconPicker.svelte.d.ts +20 -0
- package/dist/components/IconPicker.svelte.d.ts.map +1 -0
- package/dist/components/MarkdownEditor.svelte +96 -57
- package/dist/components/MarkdownEditor.svelte.d.ts +5 -6
- package/dist/components/MarkdownEditor.svelte.d.ts.map +1 -1
- package/dist/components/index.d.ts +3 -1
- package/dist/components/index.d.ts.map +1 -1
- package/dist/components/index.js +3 -1
- package/dist/components/markdown-format.d.ts +13 -0
- package/dist/components/markdown-format.d.ts.map +1 -0
- package/dist/components/markdown-format.js +23 -0
- package/dist/content/compose.d.ts.map +1 -1
- package/dist/content/compose.js +1 -0
- package/dist/content/types.d.ts +5 -0
- package/dist/content/types.d.ts.map +1 -1
- package/dist/index.d.ts +8 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -1
- package/dist/render/component-grammar.d.ts +10 -0
- package/dist/render/component-grammar.d.ts.map +1 -0
- package/dist/render/component-grammar.js +140 -0
- package/dist/render/component-insert.d.ts +14 -0
- package/dist/render/component-insert.d.ts.map +1 -0
- package/dist/render/component-insert.js +9 -0
- package/dist/render/component-reference.d.ts +11 -0
- package/dist/render/component-reference.d.ts.map +1 -0
- package/dist/render/component-reference.js +34 -0
- package/dist/render/component-validate.d.ts +10 -0
- package/dist/render/component-validate.d.ts.map +1 -0
- package/dist/render/component-validate.js +30 -0
- package/dist/render/pipeline.d.ts +1 -1
- package/dist/render/pipeline.js +1 -1
- package/dist/render/registry.d.ts +45 -1
- package/dist/render/registry.d.ts.map +1 -1
- package/dist/render/registry.js +13 -0
- package/dist/render/sanitize.js +2 -2
- package/package.json +8 -3
- package/src/lib/components/ComponentForm.svelte +178 -0
- package/src/lib/components/ComponentInsertDialog.svelte +92 -0
- package/src/lib/components/EditPage.svelte +9 -8
- package/src/lib/components/EditorToolbar.svelte +61 -0
- package/src/lib/components/IconPicker.svelte +51 -0
- package/src/lib/components/MarkdownEditor.svelte +96 -57
- package/src/lib/components/index.ts +3 -1
- package/src/lib/components/markdown-format.ts +39 -0
- package/src/lib/content/compose.ts +1 -0
- package/src/lib/content/types.ts +5 -0
- package/src/lib/index.ts +16 -2
- package/src/lib/render/component-grammar.ts +167 -0
- package/src/lib/render/component-insert.ts +15 -0
- package/src/lib/render/component-reference.ts +38 -0
- package/src/lib/render/component-validate.ts +36 -0
- package/src/lib/render/pipeline.ts +1 -1
- package/src/lib/render/registry.ts +61 -1
- package/src/lib/render/sanitize.ts +2 -2
- package/dist/components/ComponentPalette.svelte +0 -50
- package/dist/components/ComponentPalette.svelte.d.ts +0 -16
- package/dist/components/ComponentPalette.svelte.d.ts.map +0 -1
- package/src/lib/components/ComponentPalette.svelte +0 -50
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
@component
|
|
3
|
+
A visual icon choice over the site's IconSet. Each glyph is a toggle button; the selected one carries
|
|
4
|
+
aria-pressed. When the field is optional, a None button clears the value. The glyph renders inline from
|
|
5
|
+
the IconSet path data, matching the renderer's 256-unit viewBox.
|
|
6
|
+
-->
|
|
7
|
+
<script lang="ts">
|
|
8
|
+
import type { IconSet } from '../render/glyph.js';
|
|
9
|
+
|
|
10
|
+
interface Props {
|
|
11
|
+
/** The site's glyph name to SVG path-data map. */
|
|
12
|
+
icons: IconSet;
|
|
13
|
+
/** The currently selected glyph name, or '' for none. */
|
|
14
|
+
value: string;
|
|
15
|
+
/** When false, a None choice is offered. */
|
|
16
|
+
required: boolean;
|
|
17
|
+
/** Called with the new glyph name (or '' for none). */
|
|
18
|
+
onChange: (name: string) => void;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
let { icons, value, required, onChange }: Props = $props();
|
|
22
|
+
|
|
23
|
+
const names = $derived(Object.keys(icons));
|
|
24
|
+
</script>
|
|
25
|
+
|
|
26
|
+
<div class="flex flex-wrap gap-2" role="group" aria-label="Icon">
|
|
27
|
+
{#if !required}
|
|
28
|
+
<button
|
|
29
|
+
type="button"
|
|
30
|
+
class="btn btn-sm"
|
|
31
|
+
class:btn-primary={value === ''}
|
|
32
|
+
aria-pressed={value === ''}
|
|
33
|
+
onclick={() => onChange('')}
|
|
34
|
+
>None</button>
|
|
35
|
+
{/if}
|
|
36
|
+
{#each names as name (name)}
|
|
37
|
+
<button
|
|
38
|
+
type="button"
|
|
39
|
+
class="btn btn-sm gap-1"
|
|
40
|
+
class:btn-primary={value === name}
|
|
41
|
+
aria-pressed={value === name}
|
|
42
|
+
aria-label={name}
|
|
43
|
+
onclick={() => onChange(name)}
|
|
44
|
+
>
|
|
45
|
+
<svg class="ec-glyph" viewBox="0 0 256 256" fill="currentColor" aria-hidden="true" width="16" height="16">
|
|
46
|
+
<path d={icons[name]} />
|
|
47
|
+
</svg>
|
|
48
|
+
<span class="text-xs">{name}</span>
|
|
49
|
+
</button>
|
|
50
|
+
{/each}
|
|
51
|
+
</div>
|
|
@@ -1,81 +1,120 @@
|
|
|
1
1
|
<!--
|
|
2
2
|
@component
|
|
3
|
-
The `MarkdownEditor` seam (spec §6, seam 5): a thin wrapper over
|
|
4
|
-
and a cursor-insert callback.
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
The `MarkdownEditor` seam (spec §6, seam 5): a thin wrapper over CodeMirror 6 exposing a bindable
|
|
4
|
+
value and a cursor-insert callback. CodeMirror is client-only, so it mounts after the component does
|
|
5
|
+
through a dynamic import; until then a plain textarea carries the value so the form still submits, and
|
|
6
|
+
the hidden field mirrors the value throughout. The edit surface owns its toolbar; the design-accurate
|
|
7
|
+
preview lives in EditPage through the adapter's render. Swapping the editor stays a one-file change.
|
|
7
8
|
-->
|
|
8
9
|
<script lang="ts">
|
|
9
|
-
import { onMount } from 'svelte';
|
|
10
|
+
import { onMount, onDestroy } from 'svelte';
|
|
11
|
+
import EditorToolbar from './EditorToolbar.svelte';
|
|
12
|
+
import { applyMarkdownFormat, type FormatKind } from './markdown-format.js';
|
|
10
13
|
|
|
11
14
|
interface Props {
|
|
12
15
|
/** The markdown source; bindable so the parent reads edits back. */
|
|
13
16
|
value: string;
|
|
14
17
|
/** The hidden field name the value is mirrored to for form submit. */
|
|
15
18
|
name: string;
|
|
16
|
-
/** Carta extensions from the adapter, for the design-accurate preview. */
|
|
17
|
-
plugins?: unknown[];
|
|
18
19
|
/** Receives a `(text) => void` that inserts at the cursor; the palette calls it. */
|
|
19
20
|
registerInsert?: (insert: (text: string) => void) => void;
|
|
20
21
|
}
|
|
21
22
|
|
|
22
|
-
let { value = $bindable(), name,
|
|
23
|
-
|
|
24
|
-
// Local structural type for the Carta editing surface this seam uses. carta-md re-exports its
|
|
25
|
-
// Svelte components from the package entry, so its `Carta` class is not reachable as a named
|
|
26
|
-
// export under NodeNext; a structural type stays compatible without naming it (the shape
|
|
27
|
-
// legacy/src/lib/editor.ts relied on, verified against carta-md@4.11).
|
|
28
|
-
interface CartaInput {
|
|
29
|
-
getSelection(): { start: number };
|
|
30
|
-
insertAt(position: number, text: string): void;
|
|
31
|
-
update(): boolean;
|
|
32
|
-
}
|
|
33
|
-
interface CartaLike {
|
|
34
|
-
input?: CartaInput;
|
|
35
|
-
}
|
|
23
|
+
let { value = $bindable(), name, registerInsert }: Props = $props();
|
|
36
24
|
|
|
25
|
+
let host = $state<HTMLDivElement | null>(null);
|
|
37
26
|
let mounted = $state(false);
|
|
38
|
-
//
|
|
39
|
-
//
|
|
40
|
-
//
|
|
41
|
-
|
|
42
|
-
let Editor = $state<(typeof import('carta-md'))['MarkdownEditor'] | null>(null);
|
|
43
|
-
let carta = $state<CartaLike | null>(null);
|
|
27
|
+
// The CodeMirror view, untyped at the runtime boundary because @codemirror/* loads only in the
|
|
28
|
+
// browser. The type-only `import(...)` annotation is erased; the value import is dynamic in onMount,
|
|
29
|
+
// so the server bundle never pulls CodeMirror (guarded by the editor-boundary test).
|
|
30
|
+
let view: import('@codemirror/view').EditorView | null = null;
|
|
44
31
|
|
|
45
32
|
onMount(async () => {
|
|
46
|
-
const
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
)
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
33
|
+
const viewMod = await import('@codemirror/view');
|
|
34
|
+
const stateMod = await import('@codemirror/state');
|
|
35
|
+
const markdownMod = await import('@codemirror/lang-markdown');
|
|
36
|
+
const commandsMod = await import('@codemirror/commands');
|
|
37
|
+
const languageMod = await import('@codemirror/language');
|
|
38
|
+
|
|
39
|
+
if (!host) return;
|
|
40
|
+
|
|
41
|
+
const { EditorView, keymap } = viewMod;
|
|
42
|
+
const theme = EditorView.theme(
|
|
43
|
+
{
|
|
44
|
+
'&': { backgroundColor: 'var(--color-base-100)', color: 'var(--color-base-content)', fontSize: '0.875rem' },
|
|
45
|
+
'.cm-content': { fontFamily: 'ui-monospace, monospace', padding: '0.75rem', lineHeight: '1.7' },
|
|
46
|
+
'.cm-cursor': { borderLeftColor: 'var(--color-primary)' },
|
|
47
|
+
'&.cm-focused': { outline: '2px solid var(--color-primary)', outlineOffset: '-2px' },
|
|
48
|
+
'.cm-line': { padding: '0' },
|
|
49
|
+
},
|
|
50
|
+
{ dark: false },
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
view = new EditorView({
|
|
54
|
+
parent: host,
|
|
55
|
+
state: stateMod.EditorState.create({
|
|
56
|
+
doc: value,
|
|
57
|
+
extensions: [
|
|
58
|
+
commandsMod.history(),
|
|
59
|
+
keymap.of([...commandsMod.defaultKeymap, ...commandsMod.historyKeymap]),
|
|
60
|
+
markdownMod.markdown(),
|
|
61
|
+
EditorView.lineWrapping,
|
|
62
|
+
languageMod.syntaxHighlighting(languageMod.defaultHighlightStyle, { fallback: true }),
|
|
63
|
+
theme,
|
|
64
|
+
EditorView.updateListener.of((update) => {
|
|
65
|
+
if (update.docChanged) value = update.state.doc.toString();
|
|
66
|
+
}),
|
|
67
|
+
],
|
|
68
|
+
}),
|
|
69
69
|
});
|
|
70
|
+
|
|
71
|
+
registerInsert?.(insertAtCursor);
|
|
70
72
|
mounted = true;
|
|
71
73
|
});
|
|
74
|
+
|
|
75
|
+
onDestroy(() => view?.destroy());
|
|
76
|
+
|
|
77
|
+
// Reconcile an externally reassigned `value` into the mounted editor. A no-op until `view` exists,
|
|
78
|
+
// and the doc-equality guard ignores the updateListener's own writes so the two never feed back.
|
|
79
|
+
$effect(() => {
|
|
80
|
+
const incoming = value;
|
|
81
|
+
if (!view) return;
|
|
82
|
+
const current = view.state.doc.toString();
|
|
83
|
+
if (incoming === current) return;
|
|
84
|
+
view.dispatch({ changes: { from: 0, to: current.length, insert: incoming } });
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
function insertAtCursor(text: string) {
|
|
88
|
+
if (!view) {
|
|
89
|
+
value = value ? `${value}\n\n${text}` : text;
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
const pos = view.state.selection.main.head;
|
|
93
|
+
const prefix = pos > 0 ? '\n\n' : '';
|
|
94
|
+
const insert = `${prefix}${text}`;
|
|
95
|
+
view.dispatch({ changes: { from: pos, insert }, selection: { anchor: pos + insert.length } });
|
|
96
|
+
view.focus();
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function applyFormat(kind: FormatKind) {
|
|
100
|
+
if (!view) return;
|
|
101
|
+
const { from, to } = view.state.selection.main;
|
|
102
|
+
const doc = view.state.doc.toString();
|
|
103
|
+
const next = applyMarkdownFormat(doc, from, to, kind);
|
|
104
|
+
view.dispatch({
|
|
105
|
+
changes: { from: 0, to: doc.length, insert: next.doc },
|
|
106
|
+
selection: { anchor: next.from, head: next.to },
|
|
107
|
+
});
|
|
108
|
+
view.focus();
|
|
109
|
+
}
|
|
72
110
|
</script>
|
|
73
111
|
|
|
74
|
-
<input type="hidden" {name}
|
|
112
|
+
<input type="hidden" {name} {value} />
|
|
75
113
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
<
|
|
79
|
-
{
|
|
80
|
-
|
|
81
|
-
{/if}
|
|
114
|
+
<div class="border-base-300 overflow-hidden rounded-box border">
|
|
115
|
+
<EditorToolbar format={applyFormat} />
|
|
116
|
+
<div bind:this={host}></div>
|
|
117
|
+
{#if !mounted}
|
|
118
|
+
<textarea class="textarea min-h-64 w-full font-mono text-sm" bind:value aria-label="Markdown source"></textarea>
|
|
119
|
+
{/if}
|
|
120
|
+
</div>
|
|
@@ -7,5 +7,7 @@ export { default as ConceptList } from './ConceptList.svelte';
|
|
|
7
7
|
export { default as EditPage } from './EditPage.svelte';
|
|
8
8
|
export { default as ManageEditors } from './ManageEditors.svelte';
|
|
9
9
|
export { default as MarkdownEditor } from './MarkdownEditor.svelte';
|
|
10
|
-
export { default as
|
|
10
|
+
export { default as ComponentInsertDialog } from './ComponentInsertDialog.svelte';
|
|
11
|
+
export { default as ComponentForm } from './ComponentForm.svelte';
|
|
12
|
+
export { default as IconPicker } from './IconPicker.svelte';
|
|
11
13
|
export { default as NavTree } from './NavTree.svelte';
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure markdown selection transforms for the editor toolbar. Each call maps a document and a
|
|
3
|
+
* selection range to a new document and a new selection, with no DOM. The MarkdownEditor view
|
|
4
|
+
* dispatches the result; keeping the logic here lets it unit-test without a browser.
|
|
5
|
+
*/
|
|
6
|
+
export type FormatKind = 'bold' | 'italic' | 'code' | 'heading' | 'quote' | 'ul' | 'link';
|
|
7
|
+
|
|
8
|
+
export interface FormatResult {
|
|
9
|
+
doc: string;
|
|
10
|
+
from: number;
|
|
11
|
+
to: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const WRAP: Record<'bold' | 'italic' | 'code', string> = { bold: '**', italic: '_', code: '`' };
|
|
15
|
+
const LINE_PREFIX: Record<'heading' | 'quote' | 'ul', string> = { heading: '# ', quote: '> ', ul: '- ' };
|
|
16
|
+
|
|
17
|
+
export function applyMarkdownFormat(doc: string, from: number, to: number, kind: FormatKind): FormatResult {
|
|
18
|
+
if (kind === 'bold' || kind === 'italic' || kind === 'code') {
|
|
19
|
+
const marker = WRAP[kind];
|
|
20
|
+
const next = doc.slice(0, from) + marker + doc.slice(from, to) + marker + doc.slice(to);
|
|
21
|
+
return { doc: next, from: from + marker.length, to: to + marker.length };
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (kind === 'link') {
|
|
25
|
+
const text = doc.slice(from, to);
|
|
26
|
+
const placeholder = 'url';
|
|
27
|
+
const lead = `[${text}](`; // everything before the url placeholder
|
|
28
|
+
const inserted = `${lead}${placeholder})`;
|
|
29
|
+
const urlStart = from + lead.length;
|
|
30
|
+
return { doc: doc.slice(0, from) + inserted + doc.slice(to), from: urlStart, to: urlStart + placeholder.length };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const prefix = LINE_PREFIX[kind];
|
|
34
|
+
const lineStart = doc.lastIndexOf('\n', from - 1) + 1; // 0 when the selection is on the first line
|
|
35
|
+
const region = doc.slice(lineStart, to);
|
|
36
|
+
const prefixed = region.replace(/^/gm, prefix);
|
|
37
|
+
const added = prefixed.length - region.length;
|
|
38
|
+
return { doc: doc.slice(0, lineStart) + prefixed + doc.slice(to), from: from + prefix.length, to: to + added };
|
|
39
|
+
}
|
package/src/lib/content/types.ts
CHANGED
|
@@ -8,6 +8,7 @@
|
|
|
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 { IconSet } from '../render/glyph.js';
|
|
11
12
|
import type { DatePrefix } from './ids.js';
|
|
12
13
|
|
|
13
14
|
/** Common to every frontmatter field: the frontmatter key, the form label, and whether it is required. */
|
|
@@ -151,6 +152,8 @@ export interface CairnAdapter {
|
|
|
151
152
|
render(md: string, opts?: { stagger?: boolean }): string | Promise<string>;
|
|
152
153
|
/** Directive component registry; the renderer and the future palette derive from it (seam 3). */
|
|
153
154
|
registry?: ComponentRegistry;
|
|
155
|
+
/** The site's glyph name to SVG path-data map, for the admin icon picker and the renderer. */
|
|
156
|
+
icons?: IconSet;
|
|
154
157
|
navMenu?: NavMenuConfig;
|
|
155
158
|
assets?: AssetConfig;
|
|
156
159
|
}
|
|
@@ -243,6 +246,8 @@ export interface CairnRuntime {
|
|
|
243
246
|
/** The site's one renderer: the editor preview and every public page call it (design decision 4). */
|
|
244
247
|
render(md: string, opts?: { stagger?: boolean }): string | Promise<string>;
|
|
245
248
|
registry?: ComponentRegistry;
|
|
249
|
+
/** The site's glyph name to SVG path-data map, for the admin icon picker and the renderer. */
|
|
250
|
+
icons?: IconSet;
|
|
246
251
|
navMenu?: NavMenuConfig;
|
|
247
252
|
assets?: AssetConfig;
|
|
248
253
|
/** Admin panels contributed by extensions (Mode 2). Empty until Plan 09 wires the dispatch route. */
|
package/src/lib/index.ts
CHANGED
|
@@ -48,8 +48,22 @@ export {
|
|
|
48
48
|
} from './content/ids.js';
|
|
49
49
|
export type { DatePrefix } from './content/ids.js';
|
|
50
50
|
// Render engine (Plan 04): generic directive pipeline; sites own the component registry.
|
|
51
|
-
export { defineRegistry } from './render/registry.js';
|
|
52
|
-
export type {
|
|
51
|
+
export { defineRegistry, emptyValues } from './render/registry.js';
|
|
52
|
+
export type {
|
|
53
|
+
ComponentDef,
|
|
54
|
+
ComponentRegistry,
|
|
55
|
+
FieldType,
|
|
56
|
+
AttributeField,
|
|
57
|
+
SlotKind,
|
|
58
|
+
SlotDef,
|
|
59
|
+
ComponentValues,
|
|
60
|
+
} from './render/registry.js';
|
|
61
|
+
export { serializeComponent, parseComponent } from './render/component-grammar.js';
|
|
62
|
+
export { validateComponent } from './render/component-validate.js';
|
|
63
|
+
export type { ComponentValidation } from './render/component-validate.js';
|
|
64
|
+
export { buildComponentInsert, type ComponentInsert } from './render/component-insert.js';
|
|
65
|
+
export { generateComponentReference } from './render/component-reference.js';
|
|
66
|
+
export type { ReferenceOptions } from './render/component-reference.js';
|
|
53
67
|
export { glyph } from './render/glyph.js';
|
|
54
68
|
export type { IconSet } from './render/glyph.js';
|
|
55
69
|
export { remarkDirectiveStamp } from './render/remark-directives.js';
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { unified } from 'unified';
|
|
2
|
+
import remarkParse from 'remark-parse';
|
|
3
|
+
import remarkDirective from 'remark-directive';
|
|
4
|
+
import remarkStringify from 'remark-stringify';
|
|
5
|
+
import type { Root, RootContent } from 'mdast';
|
|
6
|
+
import type { ComponentDef, ComponentValues, SlotDef } from './registry.js';
|
|
7
|
+
|
|
8
|
+
const COLON = ':';
|
|
9
|
+
|
|
10
|
+
function attrBlock(def: ComponentDef, values: ComponentValues): string {
|
|
11
|
+
const parts: string[] = [];
|
|
12
|
+
for (const field of def.attributes ?? []) {
|
|
13
|
+
const v = values.attributes[field.key];
|
|
14
|
+
if (field.type === 'boolean') {
|
|
15
|
+
if (v === true) parts.push(`${field.key}="true"`);
|
|
16
|
+
} else if (typeof v === 'string' && v !== '') {
|
|
17
|
+
// The directive attribute grammar (mdast-util-directive) treats a literal `"` as the value
|
|
18
|
+
// terminator and decodes HTML entities, so a backslash escape does not survive a round-trip.
|
|
19
|
+
// Encode `&` first (so existing entities are not double-decoded) then `"`; the parser decodes
|
|
20
|
+
// both back. A backslash is literal in this grammar and needs no escaping.
|
|
21
|
+
const escaped = v.replace(/&/g, '&').replace(/"/g, '"');
|
|
22
|
+
parts.push(`${field.key}="${escaped}"`);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return parts.length ? `{${parts.join(' ')}}` : '';
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function slotByName(def: ComponentDef, name: string): SlotDef | undefined {
|
|
29
|
+
return (def.slots ?? []).find((s) => s.name === name);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function nestedSlots(def: ComponentDef): SlotDef[] {
|
|
33
|
+
return (def.slots ?? []).filter((s) => s.name !== 'title' && s.name !== 'body');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function serializeComponent(def: ComponentDef, values: ComponentValues): string {
|
|
37
|
+
const fence = COLON.repeat(nestedSlots(def).length > 0 ? 4 : 3);
|
|
38
|
+
|
|
39
|
+
const title = slotByName(def, 'title') ? (values.slots.title as string) ?? '' : '';
|
|
40
|
+
// Escape brackets in the label so a `[` or `]` in the title does not break the directive label
|
|
41
|
+
// grammar; remark un-escapes them back to literal text on parse, so readLabel recovers them.
|
|
42
|
+
const label = title ? `[${title.replace(/\[/g, '\\[').replace(/\]/g, '\\]')}]` : '';
|
|
43
|
+
|
|
44
|
+
const open = `${fence}${def.name}${label}${attrBlock(def, values)}`;
|
|
45
|
+
|
|
46
|
+
const lines: string[] = [open];
|
|
47
|
+
const body = slotByName(def, 'body') ? (values.slots.body as string) ?? '' : '';
|
|
48
|
+
if (body) lines.push(body);
|
|
49
|
+
|
|
50
|
+
for (const slot of nestedSlots(def)) {
|
|
51
|
+
const raw = values.slots[slot.name];
|
|
52
|
+
const content =
|
|
53
|
+
slot.kind === 'repeatable'
|
|
54
|
+
? (Array.isArray(raw) ? raw : []).filter((i) => i !== '').map((i) => `- ${i}`).join('\n')
|
|
55
|
+
: ((raw as string | undefined) ?? '');
|
|
56
|
+
if (!content) continue;
|
|
57
|
+
if (lines.length > 1) lines.push(''); // blank line before this block
|
|
58
|
+
lines.push(`${COLON.repeat(3)}${slot.name}`, content, COLON.repeat(3));
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
lines.push(fence);
|
|
62
|
+
return lines.join('\n');
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// A minimal structural view of a mdast containerDirective node (mdast-util-directive shape).
|
|
66
|
+
interface DirectiveNode {
|
|
67
|
+
type: 'containerDirective' | 'leafDirective' | 'textDirective';
|
|
68
|
+
name: string;
|
|
69
|
+
attributes?: Record<string, string | null> | null;
|
|
70
|
+
children: RootContent[];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function isContainer(node: RootContent): node is RootContent & DirectiveNode {
|
|
74
|
+
return (node as DirectiveNode).type === 'containerDirective';
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Pin the bullet to `-` so a markdown body or slot that uses dash bullets round-trips unchanged
|
|
78
|
+
// rather than drifting to remark-stringify's default `*`, which would silently mutate author content.
|
|
79
|
+
const toMd = unified().use(remarkStringify, { bullet: '-' });
|
|
80
|
+
|
|
81
|
+
/** Render mdast children back to trimmed markdown text. */
|
|
82
|
+
function childrenToText(children: RootContent[]): string {
|
|
83
|
+
const root: Root = { type: 'root', children };
|
|
84
|
+
return String(toMd.stringify(root)).trim();
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Parse a serialized component directive back into guided-form values, the inverse of
|
|
88
|
+
* {@link serializeComponent}. The grammar is reversible, so the editor can round-trip a
|
|
89
|
+
* saved directive through the form. */
|
|
90
|
+
export async function parseComponent(markdown: string, def: ComponentDef): Promise<ComponentValues> {
|
|
91
|
+
const tree = unified().use(remarkParse).use(remarkDirective).parse(markdown) as Root;
|
|
92
|
+
const root = tree.children.find(
|
|
93
|
+
(c): c is RootContent & DirectiveNode => isContainer(c) && (c as DirectiveNode).name === def.name,
|
|
94
|
+
);
|
|
95
|
+
const values = emptyComponentValues(def);
|
|
96
|
+
if (!root) return values;
|
|
97
|
+
|
|
98
|
+
for (const field of def.attributes ?? []) {
|
|
99
|
+
const raw = root.attributes?.[field.key];
|
|
100
|
+
if (field.type === 'boolean') values.attributes[field.key] = raw === 'true';
|
|
101
|
+
else if (typeof raw === 'string') values.attributes[field.key] = raw;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const titleSlot = slotByName(def, 'title');
|
|
105
|
+
const bodySlot = slotByName(def, 'body');
|
|
106
|
+
const nested = nestedSlots(def);
|
|
107
|
+
const nestedNames = new Set(nested.map((s) => s.name));
|
|
108
|
+
|
|
109
|
+
const directChildren = root.children.filter(
|
|
110
|
+
(c) => !(isContainer(c) && nestedNames.has((c as DirectiveNode).name)) && !isDirectiveLabel(c),
|
|
111
|
+
);
|
|
112
|
+
const nestedChildren = root.children.filter(
|
|
113
|
+
(c): c is RootContent & DirectiveNode => isContainer(c) && nestedNames.has((c as DirectiveNode).name),
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
if (titleSlot) values.slots.title = readLabel(root) ?? '';
|
|
117
|
+
if (bodySlot) values.slots.body = childrenToText(directChildren);
|
|
118
|
+
|
|
119
|
+
for (const slot of nested) {
|
|
120
|
+
const node = nestedChildren.find((c) => c.name === slot.name);
|
|
121
|
+
if (!node) continue;
|
|
122
|
+
if (slot.kind === 'repeatable') values.slots[slot.name] = readListItems(node.children);
|
|
123
|
+
else values.slots[slot.name] = childrenToText(node.children);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return values;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** The raw attribute keys present on the component's opening directive, read from the parsed tree
|
|
130
|
+
* (quote-aware, unlike a regex over the source). Used by validation to flag unknown keys. */
|
|
131
|
+
export function parseRawAttributeKeys(markdown: string, def: ComponentDef): string[] {
|
|
132
|
+
const tree = unified().use(remarkParse).use(remarkDirective).parse(markdown) as Root;
|
|
133
|
+
const root = tree.children.find(
|
|
134
|
+
(c): c is RootContent & DirectiveNode => isContainer(c) && (c as DirectiveNode).name === def.name,
|
|
135
|
+
);
|
|
136
|
+
return Object.keys(root?.attributes ?? {});
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// A bare parse base: empty strings, false, and empty lists, with no attribute defaults applied. The
|
|
140
|
+
// `emptyValues` helper in registry.ts seeds form defaults instead, so it is deliberately not reused
|
|
141
|
+
// here; the parse must overwrite only the fields actually present in the markdown.
|
|
142
|
+
function emptyComponentValues(def: ComponentDef): ComponentValues {
|
|
143
|
+
const attributes: Record<string, string | boolean> = {};
|
|
144
|
+
for (const f of def.attributes ?? []) attributes[f.key] = f.type === 'boolean' ? false : '';
|
|
145
|
+
const slots: Record<string, string | string[]> = {};
|
|
146
|
+
for (const s of def.slots ?? []) slots[s.name] = s.kind === 'repeatable' ? [] : '';
|
|
147
|
+
return { attributes, slots };
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// mdast-util-directive carries the `[label]` as a paragraph whose `data.directiveLabel` is set.
|
|
151
|
+
function isDirectiveLabel(node: RootContent): boolean {
|
|
152
|
+
return Boolean((node as { data?: { directiveLabel?: boolean } }).data?.directiveLabel);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function readLabel(root: DirectiveNode): string | undefined {
|
|
156
|
+
for (const child of root.children) {
|
|
157
|
+
const p = child as { type: string; data?: { directiveLabel?: boolean }; children?: { value?: string }[] };
|
|
158
|
+
if (p.type === 'paragraph' && p.data?.directiveLabel) return (p.children ?? []).map((c) => c.value ?? '').join('');
|
|
159
|
+
}
|
|
160
|
+
return undefined;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function readListItems(children: RootContent[]): string[] {
|
|
164
|
+
const list = children.find((c) => (c as { type: string }).type === 'list') as { children?: RootContent[] } | undefined;
|
|
165
|
+
if (!list?.children) return [];
|
|
166
|
+
return list.children.map((li) => childrenToText((li as { children?: RootContent[] }).children ?? []));
|
|
167
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { serializeComponent } from './component-grammar.js';
|
|
2
|
+
import { validateComponent } from './component-validate.js';
|
|
3
|
+
import type { ComponentDef, ComponentValues } from './registry.js';
|
|
4
|
+
|
|
5
|
+
/** The outcome of preparing a guided-form component for insertion: the markdown to insert, or the
|
|
6
|
+
* field-keyed errors to show on the form. */
|
|
7
|
+
export type ComponentInsert = { ok: true; markdown: string } | { ok: false; errors: Record<string, string> };
|
|
8
|
+
|
|
9
|
+
/** Serialize a component's form values, then validate the result against its schema. Returns the
|
|
10
|
+
* markdown to insert at the cursor, or the field errors keyed by attribute key or slot name. */
|
|
11
|
+
export async function buildComponentInsert(def: ComponentDef, values: ComponentValues): Promise<ComponentInsert> {
|
|
12
|
+
const markdown = serializeComponent(def, values);
|
|
13
|
+
const verdict = await validateComponent(markdown, def);
|
|
14
|
+
return verdict.ok ? { ok: true, markdown } : { ok: false, errors: verdict.errors };
|
|
15
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { serializeComponent } from './component-grammar.js';
|
|
2
|
+
import { emptyValues, type ComponentDef, type ComponentRegistry, type ComponentValues } from './registry.js';
|
|
3
|
+
|
|
4
|
+
export interface ReferenceOptions {
|
|
5
|
+
/** The H1 title of the reference document. */
|
|
6
|
+
title: string;
|
|
7
|
+
/** The one-line blockquote summary under the title. */
|
|
8
|
+
summary: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** Build a self-contained markdown reference (the llms-full.txt shape) for a component registry, for
|
|
12
|
+
* authors and for pointing an LLM at one curated file. */
|
|
13
|
+
export function generateComponentReference(registry: ComponentRegistry, opts: ReferenceOptions): string {
|
|
14
|
+
const sections = registry.defs.map((def) => componentSection(def));
|
|
15
|
+
return `# ${opts.title}\n\n> ${opts.summary}\n\n${sections.join('\n\n')}\n`;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function componentSection(def: ComponentDef): string {
|
|
19
|
+
const lines = [`## ${def.label} (\`:::${def.name}\`)`, '', def.description ?? ''];
|
|
20
|
+
if (def.use) lines.push('', `**When to use:** ${def.use}`);
|
|
21
|
+
lines.push('', '```', serializeComponent(def, exampleValues(def)), '```');
|
|
22
|
+
return lines.join('\n');
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Seed example values that show every declared field: an ellipsis for strings, one sample list item. */
|
|
26
|
+
function exampleValues(def: ComponentDef): ComponentValues {
|
|
27
|
+
const values = emptyValues(def);
|
|
28
|
+
for (const field of def.attributes ?? []) {
|
|
29
|
+
if (field.type === 'boolean') values.attributes[field.key] = false;
|
|
30
|
+
else values.attributes[field.key] = field.options?.[0] ?? '…';
|
|
31
|
+
}
|
|
32
|
+
for (const slot of def.slots ?? []) {
|
|
33
|
+
if (slot.kind === 'repeatable') values.slots[slot.name] = ['…'];
|
|
34
|
+
else if (slot.name === 'title') values.slots[slot.name] = 'Title';
|
|
35
|
+
else values.slots[slot.name] = '…';
|
|
36
|
+
}
|
|
37
|
+
return values;
|
|
38
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { parseComponent, parseRawAttributeKeys } from './component-grammar.js';
|
|
2
|
+
import type { ComponentDef } from './registry.js';
|
|
3
|
+
|
|
4
|
+
/** A validation verdict: ok, or field-keyed error messages. */
|
|
5
|
+
export type ComponentValidation = { ok: true } | { ok: false; errors: Record<string, string> };
|
|
6
|
+
|
|
7
|
+
export async function validateComponent(markdown: string, def: ComponentDef): Promise<ComponentValidation> {
|
|
8
|
+
const values = await parseComponent(markdown, def);
|
|
9
|
+
const errors: Record<string, string> = {};
|
|
10
|
+
const declared = new Set((def.attributes ?? []).map((f) => f.key));
|
|
11
|
+
|
|
12
|
+
for (const field of def.attributes ?? []) {
|
|
13
|
+
const v = values.attributes[field.key];
|
|
14
|
+
const filled = field.type === 'boolean' ? true : typeof v === 'string' && v !== '';
|
|
15
|
+
if (field.required && !filled) {
|
|
16
|
+
errors[field.key] = `${field.label} is required.`;
|
|
17
|
+
continue;
|
|
18
|
+
}
|
|
19
|
+
if (field.type === 'select' && typeof v === 'string' && v !== '' && !(field.options ?? []).includes(v)) {
|
|
20
|
+
errors[field.key] = `${field.label} must be one of: ${(field.options ?? []).join(', ')}.`;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
for (const key of parseRawAttributeKeys(markdown, def)) {
|
|
25
|
+
if (!declared.has(key)) errors[key] = `Unknown attribute "${key}".`;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
for (const slot of def.slots ?? []) {
|
|
29
|
+
if (!slot.required) continue;
|
|
30
|
+
const v = values.slots[slot.name];
|
|
31
|
+
const filled = Array.isArray(v) ? v.length > 0 : typeof v === 'string' && v !== '';
|
|
32
|
+
if (!filled) errors[slot.name] = `${slot.label} is required.`;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return Object.keys(errors).length ? { ok: false, errors } : { ok: true };
|
|
36
|
+
}
|
|
@@ -19,7 +19,7 @@ export interface RendererOptions {
|
|
|
19
19
|
|
|
20
20
|
/** Compose a site's render pipeline from its component registry: directive syntax to
|
|
21
21
|
* stamped markers to registry-built hast. Returns `renderMarkdown` plus the remark/
|
|
22
|
-
* rehype plugin arrays (so the
|
|
22
|
+
* rehype plugin arrays (so the admin editor preview can reuse the exact same set). */
|
|
23
23
|
export function createRenderer(registry: ComponentRegistry, options: RendererOptions = {}) {
|
|
24
24
|
const remarkPlugins: PluggableList = [remarkDirective, [remarkDirectiveStamp, registry]];
|
|
25
25
|
const rehypePlugins: PluggableList = [rehypeRaw, [rehypeDispatch, registry, options.stagger], rehypeSlug];
|