@wervt/cms 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +135 -13
- package/dist/editor/assets/Button-BHCKwuIs.js +6 -0
- package/dist/editor/assets/RichTextInput-DHmilx6T.js +271 -0
- package/dist/editor/assets/Toast-pKrAdYOo.js +13 -0
- package/dist/editor/assets/index-JJD_O40W.js +16 -0
- package/dist/editor/assets/index-kXxO7vu1.css +2 -0
- package/dist/editor/assets/poppins-latin-400-normal-BOb3E3N0.woff +0 -0
- package/dist/editor/assets/poppins-latin-400-normal-cpxAROuN.woff2 +0 -0
- package/dist/editor/assets/poppins-latin-500-normal-C8OXljZJ.woff2 +0 -0
- package/dist/editor/assets/poppins-latin-500-normal-DGXqpDMm.woff +0 -0
- package/dist/editor/assets/poppins-latin-600-normal-BJdTmd5m.woff +0 -0
- package/dist/editor/assets/poppins-latin-600-normal-zEkxB9Mr.woff2 +0 -0
- package/dist/editor/index.html +15 -0
- package/dist/editor-theme.d.ts +28 -0
- package/dist/editor-theme.js +62 -0
- package/dist/module.d.ts +6 -1
- package/dist/module.js +97 -11
- package/dist/runtime/app/CmsRichText.d.ts +15 -0
- package/dist/runtime/app/CmsRichText.js +48 -0
- package/dist/runtime/app/useCms.d.ts +9 -2
- package/dist/runtime/app/useCms.js +11 -5
- package/dist/runtime/fields.d.ts +24 -0
- package/dist/runtime/fields.js +109 -0
- package/dist/runtime/pages.d.ts +3 -0
- package/dist/runtime/pages.js +14 -0
- package/dist/runtime/schema.d.ts +108 -0
- package/dist/runtime/schema.js +74 -0
- package/dist/runtime/server/dev-images.d.ts +2 -0
- package/dist/runtime/server/dev-images.js +12 -0
- package/dist/runtime/server/editor.get.d.ts +2 -0
- package/dist/runtime/server/editor.get.js +9 -0
- package/dist/runtime/server/entry.get.d.ts +7 -2
- package/dist/runtime/server/entry.get.js +11 -1
- package/dist/runtime/server/entry.put.js +14 -1
- package/dist/runtime/server/images.d.ts +4 -0
- package/dist/runtime/server/images.js +30 -0
- package/dist/runtime/server/schema.get.d.ts +10 -0
- package/dist/runtime/server/schema.get.js +10 -0
- package/dist/runtime/server/sniff.d.ts +11 -0
- package/dist/runtime/server/sniff.js +53 -0
- package/dist/runtime/server/upload.post.d.ts +13 -0
- package/dist/runtime/server/upload.post.js +44 -0
- package/package.json +29 -5
- package/dist/runtime/app/editor.vue +0 -42
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// src/editor-theme.ts
|
|
2
|
+
const TOKENS = [
|
|
3
|
+
"bg",
|
|
4
|
+
"bg-muted",
|
|
5
|
+
"bg-elevated",
|
|
6
|
+
"bg-accented",
|
|
7
|
+
"bg-inverted",
|
|
8
|
+
"border",
|
|
9
|
+
"border-muted",
|
|
10
|
+
"border-accented",
|
|
11
|
+
"border-inverted",
|
|
12
|
+
"text",
|
|
13
|
+
"text-dimmed",
|
|
14
|
+
"text-muted",
|
|
15
|
+
"text-toned",
|
|
16
|
+
"text-highlighted",
|
|
17
|
+
"text-inverted"
|
|
18
|
+
];
|
|
19
|
+
/** What the module adds to the editor's `<head>`; throws on values that would break out of CSS. */
|
|
20
|
+
function editorThemeHead(theme) {
|
|
21
|
+
if (!theme) return "";
|
|
22
|
+
const css = (where, value) => {
|
|
23
|
+
if (typeof value !== "string" || !value.trim() || /[<>{};\\]/.test(value)) throw new Error(`[@wervt/cms] cms.editor.theme.${where} must be a plain CSS value`);
|
|
24
|
+
return value.trim();
|
|
25
|
+
};
|
|
26
|
+
const colors = (mode) => Object.entries(theme[mode] ?? {}).map(([token, value]) => {
|
|
27
|
+
if (!TOKENS.includes(token)) throw new Error(`[@wervt/cms] cms.editor.theme.${mode}.${token} is not one of ${TOKENS.join(", ")}`);
|
|
28
|
+
return `--ui-${token}: ${css(`${mode}.${token}`, value)};`;
|
|
29
|
+
});
|
|
30
|
+
const primary = typeof theme.primary === "object" ? {
|
|
31
|
+
light: css("primary.light", theme.primary.light),
|
|
32
|
+
dark: css("primary.dark", theme.primary.dark)
|
|
33
|
+
} : theme.primary === void 0 ? void 0 : {
|
|
34
|
+
light: css("primary", theme.primary),
|
|
35
|
+
dark: css("primary", theme.primary)
|
|
36
|
+
};
|
|
37
|
+
const root = [
|
|
38
|
+
...primary ? [`--ui-primary: ${primary.light};`] : [],
|
|
39
|
+
...theme.radius === void 0 ? [] : [`--ui-radius: ${css("radius", theme.radius)};`],
|
|
40
|
+
...theme.font === void 0 ? [] : [`--font-sans: ${css("font", theme.font)};`]
|
|
41
|
+
];
|
|
42
|
+
const light = colors("light");
|
|
43
|
+
const dark = [...primary ? [`--ui-primary: ${primary.dark};`] : [], ...colors("dark")];
|
|
44
|
+
const tags = [];
|
|
45
|
+
const rules = [
|
|
46
|
+
...root.length ? [`:root { ${root.join(" ")} }`] : [],
|
|
47
|
+
...light.length ? [`:root:not(.dark) { ${light.join(" ")} }`] : [],
|
|
48
|
+
...dark.length ? [`.dark { ${dark.join(" ")} }`] : []
|
|
49
|
+
];
|
|
50
|
+
if (rules.length) tags.push(`<style>${rules.join("\n")}</style>`);
|
|
51
|
+
if (theme.stylesheet !== void 0) {
|
|
52
|
+
const href = theme.stylesheet;
|
|
53
|
+
if (!/^(\/(?!\/)|https:\/\/)[^\s"'<>\\]+$/.test(href)) throw new Error("[@wervt/cms] cms.editor.theme.stylesheet must be a path like /editor.css or an https URL");
|
|
54
|
+
tags.push(`<link rel="stylesheet" href="${href}">`);
|
|
55
|
+
}
|
|
56
|
+
if (theme.buttons !== void 0) {
|
|
57
|
+
if (theme.buttons !== "neutral" && theme.buttons !== "primary") throw new Error("[@wervt/cms] cms.editor.theme.buttons must be 'neutral' or 'primary'");
|
|
58
|
+
tags.push(`<meta name="cms-buttons" content="${theme.buttons}">`);
|
|
59
|
+
}
|
|
60
|
+
return tags.join("\n");
|
|
61
|
+
}
|
|
62
|
+
export { editorThemeHead };
|
package/dist/module.d.ts
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
|
+
import { EditorTheme, EditorThemeColors } from "./editor-theme.js";
|
|
1
2
|
export interface ModuleOptions {
|
|
2
3
|
/**
|
|
3
4
|
* Signed-in people with a confirmed address who may edit besides the app's owner, members and
|
|
4
5
|
* admins. Set at runtime with `NUXT_CMS_EDITORS` (comma-separated), no redeploy needed.
|
|
5
6
|
*/
|
|
6
7
|
editors?: string;
|
|
8
|
+
editor?: {
|
|
9
|
+
/** The editor's look on this site; wervt's when missing. */
|
|
10
|
+
theme?: EditorTheme;
|
|
11
|
+
};
|
|
7
12
|
}
|
|
8
13
|
declare const _default: import("nuxt/schema").NuxtModule<ModuleOptions, ModuleOptions, false>;
|
|
9
|
-
export { _default as default };
|
|
14
|
+
export { type EditorTheme, type EditorThemeColors, _default as default };
|
package/dist/module.js
CHANGED
|
@@ -1,14 +1,18 @@
|
|
|
1
|
+
import { editorThemeHead } from "./editor-theme.js";
|
|
1
2
|
import { stampEntries } from "./runtime/server/entries.js";
|
|
2
|
-
import { existsSync, readFileSync } from "node:fs";
|
|
3
|
-
import { join } from "node:path";
|
|
4
|
-
import { addImports, addServerHandler, addServerTemplate, createResolver, defineNuxtModule,
|
|
3
|
+
import { existsSync, mkdirSync, readFileSync } from "node:fs";
|
|
4
|
+
import { join, relative, resolve } from "node:path";
|
|
5
|
+
import { addComponent, addImports, addServerHandler, addServerTemplate, addTemplate, addTypeTemplate, createResolver, defineNuxtModule, resolveFiles } from "nuxt/kit";
|
|
5
6
|
// src/module.ts
|
|
6
7
|
var module_default = defineNuxtModule({
|
|
7
8
|
meta: {
|
|
8
9
|
name: "@wervt/cms",
|
|
9
10
|
configKey: "cms"
|
|
10
11
|
},
|
|
11
|
-
moduleDependencies: {
|
|
12
|
+
moduleDependencies: {
|
|
13
|
+
ablage: { defaults: { image: false } },
|
|
14
|
+
"@wervt/nuxt": { overrides: { database: true } }
|
|
15
|
+
},
|
|
12
16
|
defaults: { editors: "" },
|
|
13
17
|
setup(options, nuxt) {
|
|
14
18
|
const resolver = createResolver(import.meta.url);
|
|
@@ -17,7 +21,51 @@ var module_default = defineNuxtModule({
|
|
|
17
21
|
editors: options.editors ?? "",
|
|
18
22
|
...config.cms
|
|
19
23
|
};
|
|
20
|
-
const
|
|
24
|
+
const cmsDir = join(nuxt.options.rootDir, "cms");
|
|
25
|
+
const file = join(cmsDir, "entries.json");
|
|
26
|
+
const publicDir = resolve(nuxt.options.rootDir, nuxt.options.dir.public);
|
|
27
|
+
const schemaFiles = async () => (await resolveFiles(cmsDir, [
|
|
28
|
+
"**/*.{ts,js,mjs}",
|
|
29
|
+
"!**/*.d.ts",
|
|
30
|
+
"!**/*.test.*"
|
|
31
|
+
])).sort();
|
|
32
|
+
const pagesTemplate = addTemplate({
|
|
33
|
+
filename: "wervt-cms/pages.mjs",
|
|
34
|
+
write: true,
|
|
35
|
+
getContents: async () => {
|
|
36
|
+
const files = await schemaFiles();
|
|
37
|
+
return [
|
|
38
|
+
...files.map((path, i) => `import * as m${i} from ${JSON.stringify(path)}`),
|
|
39
|
+
`export const modules = [${files.map((_, i) => `m${i}`).join(", ")}]`,
|
|
40
|
+
""
|
|
41
|
+
].join("\n");
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
nuxt.options.alias["#wervt-cms/pages"] = pagesTemplate.dst;
|
|
45
|
+
addTypeTemplate({
|
|
46
|
+
filename: "types/wervt-cms.d.ts",
|
|
47
|
+
getContents: async () => {
|
|
48
|
+
const dir = join(nuxt.options.buildDir, "types");
|
|
49
|
+
const pages = (await schemaFiles()).map((path) => `PagesOf<typeof import(${JSON.stringify(relative(dir, path).replace(/\.[cm]?[jt]s$/, ""))})>`);
|
|
50
|
+
return [
|
|
51
|
+
`import type { PagesOf } from '@wervt/cms/schema'`,
|
|
52
|
+
`declare module '@wervt/cms/schema' {`,
|
|
53
|
+
` interface CmsPages ${pages.length ? `extends ${pages.join(", ")} ` : ""}{}`,
|
|
54
|
+
`}`,
|
|
55
|
+
`export {}`,
|
|
56
|
+
""
|
|
57
|
+
].join("\n");
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
if (nuxt.options.dev && !nuxt.options._prepare) {
|
|
61
|
+
mkdirSync(cmsDir, { recursive: true });
|
|
62
|
+
if (!nuxt.options.watch.includes(cmsDir)) nuxt.options.watch.push(cmsDir);
|
|
63
|
+
}
|
|
64
|
+
nuxt.hook("builder:watch", async (event, path) => {
|
|
65
|
+
if (event !== "add" && event !== "unlink") return;
|
|
66
|
+
const absolute = resolve(nuxt.options.srcDir, path);
|
|
67
|
+
if (absolute.startsWith(`${cmsDir}/`) && /\.(?:ts|js|mjs)$/.test(absolute)) await nuxt.callHook("restart");
|
|
68
|
+
});
|
|
21
69
|
addServerTemplate({
|
|
22
70
|
filename: "#wervt-cms/entries",
|
|
23
71
|
getContents: () => {
|
|
@@ -26,6 +74,7 @@ var module_default = defineNuxtModule({
|
|
|
26
74
|
return [
|
|
27
75
|
`export const entries = ${JSON.stringify(stamped)}`,
|
|
28
76
|
`export const file = ${nuxt.options.dev ? JSON.stringify(file) : "undefined"}`,
|
|
77
|
+
`export const publicDir = ${nuxt.options.dev ? JSON.stringify(publicDir) : "undefined"}`,
|
|
29
78
|
""
|
|
30
79
|
].join("\n");
|
|
31
80
|
}
|
|
@@ -40,17 +89,54 @@ var module_default = defineNuxtModule({
|
|
|
40
89
|
method: "put",
|
|
41
90
|
handler: resolver.resolve("./runtime/server/entry.put")
|
|
42
91
|
});
|
|
92
|
+
addServerHandler({
|
|
93
|
+
route: "/api/_cms/schema",
|
|
94
|
+
method: "get",
|
|
95
|
+
handler: resolver.resolve("./runtime/server/schema.get")
|
|
96
|
+
});
|
|
97
|
+
addServerHandler({
|
|
98
|
+
route: "/api/_cms/images",
|
|
99
|
+
method: "post",
|
|
100
|
+
handler: resolver.resolve("./runtime/server/upload.post")
|
|
101
|
+
});
|
|
43
102
|
addImports({
|
|
44
103
|
name: "useCms",
|
|
45
104
|
from: resolver.resolve("./runtime/app/useCms")
|
|
46
105
|
});
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
path: "/_cms",
|
|
51
|
-
file: resolver.resolve("./runtime/app/editor.vue")
|
|
52
|
-
});
|
|
106
|
+
addComponent({
|
|
107
|
+
name: "CmsRichText",
|
|
108
|
+
filePath: resolver.resolve("./runtime/app/CmsRichText")
|
|
53
109
|
});
|
|
110
|
+
setupEditor(resolver.resolve("../dist/editor"), editorThemeHead(options.editor?.theme), nuxt);
|
|
54
111
|
}
|
|
55
112
|
});
|
|
113
|
+
/**
|
|
114
|
+
* The editor at `/_cms` is a prebuilt app in the package (`editor/`), not a page of the site: its
|
|
115
|
+
* Tailwind and Nuxt UI stay out of the site's pages, and it works whatever the site is built with.
|
|
116
|
+
* `../dist/editor` is the same folder from `src/` and from `dist/`.
|
|
117
|
+
*/
|
|
118
|
+
function setupEditor(dir, themeHead, nuxt) {
|
|
119
|
+
const html = join(dir, "index.html");
|
|
120
|
+
if (!existsSync(html)) {
|
|
121
|
+
console.warn(`[@wervt/cms] No editor build in ${dir}; run \`vp build editor\` in packages/cms.`);
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
nuxt.hook("nitro:config", (config) => {
|
|
125
|
+
const nitro = config;
|
|
126
|
+
nitro.publicAssets = [...nitro.publicAssets ?? [], {
|
|
127
|
+
dir: join(dir, "assets"),
|
|
128
|
+
baseURL: "/_cms/assets",
|
|
129
|
+
maxAge: 31536e3
|
|
130
|
+
}];
|
|
131
|
+
});
|
|
132
|
+
addServerTemplate({
|
|
133
|
+
filename: "#wervt-cms/editor",
|
|
134
|
+
getContents: () => `export const html = ${JSON.stringify(readFileSync(html, "utf8").replace("</head>", `${themeHead}\n</head>`))}\n`
|
|
135
|
+
});
|
|
136
|
+
addServerHandler({
|
|
137
|
+
route: "/_cms",
|
|
138
|
+
method: "get",
|
|
139
|
+
handler: createResolver(import.meta.url).resolve("./runtime/server/editor.get")
|
|
140
|
+
});
|
|
141
|
+
}
|
|
56
142
|
export { module_default as default };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** Renders a `richText` field: `<CmsRichText :value="home.intro" />`. */
|
|
2
|
+
declare const _default: import("vue").DefineComponent<import("vue").ExtractPropTypes<{
|
|
3
|
+
value: {
|
|
4
|
+
type: StringConstructor;
|
|
5
|
+
required: true;
|
|
6
|
+
};
|
|
7
|
+
}>, () => import("vue").VNode<import("vue").RendererNode, import("vue").RendererElement, {
|
|
8
|
+
[key: string]: any;
|
|
9
|
+
}>, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<import("vue").ExtractPropTypes<{
|
|
10
|
+
value: {
|
|
11
|
+
type: StringConstructor;
|
|
12
|
+
required: true;
|
|
13
|
+
};
|
|
14
|
+
}>> & Readonly<{}>, {}, {}, {}, {}, string, import("vue").ComponentProvideOptions, true, {}, any>;
|
|
15
|
+
export { _default as default };
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { Markdown } from "@comark/vue";
|
|
2
|
+
import security from "comark/plugins/security";
|
|
3
|
+
import { defineComponent, h } from "vue";
|
|
4
|
+
// src/runtime/app/CmsRichText.ts
|
|
5
|
+
const options = { registerDefaultPlugins: false };
|
|
6
|
+
const plugins = [security({ allowedTags: [
|
|
7
|
+
"p",
|
|
8
|
+
"br",
|
|
9
|
+
"strong",
|
|
10
|
+
"em",
|
|
11
|
+
"del",
|
|
12
|
+
"s",
|
|
13
|
+
"code",
|
|
14
|
+
"pre",
|
|
15
|
+
"blockquote",
|
|
16
|
+
"hr",
|
|
17
|
+
"a",
|
|
18
|
+
"img",
|
|
19
|
+
"ul",
|
|
20
|
+
"ol",
|
|
21
|
+
"li",
|
|
22
|
+
"h1",
|
|
23
|
+
"h2",
|
|
24
|
+
"h3",
|
|
25
|
+
"h4",
|
|
26
|
+
"h5",
|
|
27
|
+
"h6",
|
|
28
|
+
"table",
|
|
29
|
+
"thead",
|
|
30
|
+
"tbody",
|
|
31
|
+
"tr",
|
|
32
|
+
"th",
|
|
33
|
+
"td"
|
|
34
|
+
] })];
|
|
35
|
+
/** Renders a `richText` field: `<CmsRichText :value="home.intro" />`. */
|
|
36
|
+
var CmsRichText_default = defineComponent({
|
|
37
|
+
name: "CmsRichText",
|
|
38
|
+
props: { value: {
|
|
39
|
+
type: String,
|
|
40
|
+
required: true
|
|
41
|
+
} },
|
|
42
|
+
setup: (props) => () => h(Markdown, {
|
|
43
|
+
value: props.value,
|
|
44
|
+
options,
|
|
45
|
+
plugins
|
|
46
|
+
})
|
|
47
|
+
});
|
|
48
|
+
export { CmsRichText_default as default };
|
|
@@ -1,2 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { CmsFields, CmsPages, CmsValue } from "../schema.js";
|
|
2
|
+
import { ComputedRef } from "vue";
|
|
3
|
+
/**
|
|
4
|
+
* The content of a page defined in `cms/*.ts`, typed from its fields. Values nobody edited yet
|
|
5
|
+
* come from the defaults there.
|
|
6
|
+
*/
|
|
7
|
+
export declare function useCms<K extends Extract<keyof CmsPages, string>>(key: K): ComputedRef<CmsPages[K] extends CmsFields ? CmsValue<CmsPages[K]> : never>;
|
|
8
|
+
/** Content without a schema: what was saved, over `defaults`. */
|
|
9
|
+
export declare function useCms<T extends Record<string, unknown>>(key: string, defaults: T): ComputedRef<T>;
|
|
@@ -1,12 +1,18 @@
|
|
|
1
|
+
import { resolveValue, staticImages } from "../fields.js";
|
|
2
|
+
import { pages } from "../pages.js";
|
|
1
3
|
import { computed } from "vue";
|
|
2
4
|
import { useFetch } from "#app";
|
|
3
5
|
// src/runtime/app/useCms.ts
|
|
4
|
-
/** The entry's saved values over `defaults`, so a page renders fully before anything is saved. */
|
|
5
6
|
function useCms(key, defaults) {
|
|
6
7
|
const { data } = useFetch(`/api/_cms/entries/${key}`, { key: `wervt-cms:${key}` });
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
const page = pages.get(key);
|
|
9
|
+
return computed(() => {
|
|
10
|
+
const stored = data.value?.value;
|
|
11
|
+
if (defaults) return {
|
|
12
|
+
...defaults,
|
|
13
|
+
...stored
|
|
14
|
+
};
|
|
15
|
+
return stored ?? (page ? resolveValue(page.fields, void 0, staticImages) : {});
|
|
16
|
+
});
|
|
11
17
|
}
|
|
12
18
|
export { useCms };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { CmsField, CmsFields, CmsImage, StoredImage } from "./schema.js";
|
|
2
|
+
export declare function isField(value: CmsField | CmsFields): value is CmsField;
|
|
3
|
+
type Plain = Record<string, unknown>;
|
|
4
|
+
/** Turns a stored image into what the page shows; `undefined` when it can't be shown. */
|
|
5
|
+
export type ImageResolver = (image: StoredImage) => CmsImage | undefined;
|
|
6
|
+
/** Shows paths and URLs as they are; uploaded files need the server's resolver. */
|
|
7
|
+
export declare const staticImages: ImageResolver;
|
|
8
|
+
/**
|
|
9
|
+
* The value a page shows: what was stored where it fits the field, the default everywhere else.
|
|
10
|
+
* Stored values that don't fit (the field changed type since) fall back to the default instead
|
|
11
|
+
* of breaking the page. Keys the schema doesn't know are kept, so nothing an edit sent is lost
|
|
12
|
+
* when a field is renamed back.
|
|
13
|
+
*/
|
|
14
|
+
export declare function resolveValue(fields: CmsFields, stored: unknown, images: ImageResolver): Plain;
|
|
15
|
+
export declare class InvalidValue extends Error {}
|
|
16
|
+
/**
|
|
17
|
+
* Checks an edit against the schema and brings it into the form it's stored in. Takes what GET
|
|
18
|
+
* answered as well, so the editor can send a value back unchanged: an image the page got as a
|
|
19
|
+
* signed URL is stored as its file again.
|
|
20
|
+
*/
|
|
21
|
+
export declare function normalizeValue(fields: CmsFields, input: unknown, path?: string): Plain;
|
|
22
|
+
/** An edited `href` is put on the live site as is, so only harmless schemes get through. */
|
|
23
|
+
export declare function isSafeHref(href: string): boolean;
|
|
24
|
+
export {};
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
// src/runtime/fields.ts
|
|
2
|
+
function isField(value) {
|
|
3
|
+
return typeof value.type === "string";
|
|
4
|
+
}
|
|
5
|
+
const isPlain = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
6
|
+
/** Shows paths and URLs as they are; uploaded files need the server's resolver. */
|
|
7
|
+
const staticImages = (image) => "src" in image ? {
|
|
8
|
+
src: image.src,
|
|
9
|
+
alt: image.alt ?? ""
|
|
10
|
+
} : void 0;
|
|
11
|
+
/**
|
|
12
|
+
* The value a page shows: what was stored where it fits the field, the default everywhere else.
|
|
13
|
+
* Stored values that don't fit (the field changed type since) fall back to the default instead
|
|
14
|
+
* of breaking the page. Keys the schema doesn't know are kept, so nothing an edit sent is lost
|
|
15
|
+
* when a field is renamed back.
|
|
16
|
+
*/
|
|
17
|
+
function resolveValue(fields, stored, images) {
|
|
18
|
+
const value = isPlain(stored) ? { ...stored } : {};
|
|
19
|
+
for (const [name, field] of Object.entries(fields)) value[name] = isField(field) ? resolveField(field, value[name], images) : resolveValue(field, value[name], images);
|
|
20
|
+
return value;
|
|
21
|
+
}
|
|
22
|
+
function resolveField(field, stored, images) {
|
|
23
|
+
switch (field.type) {
|
|
24
|
+
case "text":
|
|
25
|
+
case "richText": return typeof stored === "string" ? stored : field.default;
|
|
26
|
+
case "toggle": return typeof stored === "boolean" ? stored : field.default;
|
|
27
|
+
case "link": return toLink(stored) ?? field.default;
|
|
28
|
+
case "image": {
|
|
29
|
+
const image = stored === null ? null : toImage(stored);
|
|
30
|
+
if (image === null && !field.required) return null;
|
|
31
|
+
const shown = image && images(image);
|
|
32
|
+
if (shown) return shown;
|
|
33
|
+
return field.default && (images(field.default) ?? null);
|
|
34
|
+
}
|
|
35
|
+
case "list": return (Array.isArray(stored) ? stored : field.default).map((item) => resolveValue(field.item, item, images));
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
var InvalidValue = class extends Error {};
|
|
39
|
+
/**
|
|
40
|
+
* Checks an edit against the schema and brings it into the form it's stored in. Takes what GET
|
|
41
|
+
* answered as well, so the editor can send a value back unchanged: an image the page got as a
|
|
42
|
+
* signed URL is stored as its file again.
|
|
43
|
+
*/
|
|
44
|
+
function normalizeValue(fields, input, path = "") {
|
|
45
|
+
if (!isPlain(input)) throw new InvalidValue(`${path || "value"}: expected an object`);
|
|
46
|
+
const value = { ...input };
|
|
47
|
+
for (const [name, field] of Object.entries(fields)) {
|
|
48
|
+
if (!(name in input) || input[name] === void 0) continue;
|
|
49
|
+
const at = path ? `${path}.${name}` : name;
|
|
50
|
+
value[name] = isField(field) ? normalizeField(field, input[name], at) : normalizeValue(field, input[name], at);
|
|
51
|
+
}
|
|
52
|
+
return value;
|
|
53
|
+
}
|
|
54
|
+
const MAX_ITEMS = 500;
|
|
55
|
+
function normalizeField(field, input, at) {
|
|
56
|
+
switch (field.type) {
|
|
57
|
+
case "text":
|
|
58
|
+
case "richText":
|
|
59
|
+
if (typeof input !== "string") throw new InvalidValue(`${at}: expected text`);
|
|
60
|
+
return input;
|
|
61
|
+
case "toggle":
|
|
62
|
+
if (typeof input !== "boolean") throw new InvalidValue(`${at}: expected true or false`);
|
|
63
|
+
return input;
|
|
64
|
+
case "link": {
|
|
65
|
+
const value = toLink(input);
|
|
66
|
+
if (!value) throw new InvalidValue(`${at}: expected { label, href } with a safe href`);
|
|
67
|
+
return value;
|
|
68
|
+
}
|
|
69
|
+
case "image": {
|
|
70
|
+
if (input === null && !field.required) return null;
|
|
71
|
+
const value = toImage(input);
|
|
72
|
+
if (!value) throw new InvalidValue(`${at}: expected an uploaded image or a path`);
|
|
73
|
+
return value;
|
|
74
|
+
}
|
|
75
|
+
case "list":
|
|
76
|
+
if (!Array.isArray(input)) throw new InvalidValue(`${at}: expected a list`);
|
|
77
|
+
if (input.length > MAX_ITEMS) throw new InvalidValue(`${at}: at most ${MAX_ITEMS} items`);
|
|
78
|
+
return input.map((item, i) => normalizeValue(field.item, item, `${at}[${i}]`));
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
const SAFE_HREF = /^(?:\/(?!\/)|#|\?|https?:\/\/|mailto:|tel:)/i;
|
|
82
|
+
const SAFE_SRC = /^(?:\/(?!\/)|https:\/\/)/i;
|
|
83
|
+
const FILE_ID = /^[A-Za-z0-9_-]{1,100}$/;
|
|
84
|
+
/** An edited `href` is put on the live site as is, so only harmless schemes get through. */
|
|
85
|
+
function isSafeHref(href) {
|
|
86
|
+
return href === "" || SAFE_HREF.test(href);
|
|
87
|
+
}
|
|
88
|
+
function toLink(input) {
|
|
89
|
+
if (!isPlain(input) || typeof input.label !== "string" || typeof input.href !== "string") return void 0;
|
|
90
|
+
if (!isSafeHref(input.href.trim())) return void 0;
|
|
91
|
+
return {
|
|
92
|
+
label: input.label,
|
|
93
|
+
href: input.href.trim()
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
function toImage(input) {
|
|
97
|
+
if (typeof input === "string") input = { src: input };
|
|
98
|
+
if (!isPlain(input)) return void 0;
|
|
99
|
+
const alt = typeof input.alt === "string" && input.alt ? { alt: input.alt } : {};
|
|
100
|
+
if (typeof input.id === "string") return FILE_ID.test(input.id) ? {
|
|
101
|
+
id: input.id,
|
|
102
|
+
...alt
|
|
103
|
+
} : void 0;
|
|
104
|
+
if (typeof input.src === "string" && SAFE_SRC.test(input.src)) return {
|
|
105
|
+
src: input.src,
|
|
106
|
+
...alt
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
export { InvalidValue, isField, isSafeHref, normalizeValue, resolveValue, staticImages };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { isCmsPage } from "./schema.js";
|
|
2
|
+
import { modules } from "#wervt-cms/pages";
|
|
3
|
+
// src/runtime/pages.ts
|
|
4
|
+
function collect(found) {
|
|
5
|
+
const pages = /* @__PURE__ */ new Map();
|
|
6
|
+
for (const module of found) for (const value of Object.values(module)) if (isCmsPage(value)) {
|
|
7
|
+
if (pages.has(value.key) && pages.get(value.key) !== value) console.warn(`[cms] page \`${value.key}\` is defined twice in cms/, the last one counts`);
|
|
8
|
+
pages.set(value.key, value);
|
|
9
|
+
}
|
|
10
|
+
return pages;
|
|
11
|
+
}
|
|
12
|
+
/** The pages the app defines in `cms/*.ts`, by key. */
|
|
13
|
+
const pages = collect(modules);
|
|
14
|
+
export { pages };
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
interface FieldBase<T> {
|
|
2
|
+
/** Shown in the editor instead of the field's name. */
|
|
3
|
+
label?: string;
|
|
4
|
+
default: T;
|
|
5
|
+
}
|
|
6
|
+
export interface TextField extends FieldBase<string> {
|
|
7
|
+
type: 'text';
|
|
8
|
+
/** Several lines in the editor. */
|
|
9
|
+
multiline?: boolean;
|
|
10
|
+
}
|
|
11
|
+
export interface RichTextField extends FieldBase<string> {
|
|
12
|
+
type: 'richText';
|
|
13
|
+
}
|
|
14
|
+
export interface ToggleField extends FieldBase<boolean> {
|
|
15
|
+
type: 'toggle';
|
|
16
|
+
}
|
|
17
|
+
export interface LinkField extends FieldBase<CmsLink> {
|
|
18
|
+
type: 'link';
|
|
19
|
+
}
|
|
20
|
+
export interface ImageField<Required extends boolean = boolean> extends FieldBase<StoredImage | null> {
|
|
21
|
+
type: 'image';
|
|
22
|
+
/** With a default the page always gets an image, and the editor can only replace it. */
|
|
23
|
+
required: Required;
|
|
24
|
+
}
|
|
25
|
+
export interface ListField<Item extends CmsFields = CmsFields> extends FieldBase<InputOf<Item>[]> {
|
|
26
|
+
type: 'list';
|
|
27
|
+
item: Item;
|
|
28
|
+
}
|
|
29
|
+
export type CmsField = TextField | RichTextField | ToggleField | LinkField | ImageField | ListField;
|
|
30
|
+
/** Fields by name; a nested object is a group of fields shown together. */
|
|
31
|
+
export interface CmsFields {
|
|
32
|
+
[name: string]: CmsField | CmsFields;
|
|
33
|
+
}
|
|
34
|
+
export interface CmsLink {
|
|
35
|
+
label: string;
|
|
36
|
+
href: string;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* An image as it's stored: a file the editor uploaded to the app's bucket (`id`), or a path or
|
|
40
|
+
* URL the app ships with (`src`, e.g. `/hero.jpg` from `public/`).
|
|
41
|
+
*/
|
|
42
|
+
export type StoredImage = {
|
|
43
|
+
id: string;
|
|
44
|
+
alt?: string;
|
|
45
|
+
} | {
|
|
46
|
+
src: string;
|
|
47
|
+
alt?: string;
|
|
48
|
+
};
|
|
49
|
+
/** An image as the page gets it. */
|
|
50
|
+
export interface CmsImage {
|
|
51
|
+
src: string;
|
|
52
|
+
/** For uploaded images: the same picture in several widths. */
|
|
53
|
+
srcset?: string;
|
|
54
|
+
alt: string;
|
|
55
|
+
/** The uploaded file, for the editor. */
|
|
56
|
+
id?: string;
|
|
57
|
+
}
|
|
58
|
+
/** The value the page gets. */
|
|
59
|
+
export type CmsValue<F extends CmsFields> = { [K in keyof F]: F[K] extends TextField | RichTextField ? string : F[K] extends ToggleField ? boolean : F[K] extends LinkField ? CmsLink : F[K] extends ImageField<true> ? CmsImage : F[K] extends ImageField ? CmsImage | null : F[K] extends ListField<infer Item> ? CmsValue<Item>[] : F[K] extends CmsFields ? CmsValue<F[K]> : never; };
|
|
60
|
+
/** What a default or an edit may contain: a missing field keeps its default. */
|
|
61
|
+
export type InputOf<F extends CmsFields> = { [K in keyof F]?: F[K] extends TextField | RichTextField ? string : F[K] extends ToggleField ? boolean : F[K] extends LinkField ? CmsLink : F[K] extends ImageField ? StoredImage | string | null : F[K] extends ListField<infer Item> ? InputOf<Item>[] : F[K] extends CmsFields ? InputOf<F[K]> : never; };
|
|
62
|
+
export interface CmsPage<Key extends string = string, F extends CmsFields = CmsFields> {
|
|
63
|
+
readonly __cmsPage: true;
|
|
64
|
+
key: Key;
|
|
65
|
+
/** Shown in the editor instead of the key. */
|
|
66
|
+
label?: string;
|
|
67
|
+
/** Where the page is on the site, for the editor's link to it (`/`, `/menu`). */
|
|
68
|
+
path?: string;
|
|
69
|
+
fields: F;
|
|
70
|
+
}
|
|
71
|
+
type Options = {
|
|
72
|
+
label?: string;
|
|
73
|
+
};
|
|
74
|
+
export declare function text(value?: string, options?: Options & {
|
|
75
|
+
multiline?: boolean;
|
|
76
|
+
}): TextField;
|
|
77
|
+
/** Markdown (Comark). */
|
|
78
|
+
export declare function richText(value?: string, options?: Options): RichTextField;
|
|
79
|
+
export declare function toggle(value?: boolean, options?: Options): ToggleField;
|
|
80
|
+
export declare function link(value?: CmsLink, options?: Options): LinkField;
|
|
81
|
+
/** `image('/hero.jpg')` always has an image; `image()` is `null` until one is chosen. */
|
|
82
|
+
export declare function image(value: string | StoredImage, options?: Options): ImageField<true>;
|
|
83
|
+
export declare function image(value?: null, options?: Options): ImageField<false>;
|
|
84
|
+
/** Repeated groups of fields, such as team members or opening hours, in an order of their own. */
|
|
85
|
+
export declare function list<Item extends CmsFields>(item: Item, value?: InputOf<Item>[], options?: Options): ListField<Item>;
|
|
86
|
+
/**
|
|
87
|
+
* The content of one page, read with `useCms(key)`:
|
|
88
|
+
*
|
|
89
|
+
* ```ts
|
|
90
|
+
* // cms/home.ts
|
|
91
|
+
* export default defineCmsPage('home', {
|
|
92
|
+
* hero: { title: text('Welcome'), image: image('/hero.jpg') },
|
|
93
|
+
* hours: list({ day: text(), time: text() }, [{ day: 'Mon–Fri', time: '8–18' }]),
|
|
94
|
+
* })
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
export declare function defineCmsPage<const Key extends string, F extends CmsFields>(key: Key, fields: F, options?: Options & {
|
|
98
|
+
path?: string;
|
|
99
|
+
}): CmsPage<Key, F>;
|
|
100
|
+
export declare function isCmsPage(value: unknown): value is CmsPage;
|
|
101
|
+
/**
|
|
102
|
+
* Pages by key, filled in by the module from the app's `cms/*.ts`:
|
|
103
|
+
* `useCms('home')` is typed from it.
|
|
104
|
+
*/
|
|
105
|
+
export interface CmsPages {}
|
|
106
|
+
/** The pages a `cms/*.ts` module exports, by key. Used by the generated types. */
|
|
107
|
+
export type PagesOf<M> = { [P in M[keyof M] as P extends CmsPage<infer K> ? K : never]: P extends CmsPage<string, infer F> ? F : never; };
|
|
108
|
+
export {};
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// src/runtime/schema.ts
|
|
2
|
+
function text(value = "", options = {}) {
|
|
3
|
+
return {
|
|
4
|
+
type: "text",
|
|
5
|
+
default: value,
|
|
6
|
+
...options
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
/** Markdown (Comark). */
|
|
10
|
+
function richText(value = "", options = {}) {
|
|
11
|
+
return {
|
|
12
|
+
type: "richText",
|
|
13
|
+
default: value,
|
|
14
|
+
...options
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
function toggle(value = false, options = {}) {
|
|
18
|
+
return {
|
|
19
|
+
type: "toggle",
|
|
20
|
+
default: value,
|
|
21
|
+
...options
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
function link(value = {
|
|
25
|
+
label: "",
|
|
26
|
+
href: ""
|
|
27
|
+
}, options = {}) {
|
|
28
|
+
return {
|
|
29
|
+
type: "link",
|
|
30
|
+
default: value,
|
|
31
|
+
...options
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
function image(value, options = {}) {
|
|
35
|
+
const stored = typeof value === "string" ? { src: value } : value ?? null;
|
|
36
|
+
return {
|
|
37
|
+
type: "image",
|
|
38
|
+
default: stored,
|
|
39
|
+
required: stored !== null,
|
|
40
|
+
...options
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
/** Repeated groups of fields, such as team members or opening hours, in an order of their own. */
|
|
44
|
+
function list(item, value = [], options = {}) {
|
|
45
|
+
return {
|
|
46
|
+
type: "list",
|
|
47
|
+
item,
|
|
48
|
+
default: value,
|
|
49
|
+
...options
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* The content of one page, read with `useCms(key)`:
|
|
54
|
+
*
|
|
55
|
+
* ```ts
|
|
56
|
+
* // cms/home.ts
|
|
57
|
+
* export default defineCmsPage('home', {
|
|
58
|
+
* hero: { title: text('Welcome'), image: image('/hero.jpg') },
|
|
59
|
+
* hours: list({ day: text(), time: text() }, [{ day: 'Mon–Fri', time: '8–18' }]),
|
|
60
|
+
* })
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
function defineCmsPage(key, fields, options = {}) {
|
|
64
|
+
return {
|
|
65
|
+
__cmsPage: true,
|
|
66
|
+
key,
|
|
67
|
+
fields,
|
|
68
|
+
...options
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
function isCmsPage(value) {
|
|
72
|
+
return typeof value === "object" && value !== null && "__cmsPage" in value;
|
|
73
|
+
}
|
|
74
|
+
export { defineCmsPage, image, isCmsPage, link, list, richText, text, toggle };
|