@cancia/astro 0.0.2 → 0.1.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/chunk-BOIQNZAO.js +64 -0
- package/dist/{chunk-KL2YMBFV.js → chunk-IIGDU5SV.js} +1 -1
- package/dist/{chunk-RKHJNQ6R.js → chunk-MCHQV6Y7.js} +62 -1
- package/dist/endpoints/schemas.js +3 -2
- package/dist/index.d.ts +1 -0
- package/dist/index.js +3 -2
- package/dist/portable-text-BikSqS9T.d.ts +117 -0
- package/dist/richtext/CanciaRichText.astro +44 -0
- package/dist/richtext/Link.astro +25 -0
- package/dist/richtext/index.d.ts +23 -0
- package/dist/richtext/index.js +142 -0
- package/dist/schema/index.d.ts +100 -2
- package/dist/schema/index.js +17 -1
- package/package.json +8 -2
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// src/schema/portable-text.ts
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
var PT_STYLES = ["normal", "h2", "h3", "blockquote"];
|
|
4
|
+
var PT_LIST_ITEMS = ["bullet", "number"];
|
|
5
|
+
var PT_DECORATORS = ["strong", "em"];
|
|
6
|
+
function isSafeHref(href) {
|
|
7
|
+
if (typeof href !== "string") return false;
|
|
8
|
+
const trimmed = href.trim();
|
|
9
|
+
if (trimmed === "") return false;
|
|
10
|
+
if (/^(https?:|mailto:|tel:)/i.test(trimmed)) return true;
|
|
11
|
+
const schemeMatch = /^([a-z][a-z0-9+.-]*):/i.exec(trimmed);
|
|
12
|
+
if (schemeMatch) {
|
|
13
|
+
const beforeColon = trimmed.slice(0, schemeMatch[1].length);
|
|
14
|
+
const firstSep = trimmed.search(/[/?#]/);
|
|
15
|
+
const colonIndex = beforeColon.length;
|
|
16
|
+
if (firstSep === -1 || colonIndex < firstSep) return false;
|
|
17
|
+
}
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
var ptLinkMarkDefSchema = z.object({
|
|
21
|
+
_type: z.literal("link"),
|
|
22
|
+
_key: z.string(),
|
|
23
|
+
href: z.string().refine(isSafeHref, "unsafe or unsupported URL scheme")
|
|
24
|
+
});
|
|
25
|
+
var ptSpanSchema = z.object({
|
|
26
|
+
_type: z.literal("span"),
|
|
27
|
+
_key: z.string(),
|
|
28
|
+
text: z.string(),
|
|
29
|
+
marks: z.array(z.string())
|
|
30
|
+
});
|
|
31
|
+
var ptBlockSchema = z.object({
|
|
32
|
+
_type: z.literal("block"),
|
|
33
|
+
_key: z.string(),
|
|
34
|
+
style: z.enum(PT_STYLES),
|
|
35
|
+
listItem: z.enum(PT_LIST_ITEMS).optional(),
|
|
36
|
+
level: z.number().int().positive().optional(),
|
|
37
|
+
markDefs: z.array(ptLinkMarkDefSchema).default([]),
|
|
38
|
+
children: z.array(ptSpanSchema)
|
|
39
|
+
}).superRefine((block, ctx) => {
|
|
40
|
+
const defKeys = new Set(block.markDefs.map((d) => d._key));
|
|
41
|
+
const decorators = new Set(PT_DECORATORS);
|
|
42
|
+
for (const span of block.children) {
|
|
43
|
+
for (const mark of span.marks) {
|
|
44
|
+
if (decorators.has(mark)) continue;
|
|
45
|
+
if (!defKeys.has(mark)) {
|
|
46
|
+
ctx.addIssue({
|
|
47
|
+
code: "custom",
|
|
48
|
+
message: `Mark "${mark}" is not an allowed decorator and does not reference a link markDef`,
|
|
49
|
+
path: ["children"]
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
var portableTextSubsetSchema = z.array(ptBlockSchema);
|
|
56
|
+
|
|
57
|
+
export {
|
|
58
|
+
PT_STYLES,
|
|
59
|
+
PT_LIST_ITEMS,
|
|
60
|
+
PT_DECORATORS,
|
|
61
|
+
isSafeHref,
|
|
62
|
+
ptBlockSchema,
|
|
63
|
+
portableTextSubsetSchema
|
|
64
|
+
};
|
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
import {
|
|
2
|
+
portableTextSubsetSchema
|
|
3
|
+
} from "./chunk-BOIQNZAO.js";
|
|
4
|
+
|
|
1
5
|
// src/schema/index.ts
|
|
2
6
|
import { z } from "zod";
|
|
3
7
|
function defineList(opts) {
|
|
@@ -26,13 +30,56 @@ var defineField = {
|
|
|
26
30
|
},
|
|
27
31
|
slug: (o) => z.string().regex(/^[a-z0-9-]+$/).meta({ widget: "slug", ...o }),
|
|
28
32
|
image: (o) => z.string().url().meta({ widget: "image", ...o }),
|
|
29
|
-
select: (o) => z.enum(o.options).meta({ widget: "select", ...o })
|
|
33
|
+
select: (o) => z.enum(o.options).meta({ widget: "select", ...o }),
|
|
34
|
+
/**
|
|
35
|
+
* A repeatable list of a single member type. `member` is any Zod type,
|
|
36
|
+
* typically another `defineField.*` (so it keeps its own `.meta({ widget })`
|
|
37
|
+
* and recursion carries labels/widgets into each row).
|
|
38
|
+
*
|
|
39
|
+
* bullets: f.array(f.text({ label: "Point" }), { label: "Key points" }),
|
|
40
|
+
* faqs: f.array(f.object({ q: f.text(), a: f.textarea() })),
|
|
41
|
+
*/
|
|
42
|
+
array: (member, o) => z.array(member).meta({ widget: "array", ...o }),
|
|
43
|
+
/**
|
|
44
|
+
* A nested group of named sub-fields, rendered as a collapsible fieldset.
|
|
45
|
+
*
|
|
46
|
+
* socials: f.object({ linkedin: f.url(), twitter: f.url() }, { label: "Socials" }),
|
|
47
|
+
*/
|
|
48
|
+
object: (shape, o) => z.object(shape).meta({ widget: "object", ...o }),
|
|
49
|
+
/**
|
|
50
|
+
* A pointer to an entry in another list. Stored as a plain string — the
|
|
51
|
+
* target entry's id. Per the v0.1.0 design (D5) there is NO integrity
|
|
52
|
+
* backend: dangling ids are tolerated and surfaced by the editor, never
|
|
53
|
+
* prevented. `list` is the target list's name (a key in the schemas module).
|
|
54
|
+
*
|
|
55
|
+
* author: f.reference({ list: "people", label: "Author" }),
|
|
56
|
+
* related: f.array(f.reference({ list: "posts" }), { label: "Related" }),
|
|
57
|
+
*/
|
|
58
|
+
reference: (o) => z.string().meta({ widget: "reference", ...o }),
|
|
59
|
+
/**
|
|
60
|
+
* A constrained rich-text body. Stored as a Portable-Text SUBSET array (D4):
|
|
61
|
+
* block styles normal/h2/h3/blockquote, bullet/number lists, strong/em marks,
|
|
62
|
+
* and link annotations — nothing else. The toolbar renders a structured block
|
|
63
|
+
* editor (approach B) over it, and `<CanciaRichText>` renders it to HTML.
|
|
64
|
+
*
|
|
65
|
+
* body: f.richtext({ label: "Body" }),
|
|
66
|
+
*
|
|
67
|
+
* The JSON-Schema type of this field is "array"; the explicit
|
|
68
|
+
* `widget: "richtext"` meta is what distinguishes it from a plain array in
|
|
69
|
+
* describeProperty.
|
|
70
|
+
*/
|
|
71
|
+
richtext: (o) => portableTextSubsetSchema.meta({ widget: "richtext", ...o })
|
|
30
72
|
};
|
|
73
|
+
function slugify(input) {
|
|
74
|
+
return input.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/-+/g, "-").replace(/^-+|-+$/g, "");
|
|
75
|
+
}
|
|
31
76
|
function humanise(name) {
|
|
32
77
|
return name.replace(/([A-Z])/g, " $1").replace(/[_-]/g, " ").replace(/^\w/, (c) => c.toUpperCase()).trim();
|
|
33
78
|
}
|
|
34
79
|
function inferWidget(prop, label) {
|
|
35
80
|
if (prop.widget) return prop.widget;
|
|
81
|
+
if (prop.type === "array") return "array";
|
|
82
|
+
if (prop.type === "object") return "object";
|
|
36
83
|
if (prop.enum) return "select";
|
|
37
84
|
if (prop.type === "boolean") return "checkbox";
|
|
38
85
|
if (prop.type === "number" || prop.type === "integer") return "number";
|
|
@@ -64,6 +111,19 @@ function describeProperty(name, prop, required) {
|
|
|
64
111
|
else if (prop.options) desc.options = prop.options;
|
|
65
112
|
if (prop.pattern !== void 0) desc.pattern = prop.pattern;
|
|
66
113
|
if (prop.source !== void 0) desc.source = prop.source;
|
|
114
|
+
if (desc.widget === "reference" && prop.list !== void 0) desc.referenceList = prop.list;
|
|
115
|
+
if (desc.widget === "richtext") {
|
|
116
|
+
return desc;
|
|
117
|
+
}
|
|
118
|
+
if (desc.widget === "array" && prop.items) {
|
|
119
|
+
desc.of = describeProperty("", prop.items, true);
|
|
120
|
+
}
|
|
121
|
+
if (desc.widget === "object" && prop.properties) {
|
|
122
|
+
const subRequired = new Set(prop.required ?? []);
|
|
123
|
+
desc.fields = Object.keys(prop.properties).map(
|
|
124
|
+
(subName) => describeProperty(subName, prop.properties[subName], subRequired.has(subName))
|
|
125
|
+
);
|
|
126
|
+
}
|
|
67
127
|
return desc;
|
|
68
128
|
}
|
|
69
129
|
function describeList(name, schema) {
|
|
@@ -90,5 +150,6 @@ export {
|
|
|
90
150
|
z,
|
|
91
151
|
defineList,
|
|
92
152
|
defineField,
|
|
153
|
+
slugify,
|
|
93
154
|
describeList
|
|
94
155
|
};
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
2
|
makeSchemasRoute
|
|
3
|
-
} from "../chunk-
|
|
3
|
+
} from "../chunk-IIGDU5SV.js";
|
|
4
4
|
import "../chunk-NG5GJME5.js";
|
|
5
|
-
import "../chunk-
|
|
5
|
+
import "../chunk-MCHQV6Y7.js";
|
|
6
|
+
import "../chunk-BOIQNZAO.js";
|
|
6
7
|
|
|
7
8
|
// src/endpoints/schemas.ts
|
|
8
9
|
import { getCanciaRuntime } from "virtual:cancia/runtime";
|
package/dist/index.d.ts
CHANGED
|
@@ -8,6 +8,7 @@ export { FieldDescription, FieldMeta, FieldMetaBase, FieldWidget, ListDescriptio
|
|
|
8
8
|
export { createJsonFileAdapter, createJsonFileAdapterV2, createSQLiteAdapter } from './storage/index.js';
|
|
9
9
|
export { z } from 'zod';
|
|
10
10
|
import 'astro/loaders';
|
|
11
|
+
import './portable-text-BikSqS9T.js';
|
|
11
12
|
|
|
12
13
|
interface R2UploadHandlerOptions {
|
|
13
14
|
/** Cloudflare account ID (from R2 dashboard) */
|
package/dist/index.js
CHANGED
|
@@ -3,7 +3,7 @@ import {
|
|
|
3
3
|
} from "./chunk-22DJVJBR.js";
|
|
4
4
|
import {
|
|
5
5
|
makeSchemasRoute
|
|
6
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-IIGDU5SV.js";
|
|
7
7
|
import "./chunk-NG5GJME5.js";
|
|
8
8
|
import {
|
|
9
9
|
setCanciaRuntime
|
|
@@ -13,7 +13,7 @@ import {
|
|
|
13
13
|
defineList,
|
|
14
14
|
describeList,
|
|
15
15
|
z
|
|
16
|
-
} from "./chunk-
|
|
16
|
+
} from "./chunk-MCHQV6Y7.js";
|
|
17
17
|
import {
|
|
18
18
|
canciaLoader
|
|
19
19
|
} from "./chunk-337LJIKX.js";
|
|
@@ -27,6 +27,7 @@ import {
|
|
|
27
27
|
import {
|
|
28
28
|
RevConflictError
|
|
29
29
|
} from "./chunk-7IA5B5CF.js";
|
|
30
|
+
import "./chunk-BOIQNZAO.js";
|
|
30
31
|
import {
|
|
31
32
|
detectImageType,
|
|
32
33
|
isValidSite
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
/** The four block styles the subset permits. */
|
|
4
|
+
declare const PT_STYLES: readonly ["normal", "h2", "h3", "blockquote"];
|
|
5
|
+
type PtStyle = (typeof PT_STYLES)[number];
|
|
6
|
+
/** The two list kinds the subset permits (absent = not a list item). */
|
|
7
|
+
declare const PT_LIST_ITEMS: readonly ["bullet", "number"];
|
|
8
|
+
type PtListItem = (typeof PT_LIST_ITEMS)[number];
|
|
9
|
+
/** The two decorators (marks that are not annotation keys) the subset permits. */
|
|
10
|
+
declare const PT_DECORATORS: readonly ["strong", "em"];
|
|
11
|
+
type PtDecorator = (typeof PT_DECORATORS)[number];
|
|
12
|
+
/**
|
|
13
|
+
* Whether a link href uses a SAFE scheme. This is the XSS gate for the only
|
|
14
|
+
* annotation the subset carries: astro-portabletext renders `href` straight
|
|
15
|
+
* into an <a href> without scheme filtering, so a link authored as
|
|
16
|
+
* `javascript:alert(1)` or `data:text/html,…` would be a live injection vector
|
|
17
|
+
* — exactly what the "no raw HTML" design (D4) exists to prevent. Only allow:
|
|
18
|
+
* - http(s), mailto:, tel: absolute URLs
|
|
19
|
+
* - root-relative (`/…`), fragment (`#…`), and relative (`./…`, `../…`, or a
|
|
20
|
+
* bare path) links that carry NO scheme at all.
|
|
21
|
+
* Everything with any other scheme (javascript:, data:, vbscript:, file:, …)
|
|
22
|
+
* is rejected. Exported so the parser and tests share one definition.
|
|
23
|
+
*/
|
|
24
|
+
declare function isSafeHref(href: string): boolean;
|
|
25
|
+
/**
|
|
26
|
+
* A link annotation. Stored in a block's `markDefs`; a span references it by
|
|
27
|
+
* putting the markDef's `_key` in its `marks` array. `href` is the only
|
|
28
|
+
* carried property (no title/target — kept minimal and sanitizable), and it is
|
|
29
|
+
* constrained to safe schemes (see isSafeHref) so a stored value can never
|
|
30
|
+
* carry a javascript:/data: XSS payload.
|
|
31
|
+
*/
|
|
32
|
+
declare const ptLinkMarkDefSchema: z.ZodObject<{
|
|
33
|
+
_type: z.ZodLiteral<"link">;
|
|
34
|
+
_key: z.ZodString;
|
|
35
|
+
href: z.ZodString;
|
|
36
|
+
}, z.core.$strip>;
|
|
37
|
+
type PtLinkMarkDef = z.infer<typeof ptLinkMarkDefSchema>;
|
|
38
|
+
/**
|
|
39
|
+
* A text span. `marks` entries are EITHER a decorator ("strong"/"em") OR a
|
|
40
|
+
* markDef `_key` present in the parent block's `markDefs`. We validate the
|
|
41
|
+
* decorator-or-string shape here; the cross-reference (every non-decorator mark
|
|
42
|
+
* resolves to a markDef) is enforced by the refine on the block below.
|
|
43
|
+
*/
|
|
44
|
+
declare const ptSpanSchema: z.ZodObject<{
|
|
45
|
+
_type: z.ZodLiteral<"span">;
|
|
46
|
+
_key: z.ZodString;
|
|
47
|
+
text: z.ZodString;
|
|
48
|
+
marks: z.ZodArray<z.ZodString>;
|
|
49
|
+
}, z.core.$strip>;
|
|
50
|
+
type PtSpan = z.infer<typeof ptSpanSchema>;
|
|
51
|
+
/**
|
|
52
|
+
* A single block. `listItem`/`level` are present only for list rows. Every
|
|
53
|
+
* span mark that is not a decorator must reference a link markDef by key —
|
|
54
|
+
* that guard is what keeps arbitrary annotations out of the stored value.
|
|
55
|
+
*/
|
|
56
|
+
declare const ptBlockSchema: z.ZodObject<{
|
|
57
|
+
_type: z.ZodLiteral<"block">;
|
|
58
|
+
_key: z.ZodString;
|
|
59
|
+
style: z.ZodEnum<{
|
|
60
|
+
normal: "normal";
|
|
61
|
+
h2: "h2";
|
|
62
|
+
h3: "h3";
|
|
63
|
+
blockquote: "blockquote";
|
|
64
|
+
}>;
|
|
65
|
+
listItem: z.ZodOptional<z.ZodEnum<{
|
|
66
|
+
number: "number";
|
|
67
|
+
bullet: "bullet";
|
|
68
|
+
}>>;
|
|
69
|
+
level: z.ZodOptional<z.ZodNumber>;
|
|
70
|
+
markDefs: z.ZodDefault<z.ZodArray<z.ZodObject<{
|
|
71
|
+
_type: z.ZodLiteral<"link">;
|
|
72
|
+
_key: z.ZodString;
|
|
73
|
+
href: z.ZodString;
|
|
74
|
+
}, z.core.$strip>>>;
|
|
75
|
+
children: z.ZodArray<z.ZodObject<{
|
|
76
|
+
_type: z.ZodLiteral<"span">;
|
|
77
|
+
_key: z.ZodString;
|
|
78
|
+
text: z.ZodString;
|
|
79
|
+
marks: z.ZodArray<z.ZodString>;
|
|
80
|
+
}, z.core.$strip>>;
|
|
81
|
+
}, z.core.$strip>;
|
|
82
|
+
type PtBlock = z.infer<typeof ptBlockSchema>;
|
|
83
|
+
/**
|
|
84
|
+
* The stored rich-text value: an array of subset blocks. Attach
|
|
85
|
+
* `.meta({ widget: "richtext" })` in defineField so describeList can identify
|
|
86
|
+
* it (its JSON-Schema type is "array", so the explicit widget is the only
|
|
87
|
+
* reliable signal — see the guard in describeProperty).
|
|
88
|
+
*/
|
|
89
|
+
declare const portableTextSubsetSchema: z.ZodArray<z.ZodObject<{
|
|
90
|
+
_type: z.ZodLiteral<"block">;
|
|
91
|
+
_key: z.ZodString;
|
|
92
|
+
style: z.ZodEnum<{
|
|
93
|
+
normal: "normal";
|
|
94
|
+
h2: "h2";
|
|
95
|
+
h3: "h3";
|
|
96
|
+
blockquote: "blockquote";
|
|
97
|
+
}>;
|
|
98
|
+
listItem: z.ZodOptional<z.ZodEnum<{
|
|
99
|
+
number: "number";
|
|
100
|
+
bullet: "bullet";
|
|
101
|
+
}>>;
|
|
102
|
+
level: z.ZodOptional<z.ZodNumber>;
|
|
103
|
+
markDefs: z.ZodDefault<z.ZodArray<z.ZodObject<{
|
|
104
|
+
_type: z.ZodLiteral<"link">;
|
|
105
|
+
_key: z.ZodString;
|
|
106
|
+
href: z.ZodString;
|
|
107
|
+
}, z.core.$strip>>>;
|
|
108
|
+
children: z.ZodArray<z.ZodObject<{
|
|
109
|
+
_type: z.ZodLiteral<"span">;
|
|
110
|
+
_key: z.ZodString;
|
|
111
|
+
text: z.ZodString;
|
|
112
|
+
marks: z.ZodArray<z.ZodString>;
|
|
113
|
+
}, z.core.$strip>>;
|
|
114
|
+
}, z.core.$strip>>;
|
|
115
|
+
type PortableTextValue = z.infer<typeof portableTextSubsetSchema>;
|
|
116
|
+
|
|
117
|
+
export { PT_DECORATORS as P, PT_LIST_ITEMS as a, PT_STYLES as b, type PortableTextValue as c, type PtBlock as d, type PtDecorator as e, type PtLinkMarkDef as f, type PtListItem as g, type PtSpan as h, type PtStyle as i, isSafeHref as j, ptBlockSchema as k, portableTextSubsetSchema as p };
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
---
|
|
2
|
+
// =============================================================================
|
|
3
|
+
// <CanciaRichText> — render a Cancia rich-text (Portable-Text SUBSET) value
|
|
4
|
+
// =============================================================================
|
|
5
|
+
// Renders the stored PT-subset array (see schema/portable-text.ts) to real
|
|
6
|
+
// HTML using astro-portabletext. The subset — normal/h2/h3/blockquote blocks,
|
|
7
|
+
// bullet/number lists, strong/em marks, link annotations — is a strict subset
|
|
8
|
+
// of Portable Text, so astro-portabletext's DEFAULT components already cover
|
|
9
|
+
// every case; we only override `link` to add rel/target hardening for external
|
|
10
|
+
// hrefs. There is no raw HTML in the value (D4), so nothing is dangerouslySet.
|
|
11
|
+
//
|
|
12
|
+
// Usage in a page/template:
|
|
13
|
+
// ---
|
|
14
|
+
// import CanciaRichText from "@cancia/astro/richtext/CanciaRichText.astro";
|
|
15
|
+
// const { body } = entry.data;
|
|
16
|
+
// ---
|
|
17
|
+
// <CanciaRichText value={body} />
|
|
18
|
+
//
|
|
19
|
+
// This file ships as a raw .astro under dist/richtext/ (an .astro file can't be
|
|
20
|
+
// a tsup entry) and is Vite-free at the package boundary — importing it does
|
|
21
|
+
// NOT pull in the Cancia integration.
|
|
22
|
+
// =============================================================================
|
|
23
|
+
import { PortableText } from "astro-portabletext";
|
|
24
|
+
import Link from "./Link.astro";
|
|
25
|
+
|
|
26
|
+
export interface Props {
|
|
27
|
+
/** The stored PT-subset value. Undefined / empty renders nothing. */
|
|
28
|
+
value?: unknown;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const { value } = Astro.props;
|
|
32
|
+
|
|
33
|
+
// Normalise: only render for a non-empty array of blocks. undefined, null, or
|
|
34
|
+
// [] all render nothing (empty-value handling — [] vs undefined both no-op).
|
|
35
|
+
const blocks = Array.isArray(value) ? value : [];
|
|
36
|
+
---
|
|
37
|
+
|
|
38
|
+
{
|
|
39
|
+
blocks.length > 0 && (
|
|
40
|
+
<div class="cancia-richtext">
|
|
41
|
+
<PortableText value={blocks} components={{ mark: { link: Link } }} />
|
|
42
|
+
</div>
|
|
43
|
+
)
|
|
44
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
---
|
|
2
|
+
// =============================================================================
|
|
3
|
+
// Link mark for <CanciaRichText>
|
|
4
|
+
// =============================================================================
|
|
5
|
+
// astro-portabletext hands a mark component the resolved markDef on `node`. For
|
|
6
|
+
// the subset's only annotation (link) that markDef is { _type:"link", href }.
|
|
7
|
+
// We render an <a> and harden EXTERNAL hrefs (anything with a scheme) with
|
|
8
|
+
// rel="noopener noreferrer" + target="_blank"; in-page/relative links stay
|
|
9
|
+
// plain. The href is a plain string from the validated value — never HTML.
|
|
10
|
+
// =============================================================================
|
|
11
|
+
const { node } = Astro.props as {
|
|
12
|
+
node: { markDef?: { href?: string } };
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const href = node?.markDef?.href ?? "#";
|
|
16
|
+
const isExternal = /^[a-z][a-z0-9+.-]*:/i.test(href) || href.startsWith("//");
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
{
|
|
20
|
+
isExternal ? (
|
|
21
|
+
<a href={href} rel="noopener noreferrer" target="_blank"><slot /></a>
|
|
22
|
+
) : (
|
|
23
|
+
<a href={href}><slot /></a>
|
|
24
|
+
)
|
|
25
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { i as PtStyle, g as PtListItem, d as PtBlock } from '../portable-text-BikSqS9T.js';
|
|
2
|
+
export { P as PT_DECORATORS, a as PT_LIST_ITEMS, b as PT_STYLES, c as PortableTextValue, e as PtDecorator, f as PtLinkMarkDef, h as PtSpan, j as isSafeHref, p as portableTextSubsetSchema, k as ptBlockSchema } from '../portable-text-BikSqS9T.js';
|
|
3
|
+
import 'zod';
|
|
4
|
+
|
|
5
|
+
/** A row as the editor holds it: raw shorthand text + style + optional list kind. */
|
|
6
|
+
interface RichTextRow {
|
|
7
|
+
text: string;
|
|
8
|
+
style: PtStyle;
|
|
9
|
+
listItem?: PtListItem;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Turn one editor row into a PT-subset block. Empty text still yields a block
|
|
13
|
+
* with a single empty span (PT convention) so the row survives a round-trip.
|
|
14
|
+
*/
|
|
15
|
+
declare function rowToBlock(row: RichTextRow): PtBlock;
|
|
16
|
+
/** Serialise a list of editor rows into a PT-subset value (block array). */
|
|
17
|
+
declare function rowsToPortableText(rows: RichTextRow[]): PtBlock[];
|
|
18
|
+
/** Turn a PT-subset block back into an editable row (text + style + list). */
|
|
19
|
+
declare function blockToRow(block: PtBlock): RichTextRow;
|
|
20
|
+
/** Deserialise a PT-subset value into editor rows. */
|
|
21
|
+
declare function portableTextToRows(value: PtBlock[]): RichTextRow[];
|
|
22
|
+
|
|
23
|
+
export { PtBlock, PtListItem, PtStyle, type RichTextRow, blockToRow, portableTextToRows, rowToBlock, rowsToPortableText };
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import {
|
|
2
|
+
PT_DECORATORS,
|
|
3
|
+
PT_LIST_ITEMS,
|
|
4
|
+
PT_STYLES,
|
|
5
|
+
isSafeHref,
|
|
6
|
+
portableTextSubsetSchema,
|
|
7
|
+
ptBlockSchema
|
|
8
|
+
} from "../chunk-BOIQNZAO.js";
|
|
9
|
+
|
|
10
|
+
// src/richtext/markdown.ts
|
|
11
|
+
function keyGen(prefix) {
|
|
12
|
+
let n = 0;
|
|
13
|
+
return () => `${prefix}${(n++).toString(36)}`;
|
|
14
|
+
}
|
|
15
|
+
function parseInline(input) {
|
|
16
|
+
const segments = [];
|
|
17
|
+
let plain = "";
|
|
18
|
+
let i = 0;
|
|
19
|
+
const flushPlain = () => {
|
|
20
|
+
if (plain) {
|
|
21
|
+
segments.push({ text: plain, marks: [] });
|
|
22
|
+
plain = "";
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
while (i < input.length) {
|
|
26
|
+
if (input[i] === "\\" && i + 1 < input.length) {
|
|
27
|
+
plain += input[i + 1];
|
|
28
|
+
i += 2;
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
if (input[i] === "[") {
|
|
32
|
+
const close = input.indexOf("]", i + 1);
|
|
33
|
+
if (close !== -1 && input[close + 1] === "(") {
|
|
34
|
+
const paren = input.indexOf(")", close + 2);
|
|
35
|
+
if (paren !== -1) {
|
|
36
|
+
const text = input.slice(i + 1, close);
|
|
37
|
+
const href = input.slice(close + 2, paren);
|
|
38
|
+
flushPlain();
|
|
39
|
+
segments.push({ text, marks: [], href });
|
|
40
|
+
i = paren + 1;
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
if (input[i] === "*" && input[i + 1] === "*") {
|
|
46
|
+
const end = input.indexOf("**", i + 2);
|
|
47
|
+
if (end !== -1 && end > i + 2) {
|
|
48
|
+
flushPlain();
|
|
49
|
+
segments.push({ text: input.slice(i + 2, end), marks: ["strong"] });
|
|
50
|
+
i = end + 2;
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
if (input[i] === "*") {
|
|
55
|
+
const end = input.indexOf("*", i + 1);
|
|
56
|
+
if (end !== -1 && end > i + 1) {
|
|
57
|
+
flushPlain();
|
|
58
|
+
segments.push({ text: input.slice(i + 1, end), marks: ["em"] });
|
|
59
|
+
i = end + 1;
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
plain += input[i];
|
|
64
|
+
i += 1;
|
|
65
|
+
}
|
|
66
|
+
flushPlain();
|
|
67
|
+
return segments;
|
|
68
|
+
}
|
|
69
|
+
function rowToBlock(row) {
|
|
70
|
+
const spanKey = keyGen("s");
|
|
71
|
+
const linkKey = keyGen("l");
|
|
72
|
+
const segments = parseInline(row.text);
|
|
73
|
+
const markDefs = [];
|
|
74
|
+
const children = [];
|
|
75
|
+
for (const seg of segments) {
|
|
76
|
+
const marks = [...seg.marks];
|
|
77
|
+
if (seg.href !== void 0 && isSafeHref(seg.href)) {
|
|
78
|
+
const _key = linkKey();
|
|
79
|
+
markDefs.push({ _type: "link", _key, href: seg.href });
|
|
80
|
+
marks.push(_key);
|
|
81
|
+
}
|
|
82
|
+
children.push({ _type: "span", _key: spanKey(), text: seg.text, marks });
|
|
83
|
+
}
|
|
84
|
+
if (children.length === 0) {
|
|
85
|
+
children.push({ _type: "span", _key: spanKey(), text: "", marks: [] });
|
|
86
|
+
}
|
|
87
|
+
const style = PT_STYLES.includes(row.style) ? row.style : "normal";
|
|
88
|
+
const block = {
|
|
89
|
+
_type: "block",
|
|
90
|
+
_key: keyGen("b")(),
|
|
91
|
+
style,
|
|
92
|
+
markDefs,
|
|
93
|
+
children
|
|
94
|
+
};
|
|
95
|
+
if (row.listItem && PT_LIST_ITEMS.includes(row.listItem)) {
|
|
96
|
+
block.listItem = row.listItem;
|
|
97
|
+
block.level = 1;
|
|
98
|
+
}
|
|
99
|
+
return block;
|
|
100
|
+
}
|
|
101
|
+
function rowsToPortableText(rows) {
|
|
102
|
+
return rows.map(rowToBlock);
|
|
103
|
+
}
|
|
104
|
+
function escapeShorthand(text) {
|
|
105
|
+
return text.replace(/([\\*\[\]])/g, "\\$1");
|
|
106
|
+
}
|
|
107
|
+
function spanToShorthand(span, linkHrefByKey) {
|
|
108
|
+
let out = escapeShorthand(span.text);
|
|
109
|
+
const href = span.marks.map((m) => linkHrefByKey.get(m)).find((h) => h !== void 0);
|
|
110
|
+
if (href !== void 0) out = `[${out}](${href})`;
|
|
111
|
+
if (span.marks.includes("strong")) out = `**${out}**`;
|
|
112
|
+
if (span.marks.includes("em")) out = `*${out}*`;
|
|
113
|
+
return out;
|
|
114
|
+
}
|
|
115
|
+
function blockToRow(block) {
|
|
116
|
+
const linkHrefByKey = /* @__PURE__ */ new Map();
|
|
117
|
+
for (const def of block.markDefs ?? []) {
|
|
118
|
+
if (def._type === "link") linkHrefByKey.set(def._key, def.href);
|
|
119
|
+
}
|
|
120
|
+
const text = (block.children ?? []).map((span) => spanToShorthand(span, linkHrefByKey)).join("");
|
|
121
|
+
const row = {
|
|
122
|
+
text,
|
|
123
|
+
style: PT_STYLES.includes(block.style) ? block.style : "normal"
|
|
124
|
+
};
|
|
125
|
+
if (block.listItem && PT_LIST_ITEMS.includes(block.listItem)) row.listItem = block.listItem;
|
|
126
|
+
return row;
|
|
127
|
+
}
|
|
128
|
+
function portableTextToRows(value) {
|
|
129
|
+
return value.map(blockToRow);
|
|
130
|
+
}
|
|
131
|
+
export {
|
|
132
|
+
PT_DECORATORS,
|
|
133
|
+
PT_LIST_ITEMS,
|
|
134
|
+
PT_STYLES,
|
|
135
|
+
blockToRow,
|
|
136
|
+
isSafeHref,
|
|
137
|
+
portableTextSubsetSchema,
|
|
138
|
+
portableTextToRows,
|
|
139
|
+
ptBlockSchema,
|
|
140
|
+
rowToBlock,
|
|
141
|
+
rowsToPortableText
|
|
142
|
+
};
|
package/dist/schema/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
2
|
export { z } from 'zod';
|
|
3
|
+
export { P as PT_DECORATORS, a as PT_LIST_ITEMS, b as PT_STYLES, c as PortableTextValue, d as PtBlock, e as PtDecorator, f as PtLinkMarkDef, g as PtListItem, h as PtSpan, i as PtStyle, j as isSafeHref, p as portableTextSubsetSchema, k as ptBlockSchema } from '../portable-text-BikSqS9T.js';
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* Widget hint the modal form uses to pick an input element. The Zod type
|
|
@@ -15,7 +16,7 @@ export { z } from 'zod';
|
|
|
15
16
|
* z.boolean() → "checkbox"
|
|
16
17
|
* z.enum([...]) → "select"
|
|
17
18
|
*/
|
|
18
|
-
type FieldWidget = "text" | "textarea" | "url" | "email" | "datetime" | "number" | "checkbox" | "select" | "image" | "slug";
|
|
19
|
+
type FieldWidget = "text" | "textarea" | "url" | "email" | "datetime" | "number" | "checkbox" | "select" | "image" | "slug" | "array" | "object" | "reference" | "richtext";
|
|
19
20
|
/**
|
|
20
21
|
* Fields common to every widget's metadata. All optional.
|
|
21
22
|
*/
|
|
@@ -51,6 +52,15 @@ type FieldMeta = (FieldMetaBase & {
|
|
|
51
52
|
}) | (FieldMetaBase & {
|
|
52
53
|
widget: "slug";
|
|
53
54
|
source?: string;
|
|
55
|
+
}) | (FieldMetaBase & {
|
|
56
|
+
widget: "array";
|
|
57
|
+
}) | (FieldMetaBase & {
|
|
58
|
+
widget: "object";
|
|
59
|
+
}) | (FieldMetaBase & {
|
|
60
|
+
widget: "reference";
|
|
61
|
+
list: string;
|
|
62
|
+
}) | (FieldMetaBase & {
|
|
63
|
+
widget: "richtext";
|
|
54
64
|
});
|
|
55
65
|
interface ListSchemaOptions<TFields extends Record<string, z.ZodTypeAny>> {
|
|
56
66
|
/** Display label for the list (e.g. "Blog Posts"). */
|
|
@@ -106,6 +116,15 @@ interface FieldDescription {
|
|
|
106
116
|
pattern?: string;
|
|
107
117
|
/** For slug widgets: the field name to auto-generate the slug from. */
|
|
108
118
|
source?: string;
|
|
119
|
+
/**
|
|
120
|
+
* For array widgets: the description of a single item. The item has no name
|
|
121
|
+
* of its own (arrays are keyed by position), so `of.name` is "".
|
|
122
|
+
*/
|
|
123
|
+
of?: FieldDescription;
|
|
124
|
+
/** For object widgets: the sub-field descriptions, in declared order. */
|
|
125
|
+
fields?: FieldDescription[];
|
|
126
|
+
/** For reference widgets: the name of the list whose entries this points at. */
|
|
127
|
+
referenceList?: string;
|
|
109
128
|
}
|
|
110
129
|
/**
|
|
111
130
|
* Typed field constructors. Each returns the underlying Zod type with a
|
|
@@ -148,6 +167,71 @@ declare const defineField: {
|
|
|
148
167
|
}) => z.ZodEnum<{
|
|
149
168
|
[x: string]: string;
|
|
150
169
|
}>;
|
|
170
|
+
/**
|
|
171
|
+
* A repeatable list of a single member type. `member` is any Zod type,
|
|
172
|
+
* typically another `defineField.*` (so it keeps its own `.meta({ widget })`
|
|
173
|
+
* and recursion carries labels/widgets into each row).
|
|
174
|
+
*
|
|
175
|
+
* bullets: f.array(f.text({ label: "Point" }), { label: "Key points" }),
|
|
176
|
+
* faqs: f.array(f.object({ q: f.text(), a: f.textarea() })),
|
|
177
|
+
*/
|
|
178
|
+
array: <TMember extends z.ZodTypeAny>(member: TMember, o?: FieldMetaBase) => z.ZodArray<TMember>;
|
|
179
|
+
/**
|
|
180
|
+
* A nested group of named sub-fields, rendered as a collapsible fieldset.
|
|
181
|
+
*
|
|
182
|
+
* socials: f.object({ linkedin: f.url(), twitter: f.url() }, { label: "Socials" }),
|
|
183
|
+
*/
|
|
184
|
+
object: <TShape extends Record<string, z.ZodTypeAny>>(shape: TShape, o?: FieldMetaBase) => z.ZodObject<{ -readonly [P in keyof TShape]: TShape[P]; }, z.core.$strip>;
|
|
185
|
+
/**
|
|
186
|
+
* A pointer to an entry in another list. Stored as a plain string — the
|
|
187
|
+
* target entry's id. Per the v0.1.0 design (D5) there is NO integrity
|
|
188
|
+
* backend: dangling ids are tolerated and surfaced by the editor, never
|
|
189
|
+
* prevented. `list` is the target list's name (a key in the schemas module).
|
|
190
|
+
*
|
|
191
|
+
* author: f.reference({ list: "people", label: "Author" }),
|
|
192
|
+
* related: f.array(f.reference({ list: "posts" }), { label: "Related" }),
|
|
193
|
+
*/
|
|
194
|
+
reference: (o: FieldMetaBase & {
|
|
195
|
+
list: string;
|
|
196
|
+
}) => z.ZodString;
|
|
197
|
+
/**
|
|
198
|
+
* A constrained rich-text body. Stored as a Portable-Text SUBSET array (D4):
|
|
199
|
+
* block styles normal/h2/h3/blockquote, bullet/number lists, strong/em marks,
|
|
200
|
+
* and link annotations — nothing else. The toolbar renders a structured block
|
|
201
|
+
* editor (approach B) over it, and `<CanciaRichText>` renders it to HTML.
|
|
202
|
+
*
|
|
203
|
+
* body: f.richtext({ label: "Body" }),
|
|
204
|
+
*
|
|
205
|
+
* The JSON-Schema type of this field is "array"; the explicit
|
|
206
|
+
* `widget: "richtext"` meta is what distinguishes it from a plain array in
|
|
207
|
+
* describeProperty.
|
|
208
|
+
*/
|
|
209
|
+
richtext: (o?: PlainFieldOptions) => z.ZodArray<z.ZodObject<{
|
|
210
|
+
_type: z.ZodLiteral<"block">;
|
|
211
|
+
_key: z.ZodString;
|
|
212
|
+
style: z.ZodEnum<{
|
|
213
|
+
normal: "normal";
|
|
214
|
+
h2: "h2";
|
|
215
|
+
h3: "h3";
|
|
216
|
+
blockquote: "blockquote";
|
|
217
|
+
}>;
|
|
218
|
+
listItem: z.ZodOptional<z.ZodEnum<{
|
|
219
|
+
number: "number";
|
|
220
|
+
bullet: "bullet";
|
|
221
|
+
}>>;
|
|
222
|
+
level: z.ZodOptional<z.ZodNumber>;
|
|
223
|
+
markDefs: z.ZodDefault<z.ZodArray<z.ZodObject<{
|
|
224
|
+
_type: z.ZodLiteral<"link">;
|
|
225
|
+
_key: z.ZodString;
|
|
226
|
+
href: z.ZodString;
|
|
227
|
+
}, z.core.$strip>>>;
|
|
228
|
+
children: z.ZodArray<z.ZodObject<{
|
|
229
|
+
_type: z.ZodLiteral<"span">;
|
|
230
|
+
_key: z.ZodString;
|
|
231
|
+
text: z.ZodString;
|
|
232
|
+
marks: z.ZodArray<z.ZodString>;
|
|
233
|
+
}, z.core.$strip>>;
|
|
234
|
+
}, z.core.$strip>>;
|
|
151
235
|
};
|
|
152
236
|
interface ListDescription {
|
|
153
237
|
name: string;
|
|
@@ -158,8 +242,22 @@ interface ListDescription {
|
|
|
158
242
|
slugField?: string;
|
|
159
243
|
fields: FieldDescription[];
|
|
160
244
|
}
|
|
245
|
+
/**
|
|
246
|
+
* Turn arbitrary text into a URL-safe slug: lowercase, spaces → hyphens,
|
|
247
|
+
* strip anything outside [a-z0-9-], collapse runs of hyphens, trim leading
|
|
248
|
+
* and trailing hyphens. Matches the slug field regex `^[a-z0-9-]+$`.
|
|
249
|
+
*
|
|
250
|
+
* Used by the toolbar to auto-derive a slug from a title as the editor types
|
|
251
|
+
* (plan 025). Kept here (not in the toolbar) so it is unit-tested alongside the
|
|
252
|
+
* schema helpers and stays the single definition of "what a slug looks like".
|
|
253
|
+
*
|
|
254
|
+
* slugify("Hello, World!") → "hello-world"
|
|
255
|
+
* slugify(" Multiple spaces ") → "multiple-spaces"
|
|
256
|
+
* slugify("Café déjà vu") → "caf-dj-vu" (non-ASCII stripped)
|
|
257
|
+
*/
|
|
258
|
+
declare function slugify(input: string): string;
|
|
161
259
|
declare function describeList(name: string, schema: ListSchema): ListDescription;
|
|
162
260
|
/** Map of list name → schema. What users export from src/cms/schemas.ts. */
|
|
163
261
|
type SchemasModule = Record<string, ListSchema>;
|
|
164
262
|
|
|
165
|
-
export { type FieldDescription, type FieldMeta, type FieldMetaBase, type FieldWidget, type ListDescription, type ListSchema, type ListSchemaOptions, type SchemasModule, defineField, defineList, describeList };
|
|
263
|
+
export { type FieldDescription, type FieldMeta, type FieldMetaBase, type FieldWidget, type ListDescription, type ListSchema, type ListSchemaOptions, type SchemasModule, defineField, defineList, describeList, slugify };
|
package/dist/schema/index.js
CHANGED
|
@@ -2,11 +2,27 @@ import {
|
|
|
2
2
|
defineField,
|
|
3
3
|
defineList,
|
|
4
4
|
describeList,
|
|
5
|
+
slugify,
|
|
5
6
|
z
|
|
6
|
-
} from "../chunk-
|
|
7
|
+
} from "../chunk-MCHQV6Y7.js";
|
|
8
|
+
import {
|
|
9
|
+
PT_DECORATORS,
|
|
10
|
+
PT_LIST_ITEMS,
|
|
11
|
+
PT_STYLES,
|
|
12
|
+
isSafeHref,
|
|
13
|
+
portableTextSubsetSchema,
|
|
14
|
+
ptBlockSchema
|
|
15
|
+
} from "../chunk-BOIQNZAO.js";
|
|
7
16
|
export {
|
|
17
|
+
PT_DECORATORS,
|
|
18
|
+
PT_LIST_ITEMS,
|
|
19
|
+
PT_STYLES,
|
|
8
20
|
defineField,
|
|
9
21
|
defineList,
|
|
10
22
|
describeList,
|
|
23
|
+
isSafeHref,
|
|
24
|
+
portableTextSubsetSchema,
|
|
25
|
+
ptBlockSchema,
|
|
26
|
+
slugify,
|
|
11
27
|
z
|
|
12
28
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cancia/astro",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"description": "Astro integration for Cancia CMS — inline editing with zero separate server",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -27,7 +27,12 @@
|
|
|
27
27
|
"./storage": {
|
|
28
28
|
"types": "./dist/storage/index.d.ts",
|
|
29
29
|
"import": "./dist/storage/index.js"
|
|
30
|
-
}
|
|
30
|
+
},
|
|
31
|
+
"./richtext": {
|
|
32
|
+
"types": "./dist/richtext/index.d.ts",
|
|
33
|
+
"import": "./dist/richtext/index.js"
|
|
34
|
+
},
|
|
35
|
+
"./richtext/CanciaRichText.astro": "./dist/richtext/CanciaRichText.astro"
|
|
31
36
|
},
|
|
32
37
|
"files": [
|
|
33
38
|
"dist"
|
|
@@ -54,6 +59,7 @@
|
|
|
54
59
|
"vitest": "^4.1.9"
|
|
55
60
|
},
|
|
56
61
|
"dependencies": {
|
|
62
|
+
"astro-portabletext": "^0.13.0",
|
|
57
63
|
"zod": "^4.4.3"
|
|
58
64
|
},
|
|
59
65
|
"scripts": {
|