uncial-cms 5.1.0 → 5.3.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/README.md +216 -23
- package/dist/astro/EditorSurface.svelte +6 -2
- package/dist/astro/EditorSurface.svelte.d.ts +3 -1
- package/dist/astro/index.d.ts +2 -1
- package/dist/astro/index.js +7 -2
- package/dist/content-sources.d.ts +10 -0
- package/dist/content-sources.js +25 -1
- package/dist/dashboard/ConfirmDialog.svelte +76 -0
- package/dist/dashboard/ConfirmDialog.svelte.d.ts +10 -0
- package/dist/dashboard/Dashboard.svelte +650 -0
- package/dist/dashboard/Dashboard.svelte.d.ts +4 -0
- package/dist/dashboard/FallbackEditor.svelte +42 -0
- package/dist/dashboard/FallbackEditor.svelte.d.ts +14 -0
- package/dist/dashboard/GlobalForm.svelte +405 -0
- package/dist/dashboard/GlobalForm.svelte.d.ts +17 -0
- package/dist/dashboard/GlobalsList.svelte +62 -0
- package/dist/dashboard/GlobalsList.svelte.d.ts +7 -0
- package/dist/dashboard/MediaSection.svelte +221 -0
- package/dist/dashboard/MediaSection.svelte.d.ts +16 -0
- package/dist/dashboard/PagesSection.svelte +477 -0
- package/dist/dashboard/PagesSection.svelte.d.ts +23 -0
- package/dist/dashboard/element.d.ts +41 -0
- package/dist/dashboard/element.js +113 -0
- package/dist/dashboard/pages-list.d.ts +18 -0
- package/dist/dashboard/pages-list.js +29 -0
- package/dist/dashboard/routes.d.ts +25 -0
- package/dist/dashboard/routes.js +81 -0
- package/dist/dashboard/types.d.ts +34 -0
- package/dist/dashboard/types.js +1 -0
- package/dist/define-site.d.ts +15 -6
- package/dist/define-site.js +12 -9
- package/dist/editor-controller.d.ts +1 -0
- package/dist/editor-controller.js +1 -0
- package/dist/editor-session.d.ts +5 -0
- package/dist/editor-session.js +1 -1
- package/dist/errors.d.ts +13 -0
- package/dist/errors.js +14 -0
- package/dist/github/broker.d.ts +26 -0
- package/dist/github/broker.js +196 -0
- package/dist/github/index.d.ts +1 -0
- package/dist/github/index.js +1 -0
- package/dist/globals.d.ts +12 -0
- package/dist/globals.js +29 -0
- package/dist/image-source.d.ts +0 -10
- package/dist/image-source.js +15 -44
- package/dist/index-actions.d.ts +29 -2
- package/dist/index-actions.js +36 -4
- package/dist/index-page.d.ts +2 -0
- package/dist/index-page.js +6 -256
- package/dist/index.d.ts +6 -3
- package/dist/index.js +4 -3
- package/dist/local/vite.d.ts +8 -2
- package/dist/local/vite.js +32 -15
- package/dist/media-source.d.ts +45 -0
- package/dist/media-source.js +113 -0
- package/dist/mount.d.ts +3 -0
- package/dist/mount.js +3 -0
- package/dist/nav/index.d.ts +39 -0
- package/dist/nav/index.js +52 -0
- package/dist/served-url.d.ts +6 -4
- package/dist/served-url.js +18 -7
- package/dist/server/handlers.d.ts +1 -1
- package/dist/server/handlers.js +32 -5
- package/dist/server/index.d.ts +2 -1
- package/dist/server/index.js +2 -1
- package/dist/server/media.d.ts +1 -10
- package/dist/server/media.js +7 -11
- package/dist/server/postgres-store.d.ts +5 -0
- package/dist/server/postgres-store.js +50 -5
- package/dist/server-forge/adapter.d.ts +1 -0
- package/dist/server-forge/adapter.js +7 -4
- package/dist/server-forge/protocol.d.ts +3 -1
- package/dist/server-forge/protocol.js +2 -1
- package/dist/svelte/EditorPage.svelte +5 -1
- package/dist/svelte/EditorPage.svelte.d.ts +3 -1
- package/dist/svelte/MediaLibrary.svelte +109 -53
- package/dist/svelte/MediaLibrary.svelte.d.ts +10 -2
- package/dist/svelte/index.d.ts +1 -4
- package/dist/svelte/index.js +1 -4
- package/dist/sveltekit/index.d.ts +11 -1
- package/dist/sveltekit/index.js +23 -9
- package/dist/types.d.ts +2 -0
- package/dist/upload-context.d.ts +1 -0
- package/dist/upload-context.js +3 -0
- package/dist/vite/index.js +18 -4
- package/package.json +16 -3
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export const PAGE_SIZE = 50;
|
|
2
|
+
const firstSegment = (pagePath) => pagePath.split('/')[0];
|
|
3
|
+
const sorted = (values) => [...new Set(values)].sort();
|
|
4
|
+
export function kindOptions(pages) {
|
|
5
|
+
return sorted(pages.flatMap((page) => page.kind ?? []));
|
|
6
|
+
}
|
|
7
|
+
export function folderOptions(pages) {
|
|
8
|
+
return sorted(pages.filter((page) => page.pagePath.includes('/')).map((page) => firstSegment(page.pagePath)));
|
|
9
|
+
}
|
|
10
|
+
/** `draft` and `published` match as the store's `list({ status })` does: a record with both matches both. */
|
|
11
|
+
function matchesStatus(page, status) {
|
|
12
|
+
if (!status)
|
|
13
|
+
return true;
|
|
14
|
+
return page.status !== (status === 'draft' ? 'published' : 'draft');
|
|
15
|
+
}
|
|
16
|
+
export function filterPages(pages, query) {
|
|
17
|
+
const needle = query.q?.trim().toLowerCase();
|
|
18
|
+
return pages.filter((page) => (!needle ||
|
|
19
|
+
[page.title, `/${page.pagePath}/`].some((text) => text?.toLowerCase().includes(needle))) &&
|
|
20
|
+
(!query.kind || page.kind === query.kind) &&
|
|
21
|
+
(!query.folder || firstSegment(page.pagePath) === query.folder) &&
|
|
22
|
+
matchesStatus(page, query.status));
|
|
23
|
+
}
|
|
24
|
+
export function pageWindow(total, page = 1) {
|
|
25
|
+
const pageCount = Math.max(1, Math.ceil(total / PAGE_SIZE));
|
|
26
|
+
const current = Math.min(Math.max(1, page), pageCount);
|
|
27
|
+
const start = (current - 1) * PAGE_SIZE;
|
|
28
|
+
return { page: current, pageCount, start, end: Math.min(start + PAGE_SIZE, total) };
|
|
29
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export type DashboardSection = 'pages' | 'media' | 'globals';
|
|
2
|
+
export interface PagesQuery {
|
|
3
|
+
q?: string;
|
|
4
|
+
kind?: string;
|
|
5
|
+
folder?: string;
|
|
6
|
+
status?: 'draft' | 'published';
|
|
7
|
+
page?: number;
|
|
8
|
+
}
|
|
9
|
+
export type DashboardRoute = {
|
|
10
|
+
section: 'pages';
|
|
11
|
+
query?: PagesQuery;
|
|
12
|
+
} | {
|
|
13
|
+
section: 'edit';
|
|
14
|
+
pagePath: string;
|
|
15
|
+
} | {
|
|
16
|
+
section: 'media';
|
|
17
|
+
} | {
|
|
18
|
+
section: 'globals';
|
|
19
|
+
name?: string;
|
|
20
|
+
} | {
|
|
21
|
+
section: 'app';
|
|
22
|
+
id: string;
|
|
23
|
+
};
|
|
24
|
+
export declare function routeFromHash(hash: string): DashboardRoute;
|
|
25
|
+
export declare function hashForRoute(route: DashboardRoute): string;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { GLOBAL_NAME, isGlobalsPath } from '../globals.js';
|
|
2
|
+
const PAGES = { section: 'pages' };
|
|
3
|
+
function editRoute(path) {
|
|
4
|
+
try {
|
|
5
|
+
const pagePath = decodeURIComponent(path);
|
|
6
|
+
if (isGlobalsPath(pagePath) || pagePath.split('/').some((part) => part === '.' || part === '..')) {
|
|
7
|
+
return PAGES;
|
|
8
|
+
}
|
|
9
|
+
return { section: 'edit', pagePath };
|
|
10
|
+
}
|
|
11
|
+
catch {
|
|
12
|
+
return PAGES;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
function pagesRoute(search) {
|
|
16
|
+
const params = new URLSearchParams(search);
|
|
17
|
+
const query = {};
|
|
18
|
+
for (const key of ['q', 'kind', 'folder']) {
|
|
19
|
+
const value = params.get(key);
|
|
20
|
+
if (value)
|
|
21
|
+
query[key] = value;
|
|
22
|
+
}
|
|
23
|
+
const status = params.get('status');
|
|
24
|
+
if (status === 'draft' || status === 'published')
|
|
25
|
+
query.status = status;
|
|
26
|
+
const page = Number(params.get('page'));
|
|
27
|
+
if (Number.isInteger(page) && page > 1)
|
|
28
|
+
query.page = page;
|
|
29
|
+
return { section: 'pages', query };
|
|
30
|
+
}
|
|
31
|
+
export function routeFromHash(hash) {
|
|
32
|
+
if (!hash.startsWith('#/'))
|
|
33
|
+
return PAGES;
|
|
34
|
+
const rest = hash.slice(2);
|
|
35
|
+
if (rest === 'pages' || rest.startsWith('pages?'))
|
|
36
|
+
return pagesRoute(rest.slice('pages'.length));
|
|
37
|
+
if (rest === 'media')
|
|
38
|
+
return { section: 'media' };
|
|
39
|
+
if (rest === 'globals')
|
|
40
|
+
return { section: 'globals' };
|
|
41
|
+
if (rest.startsWith('edit/'))
|
|
42
|
+
return editRoute(rest.slice('edit/'.length).replace(/\/+$/, ''));
|
|
43
|
+
const [head, name, ...more] = rest.split('/');
|
|
44
|
+
if (head === 'globals' && name && more.length === 0 && GLOBAL_NAME.test(name)) {
|
|
45
|
+
return { section: 'globals', name };
|
|
46
|
+
}
|
|
47
|
+
if (head === 'app' && name && more.length === 0) {
|
|
48
|
+
try {
|
|
49
|
+
return { section: 'app', id: decodeURIComponent(name) };
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
return PAGES;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
if (rest === '' || rest.endsWith('/'))
|
|
56
|
+
return editRoute(rest.replace(/\/+$/, ''));
|
|
57
|
+
return PAGES;
|
|
58
|
+
}
|
|
59
|
+
export function hashForRoute(route) {
|
|
60
|
+
switch (route.section) {
|
|
61
|
+
case 'edit':
|
|
62
|
+
return `#/edit/${route.pagePath.split('/').map(encodeURIComponent).join('/')}`;
|
|
63
|
+
case 'globals':
|
|
64
|
+
return route.name ? `#/globals/${route.name}` : '#/globals';
|
|
65
|
+
case 'app':
|
|
66
|
+
return `#/app/${encodeURIComponent(route.id)}`;
|
|
67
|
+
case 'pages': {
|
|
68
|
+
const { page, ...filters } = route.query ?? {};
|
|
69
|
+
const search = new URLSearchParams();
|
|
70
|
+
for (const [key, value] of Object.entries(filters))
|
|
71
|
+
if (value)
|
|
72
|
+
search.set(key, value);
|
|
73
|
+
if (page && page > 1)
|
|
74
|
+
search.set('page', String(page));
|
|
75
|
+
const query = search.toString();
|
|
76
|
+
return query ? `#/pages?${query}` : '#/pages';
|
|
77
|
+
}
|
|
78
|
+
default:
|
|
79
|
+
return `#/${route.section}`;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { AttributeInputs, ContentSchema } from 'uncial/core';
|
|
2
|
+
import type { Site } from '../define-site.js';
|
|
3
|
+
import type { Action } from '../server-forge/protocol.js';
|
|
4
|
+
import type { SessionProvider, UncialCmsSiteConfig } from '../types.js';
|
|
5
|
+
import type { DashboardSection } from './routes.js';
|
|
6
|
+
export type DashboardAction = Action;
|
|
7
|
+
export interface DashboardOptions {
|
|
8
|
+
config: UncialCmsSiteConfig | Site;
|
|
9
|
+
blocks: unknown;
|
|
10
|
+
schema: unknown;
|
|
11
|
+
globals?: Record<string, ContentSchema>;
|
|
12
|
+
editorHref?: (pagePath: string) => string;
|
|
13
|
+
can?: (action: DashboardAction, subject?: unknown) => boolean;
|
|
14
|
+
appSections?: Array<{
|
|
15
|
+
id: string;
|
|
16
|
+
label: string;
|
|
17
|
+
href?: string;
|
|
18
|
+
}>;
|
|
19
|
+
signOutHref?: string;
|
|
20
|
+
basePath?: string;
|
|
21
|
+
theme?: 'light' | 'dark' | 'system';
|
|
22
|
+
sessionProvider?: SessionProvider;
|
|
23
|
+
/** Site-relative page path → source path. Must match the one baked into the Editor variants. */
|
|
24
|
+
mapPathToSource?: (path: string) => string;
|
|
25
|
+
mapSourceToPath?: (source: string) => string;
|
|
26
|
+
editorStylesheets?: string[];
|
|
27
|
+
/** Repo-root-relative dir the build serves at the site root, as `cmsImageSource` takes it. Default 'static'. */
|
|
28
|
+
staticDir?: string;
|
|
29
|
+
/** Svelte components for host-defined attribute `input` kinds, in the Fallback editor. */
|
|
30
|
+
attributeInputs?: AttributeInputs;
|
|
31
|
+
}
|
|
32
|
+
export type DashboardProps = DashboardOptions & {
|
|
33
|
+
sections?: DashboardSection[];
|
|
34
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/define-site.d.ts
CHANGED
|
@@ -3,19 +3,25 @@
|
|
|
3
3
|
* build: the local forge while developing, the declared GitHub forge in a
|
|
4
4
|
* production build, and a local-only site when no GitHub half is declared.
|
|
5
5
|
*/
|
|
6
|
+
import type { ContentSchema } from 'uncial/core';
|
|
6
7
|
import { type DeployStatusTimings } from './deploy-status.js';
|
|
7
8
|
import type { GitHubSiteConfig, LocalSiteConfig, ServerSiteConfig, UncialCmsSiteConfig } from './types.js';
|
|
8
9
|
/** The auth worker this project operates for sites using the canonical GitHub App. */
|
|
9
10
|
export declare const DEFAULT_AUTH_WORKER_URL = "https://uncial-cms-auth.dflood.workers.dev";
|
|
10
11
|
/** The canonical GitHub App a site installs on its repository. */
|
|
11
12
|
export declare const DEFAULT_APP_SLUG = "uncial-cms";
|
|
12
|
-
export
|
|
13
|
+
export type Globals = Record<string, ContentSchema>;
|
|
14
|
+
export interface SiteOptions<G extends Globals = Globals> {
|
|
13
15
|
/** Repo-root-relative content directory, as the forge APIs address it. */
|
|
14
16
|
contentDir: string;
|
|
15
17
|
/** FS path of that directory at build time; defaults to `contentDir`. */
|
|
16
18
|
localContentDir?: string;
|
|
17
19
|
/** Repo-root-relative directory uploaded media commits into. */
|
|
18
20
|
mediaDir?: string;
|
|
21
|
+
/** FS path of `mediaDir` in development; defaults to where it sits beside `localContentDir`. */
|
|
22
|
+
localMediaDir?: string;
|
|
23
|
+
/** Site-root URL path `mediaDir` is served under, for media outside the static dir. */
|
|
24
|
+
mediaUrlPrefix?: string;
|
|
19
25
|
/** Omit for a local-only site: one with no forge to commit to. */
|
|
20
26
|
github?: {
|
|
21
27
|
repo: string;
|
|
@@ -31,24 +37,27 @@ export interface SiteOptions {
|
|
|
31
37
|
* ends on "status unknown". Each field falls back to its default.
|
|
32
38
|
*/
|
|
33
39
|
deployStatus?: Partial<DeployStatusTimings>;
|
|
40
|
+
globals?: G;
|
|
34
41
|
}
|
|
35
|
-
export interface Site<Config extends UncialCmsSiteConfig = UncialCmsSiteConfig> {
|
|
42
|
+
export interface Site<Config extends UncialCmsSiteConfig = UncialCmsSiteConfig, G extends Globals = Globals> {
|
|
36
43
|
config: Config;
|
|
37
44
|
/** No GitHub half was declared, so this site is only editable in development. */
|
|
38
45
|
localOnly: boolean;
|
|
39
46
|
autosaveMs: number | undefined;
|
|
40
47
|
localContentDir: string;
|
|
41
48
|
deployStatusTimings: DeployStatusTimings;
|
|
49
|
+
globals: G;
|
|
42
50
|
}
|
|
43
|
-
export declare function defineSite(options: SiteOptions
|
|
51
|
+
export declare function defineSite<G extends Globals = {}>(options: SiteOptions<G>, env?: {
|
|
44
52
|
dev: boolean;
|
|
45
|
-
}): Site<GitHubSiteConfig | LocalSiteConfig>;
|
|
46
|
-
export interface ServerSiteOptions {
|
|
53
|
+
}): Site<GitHubSiteConfig | LocalSiteConfig, G>;
|
|
54
|
+
export interface ServerSiteOptions<G extends Globals = Globals> {
|
|
47
55
|
/** The host endpoint serving `createServerContentHandlers`, e.g. '/dashboard/api/content'. */
|
|
48
56
|
apiBase: string;
|
|
49
57
|
mediaApiBase?: string;
|
|
50
58
|
/** Autosave writes only the Draft, so a server site may autosave. */
|
|
51
59
|
autosaveMs?: number;
|
|
60
|
+
globals?: G;
|
|
52
61
|
}
|
|
53
62
|
/** A site whose content lives behind the host's own endpoints, in every build. */
|
|
54
|
-
export declare function defineServerSite(options: ServerSiteOptions): Site<ServerSiteConfig>;
|
|
63
|
+
export declare function defineServerSite<G extends Globals = {}>(options: ServerSiteOptions<G>): Site<ServerSiteConfig, G>;
|
package/dist/define-site.js
CHANGED
|
@@ -1,8 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* One declaration of a site's CMS configuration, resolved for the current
|
|
3
|
-
* build: the local forge while developing, the declared GitHub forge in a
|
|
4
|
-
* production build, and a local-only site when no GitHub half is declared.
|
|
5
|
-
*/
|
|
6
1
|
import { DEFAULT_DEPLOY_STATUS_TIMINGS } from './deploy-status.js';
|
|
7
2
|
/** The auth worker this project operates for sites using the canonical GitHub App. */
|
|
8
3
|
export const DEFAULT_AUTH_WORKER_URL = 'https://uncial-cms-auth.dflood.workers.dev';
|
|
@@ -18,7 +13,12 @@ function resolveDeployStatusTimings(overrides = {}) {
|
|
|
18
13
|
export function defineSite(options, env = { dev: import.meta.env.DEV }) {
|
|
19
14
|
const localOnly = options.github === undefined;
|
|
20
15
|
const config = env.dev || options.github === undefined
|
|
21
|
-
? {
|
|
16
|
+
? {
|
|
17
|
+
forge: 'local',
|
|
18
|
+
contentDir: options.contentDir,
|
|
19
|
+
mediaDir: options.mediaDir,
|
|
20
|
+
mediaUrlPrefix: options.mediaUrlPrefix
|
|
21
|
+
}
|
|
22
22
|
: {
|
|
23
23
|
forge: 'github',
|
|
24
24
|
repo: options.github.repo,
|
|
@@ -26,14 +26,16 @@ export function defineSite(options, env = { dev: import.meta.env.DEV }) {
|
|
|
26
26
|
contentDir: options.contentDir,
|
|
27
27
|
authWorkerUrl: options.github.authWorkerUrl ?? DEFAULT_AUTH_WORKER_URL,
|
|
28
28
|
appSlug: options.github.appSlug ?? DEFAULT_APP_SLUG,
|
|
29
|
-
mediaDir: options.mediaDir
|
|
29
|
+
mediaDir: options.mediaDir,
|
|
30
|
+
mediaUrlPrefix: options.mediaUrlPrefix
|
|
30
31
|
};
|
|
31
32
|
return {
|
|
32
33
|
config,
|
|
33
34
|
localOnly,
|
|
34
35
|
autosaveMs: config.forge === 'local' ? options.autosaveMs : undefined,
|
|
35
36
|
localContentDir: options.localContentDir ?? options.contentDir,
|
|
36
|
-
deployStatusTimings: resolveDeployStatusTimings(options.deployStatus)
|
|
37
|
+
deployStatusTimings: resolveDeployStatusTimings(options.deployStatus),
|
|
38
|
+
globals: options.globals ?? {}
|
|
37
39
|
};
|
|
38
40
|
}
|
|
39
41
|
/** A site whose content lives behind the host's own endpoints, in every build. */
|
|
@@ -43,6 +45,7 @@ export function defineServerSite(options) {
|
|
|
43
45
|
localOnly: false,
|
|
44
46
|
autosaveMs: options.autosaveMs,
|
|
45
47
|
localContentDir: '',
|
|
46
|
-
deployStatusTimings: resolveDeployStatusTimings()
|
|
48
|
+
deployStatusTimings: resolveDeployStatusTimings(),
|
|
49
|
+
globals: options.globals ?? {}
|
|
47
50
|
};
|
|
48
51
|
}
|
|
@@ -40,6 +40,7 @@ export interface EditorPageUi {
|
|
|
40
40
|
record?(state: RecordState): void;
|
|
41
41
|
/** The record moved to a new path; this controller no longer addresses it. */
|
|
42
42
|
moved?(path: string): void;
|
|
43
|
+
failed?(error: unknown): void;
|
|
43
44
|
}
|
|
44
45
|
export interface EditorControllerOptions {
|
|
45
46
|
config: UncialCmsSiteConfig;
|
|
@@ -43,6 +43,7 @@ export function createEditorController(opts) {
|
|
|
43
43
|
showRecord({ draft: view.draft !== null, published: view.published !== null, allowed: view.allowed });
|
|
44
44
|
};
|
|
45
45
|
const showFailure = (error, fallback) => {
|
|
46
|
+
ui.failed?.(error);
|
|
46
47
|
if (error instanceof ConflictError) {
|
|
47
48
|
// Do NOT touch content or dirty state: the unsaved edit must survive.
|
|
48
49
|
ui.conflictVisible(true);
|
package/dist/editor-session.d.ts
CHANGED
|
@@ -65,4 +65,9 @@ export interface CreateEditorSessionOptions {
|
|
|
65
65
|
}
|
|
66
66
|
export declare function forgeAdapter(config: UncialCmsSiteConfig): ForgeAdapter;
|
|
67
67
|
export declare function defaultSessionProvider(config: UncialCmsSiteConfig): SessionProvider;
|
|
68
|
+
export declare function triggerDownload(payload: {
|
|
69
|
+
filename: string;
|
|
70
|
+
content: string;
|
|
71
|
+
mimeType: string;
|
|
72
|
+
}): void;
|
|
68
73
|
export declare function createEditorSession(opts: CreateEditorSessionOptions): EditorController;
|
package/dist/editor-session.js
CHANGED
|
@@ -42,7 +42,7 @@ export function defaultSessionProvider(config) {
|
|
|
42
42
|
return serverSessionProvider;
|
|
43
43
|
return popupSessionProvider;
|
|
44
44
|
}
|
|
45
|
-
function triggerDownload(payload) {
|
|
45
|
+
export function triggerDownload(payload) {
|
|
46
46
|
const blob = new Blob([payload.content], { type: payload.mimeType });
|
|
47
47
|
const url = URL.createObjectURL(blob);
|
|
48
48
|
const anchor = document.createElement('a');
|
package/dist/errors.d.ts
CHANGED
|
@@ -6,3 +6,16 @@ export declare class ConflictError extends Error {
|
|
|
6
6
|
export declare class NotFoundError extends Error {
|
|
7
7
|
constructor(message?: string);
|
|
8
8
|
}
|
|
9
|
+
export declare class MediaInUseError extends Error {
|
|
10
|
+
readonly usage: {
|
|
11
|
+
count: number;
|
|
12
|
+
paths: string[];
|
|
13
|
+
};
|
|
14
|
+
constructor(usage: {
|
|
15
|
+
count: number;
|
|
16
|
+
paths: string[];
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
export declare class SignedOutError extends Error {
|
|
20
|
+
constructor(message?: string);
|
|
21
|
+
}
|
package/dist/errors.js
CHANGED
|
@@ -12,3 +12,17 @@ export class NotFoundError extends Error {
|
|
|
12
12
|
this.name = 'NotFoundError';
|
|
13
13
|
}
|
|
14
14
|
}
|
|
15
|
+
export class MediaInUseError extends Error {
|
|
16
|
+
usage;
|
|
17
|
+
constructor(usage) {
|
|
18
|
+
super(`This Media item is used by ${usage.count} Content document${usage.count === 1 ? '' : 's'}: ${usage.paths.join(', ')}.`);
|
|
19
|
+
this.usage = usage;
|
|
20
|
+
this.name = 'MediaInUseError';
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
export class SignedOutError extends Error {
|
|
24
|
+
constructor(message = 'You are signed out. Sign in again to keep editing.') {
|
|
25
|
+
super(message);
|
|
26
|
+
this.name = 'SignedOutError';
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { SessionProvider } from '../types.js';
|
|
2
|
+
export interface BrokerSessionOptions {
|
|
3
|
+
/** Origin of a code-exchange broker serving `POST /github/token` and `/github/refresh`. */
|
|
4
|
+
brokerOrigin: string;
|
|
5
|
+
/** The GitHub App's client id, registered with the broker. */
|
|
6
|
+
clientId: string;
|
|
7
|
+
/** The GitHub App's slug, named when the App cannot reach the repository. */
|
|
8
|
+
appSlug: string;
|
|
9
|
+
/** The site's base path; the callback is the Dashboard at `<basePath>/uncial/`. */
|
|
10
|
+
basePath?: string;
|
|
11
|
+
}
|
|
12
|
+
/** Forget the refresh token kept for `repo`, so the next sign-in asks GitHub again. */
|
|
13
|
+
export declare function forgetBrokerGrant(repo: string): void;
|
|
14
|
+
/**
|
|
15
|
+
* Called by the Dashboard as it loads: in the sign-in popup GitHub returns to
|
|
16
|
+
* the Dashboard with `code` and `state`, which go back to the opener here.
|
|
17
|
+
* True when this page load is that callback, so the Dashboard renders nothing else.
|
|
18
|
+
*/
|
|
19
|
+
export declare function relayBrokerCallback(): boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Signs in through a code-exchange broker holding a GitHub App's client
|
|
22
|
+
* secret. The user-to-server token it returns is the forge's bearer, so GitHub
|
|
23
|
+
* itself decides who may push. A refresh token, when the App issues one, is
|
|
24
|
+
* kept in `localStorage` and renews the session without the popup.
|
|
25
|
+
*/
|
|
26
|
+
export declare function brokerSessionProvider(options: BrokerSessionOptions): SessionProvider;
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import { GITHUB_API_URL } from './adapter.js';
|
|
2
|
+
const STATE_KEY = 'uncial-cms:broker-state';
|
|
3
|
+
const RELAY_SOURCE = 'uncial-cms-broker';
|
|
4
|
+
const refreshKey = (repo) => `uncial-cms:broker-refresh:${repo}`;
|
|
5
|
+
function readRefresh(repo) {
|
|
6
|
+
try {
|
|
7
|
+
const grant = JSON.parse(localStorage.getItem(refreshKey(repo)) ?? 'null');
|
|
8
|
+
if (!grant?.refreshToken)
|
|
9
|
+
return null;
|
|
10
|
+
return grant.refreshExpiresAt === null || grant.refreshExpiresAt > Date.now() ? grant : null;
|
|
11
|
+
}
|
|
12
|
+
catch {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
function writeRefresh(repo, grant) {
|
|
17
|
+
try {
|
|
18
|
+
if (grant.refreshToken)
|
|
19
|
+
localStorage.setItem(refreshKey(repo), JSON.stringify(grant));
|
|
20
|
+
else
|
|
21
|
+
localStorage.removeItem(refreshKey(repo));
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
// Storage blocked: the next sign-in falls back to the popup.
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/** Forget the refresh token kept for `repo`, so the next sign-in asks GitHub again. */
|
|
28
|
+
export function forgetBrokerGrant(repo) {
|
|
29
|
+
try {
|
|
30
|
+
localStorage.removeItem(refreshKey(repo));
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
// Storage blocked: nothing was kept.
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Called by the Dashboard as it loads: in the sign-in popup GitHub returns to
|
|
38
|
+
* the Dashboard with `code` and `state`, which go back to the opener here.
|
|
39
|
+
* True when this page load is that callback, so the Dashboard renders nothing else.
|
|
40
|
+
*/
|
|
41
|
+
export function relayBrokerCallback() {
|
|
42
|
+
if (typeof window === 'undefined')
|
|
43
|
+
return false;
|
|
44
|
+
const params = new URLSearchParams(window.location.search);
|
|
45
|
+
const state = params.get('state');
|
|
46
|
+
if (!state || (!params.has('code') && !params.has('error')))
|
|
47
|
+
return false;
|
|
48
|
+
const relay = {
|
|
49
|
+
source: RELAY_SOURCE,
|
|
50
|
+
state,
|
|
51
|
+
code: params.get('code') ?? '',
|
|
52
|
+
error: params.get('error') ?? '',
|
|
53
|
+
errorDescription: params.get('error_description') ?? ''
|
|
54
|
+
};
|
|
55
|
+
if (window.opener) {
|
|
56
|
+
window.opener.postMessage(relay, window.location.origin);
|
|
57
|
+
window.close();
|
|
58
|
+
}
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
function waitForRelay(popup) {
|
|
62
|
+
return new Promise((resolve, reject) => {
|
|
63
|
+
const cleanup = () => {
|
|
64
|
+
window.removeEventListener('message', onMessage);
|
|
65
|
+
clearInterval(closedPoll);
|
|
66
|
+
};
|
|
67
|
+
const onMessage = (event) => {
|
|
68
|
+
if (event.origin !== window.location.origin)
|
|
69
|
+
return;
|
|
70
|
+
const data = event.data;
|
|
71
|
+
// GitHub Apps' web flow has no PKCE; `state` is the only CSRF check.
|
|
72
|
+
if (data?.source !== RELAY_SOURCE || data.state !== sessionStorage.getItem(STATE_KEY))
|
|
73
|
+
return;
|
|
74
|
+
cleanup();
|
|
75
|
+
if (data.error === 'access_denied')
|
|
76
|
+
reject(new Error('Sign-in was cancelled on GitHub.'));
|
|
77
|
+
else if (data.error)
|
|
78
|
+
reject(new Error(`GitHub refused the sign-in: ${data.errorDescription || data.error}.`));
|
|
79
|
+
else if (!data.code)
|
|
80
|
+
reject(new Error('GitHub returned no authorization code.'));
|
|
81
|
+
else
|
|
82
|
+
resolve(data.code);
|
|
83
|
+
};
|
|
84
|
+
const closedPoll = setInterval(() => {
|
|
85
|
+
if (!popup.closed)
|
|
86
|
+
return;
|
|
87
|
+
cleanup();
|
|
88
|
+
reject(new Error('The sign-in popup was closed before completing.'));
|
|
89
|
+
}, 500);
|
|
90
|
+
window.addEventListener('message', onMessage);
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
async function askBroker(options, path, body) {
|
|
94
|
+
let response;
|
|
95
|
+
try {
|
|
96
|
+
response = await fetch(`${options.brokerOrigin.replace(/\/+$/, '')}${path}`, {
|
|
97
|
+
method: 'POST',
|
|
98
|
+
headers: { 'Content-Type': 'application/json', Accept: 'application/json' },
|
|
99
|
+
body: JSON.stringify({ client_id: options.clientId, ...body })
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
catch {
|
|
103
|
+
throw new Error('The GitHub sign-in service could not be reached.');
|
|
104
|
+
}
|
|
105
|
+
const answer = (await response.json().catch(() => ({})));
|
|
106
|
+
// GitHub reports a refusal as `{ error, error_description }` with a 200.
|
|
107
|
+
if (typeof answer.error === 'string' && answer.error) {
|
|
108
|
+
const detail = typeof answer.error_description === 'string' ? answer.error_description : '';
|
|
109
|
+
throw new Error(`GitHub refused the sign-in: ${detail || answer.error}.`);
|
|
110
|
+
}
|
|
111
|
+
if (!response.ok || typeof answer.access_token !== 'string' || !answer.access_token) {
|
|
112
|
+
throw new Error(`The GitHub sign-in service sent an unexpected reply (${response.status}).`);
|
|
113
|
+
}
|
|
114
|
+
const now = Date.now();
|
|
115
|
+
const after = (seconds) => (typeof seconds === 'number' ? now + seconds * 1000 : null);
|
|
116
|
+
return {
|
|
117
|
+
token: answer.access_token,
|
|
118
|
+
expiresAt: after(answer.expires_in),
|
|
119
|
+
refreshToken: typeof answer.refresh_token === 'string' ? answer.refresh_token : '',
|
|
120
|
+
refreshExpiresAt: after(answer.refresh_token_expires_in)
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
async function signInInPopup(options) {
|
|
124
|
+
const redirectUri = `${window.location.origin}${(options.basePath ?? '').replace(/\/+$/, '')}/uncial/`;
|
|
125
|
+
const state = crypto.randomUUID();
|
|
126
|
+
sessionStorage.setItem(STATE_KEY, state);
|
|
127
|
+
const authorize = new URL('https://github.com/login/oauth/authorize');
|
|
128
|
+
authorize.searchParams.set('client_id', options.clientId);
|
|
129
|
+
authorize.searchParams.set('redirect_uri', redirectUri);
|
|
130
|
+
authorize.searchParams.set('state', state);
|
|
131
|
+
const popup = window.open(authorize, 'uncial-cms-broker', 'popup,width=640,height=760');
|
|
132
|
+
if (!popup) {
|
|
133
|
+
sessionStorage.removeItem(STATE_KEY);
|
|
134
|
+
throw new Error('The sign-in popup was blocked; allow popups for this site.');
|
|
135
|
+
}
|
|
136
|
+
try {
|
|
137
|
+
const code = await waitForRelay(popup);
|
|
138
|
+
return await askBroker(options, '/github/token', { code, redirect_uri: redirectUri });
|
|
139
|
+
}
|
|
140
|
+
finally {
|
|
141
|
+
sessionStorage.removeItem(STATE_KEY);
|
|
142
|
+
if (!popup.closed)
|
|
143
|
+
popup.close();
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
async function github(token, path) {
|
|
147
|
+
return fetch(`${GITHUB_API_URL}${path}`, {
|
|
148
|
+
headers: {
|
|
149
|
+
Accept: 'application/vnd.github+json',
|
|
150
|
+
Authorization: `Bearer ${token}`,
|
|
151
|
+
'X-GitHub-Api-Version': '2022-11-28'
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
async function readUser(token) {
|
|
156
|
+
const response = await github(token, '/user');
|
|
157
|
+
if (!response.ok)
|
|
158
|
+
throw new Error(`GitHub rejected the sign-in (${response.status}).`);
|
|
159
|
+
const user = (await response.json());
|
|
160
|
+
return {
|
|
161
|
+
login: user.login,
|
|
162
|
+
name: user.name ?? user.login,
|
|
163
|
+
email: `${user.id}+${user.login}@users.noreply.github.com`
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
async function assertReachable(token, repo, appSlug) {
|
|
167
|
+
const response = await github(token, `/repos/${repo}`);
|
|
168
|
+
if (response.status === 404) {
|
|
169
|
+
throw new Error(`The ${appSlug} GitHub App cannot reach ${repo} for this account. Install it at https://github.com/apps/${appSlug}/installations/new, or ask for access to the repository.`);
|
|
170
|
+
}
|
|
171
|
+
if (!response.ok)
|
|
172
|
+
throw new Error(`GitHub rejected the sign-in (${response.status}).`);
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Signs in through a code-exchange broker holding a GitHub App's client
|
|
176
|
+
* secret. The user-to-server token it returns is the forge's bearer, so GitHub
|
|
177
|
+
* itself decides who may push. A refresh token, when the App issues one, is
|
|
178
|
+
* kept in `localStorage` and renews the session without the popup.
|
|
179
|
+
*/
|
|
180
|
+
export function brokerSessionProvider(options) {
|
|
181
|
+
return async (config) => {
|
|
182
|
+
const { repo } = config;
|
|
183
|
+
const remembered = readRefresh(repo);
|
|
184
|
+
let grant = null;
|
|
185
|
+
if (remembered) {
|
|
186
|
+
grant = await askBroker(options, '/github/refresh', {
|
|
187
|
+
refresh_token: remembered.refreshToken
|
|
188
|
+
}).catch(() => null);
|
|
189
|
+
}
|
|
190
|
+
grant ??= await signInInPopup(options);
|
|
191
|
+
writeRefresh(repo, grant);
|
|
192
|
+
const user = await readUser(grant.token);
|
|
193
|
+
await assertReachable(grant.token, repo, options.appSlug);
|
|
194
|
+
return { token: grant.token, expiresAt: grant.expiresAt, repo, user };
|
|
195
|
+
};
|
|
196
|
+
}
|
package/dist/github/index.d.ts
CHANGED
package/dist/github/index.js
CHANGED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ContentDocument, ContentSchema } from 'uncial/core';
|
|
2
|
+
import type { Site } from './define-site.js';
|
|
3
|
+
export declare const GLOBALS_DIR = "_globals";
|
|
4
|
+
export declare const GLOBAL_KIND = "global";
|
|
5
|
+
export type GlobalValue<S extends Site, N extends keyof S['globals']> = S['globals'][N] extends ContentSchema<infer Meta> ? Meta : never;
|
|
6
|
+
export declare const GLOBAL_NAME: RegExp;
|
|
7
|
+
export declare function globalSourcePath(contentDir: string, name: string): string;
|
|
8
|
+
export declare function globalRecordPath(name: string): string;
|
|
9
|
+
export declare function isGlobalsPath(path: string): boolean;
|
|
10
|
+
export declare function assertPagePath(path: string): void;
|
|
11
|
+
export declare function globalTitle(schema: ContentSchema): string | undefined;
|
|
12
|
+
export declare function globalDocument(value: Record<string, unknown>): ContentDocument;
|
package/dist/globals.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export const GLOBALS_DIR = '_globals';
|
|
2
|
+
export const GLOBAL_KIND = 'global';
|
|
3
|
+
export const GLOBAL_NAME = /^[A-Za-z0-9_-]+$/;
|
|
4
|
+
function checkName(name) {
|
|
5
|
+
if (!GLOBAL_NAME.test(name)) {
|
|
6
|
+
throw new Error(`"${name}" is not a Global name: use letters, digits, "-" and "_".`);
|
|
7
|
+
}
|
|
8
|
+
return name;
|
|
9
|
+
}
|
|
10
|
+
export function globalSourcePath(contentDir, name) {
|
|
11
|
+
return `${contentDir}/${GLOBALS_DIR}/${checkName(name)}.json`;
|
|
12
|
+
}
|
|
13
|
+
export function globalRecordPath(name) {
|
|
14
|
+
return `/${GLOBALS_DIR}/${checkName(name)}/`;
|
|
15
|
+
}
|
|
16
|
+
export function isGlobalsPath(path) {
|
|
17
|
+
return path.replace(/^\/+/, '').split('/')[0] === GLOBALS_DIR;
|
|
18
|
+
}
|
|
19
|
+
export function assertPagePath(path) {
|
|
20
|
+
if (isGlobalsPath(path))
|
|
21
|
+
throw new Error('The _globals namespace is reserved for Globals.');
|
|
22
|
+
}
|
|
23
|
+
export function globalTitle(schema) {
|
|
24
|
+
const title = schema.title;
|
|
25
|
+
return typeof title === 'string' && title !== '' ? title : undefined;
|
|
26
|
+
}
|
|
27
|
+
export function globalDocument(value) {
|
|
28
|
+
return { type: 'doc', content: [], meta: value };
|
|
29
|
+
}
|
package/dist/image-source.d.ts
CHANGED
|
@@ -1,15 +1,5 @@
|
|
|
1
1
|
import type { ImageSource } from 'uncial/editor';
|
|
2
2
|
import type { UncialCmsSiteConfig } from './types.js';
|
|
3
|
-
/**
|
|
4
|
-
* The editor's image source for a CMS site: an upload is fitted under the
|
|
5
|
-
* forge limit, committed under the media dir through the active editor
|
|
6
|
-
* session, and stored as its base-less served URL; browse lists the media
|
|
7
|
-
* dir's images the same way. `base` and `staticDir` are the site's build-time
|
|
8
|
-
* values, which the config does not carry. A site with a rendition pipeline
|
|
9
|
-
* replaces the listing with `list` (served paths) and the tile URL with
|
|
10
|
-
* `thumbnail`. On a `server` forge, upload and browse go to the Media
|
|
11
|
-
* library at `mediaApiBase` instead.
|
|
12
|
-
*/
|
|
13
3
|
export declare function cmsImageSource(config: UncialCmsSiteConfig, options?: {
|
|
14
4
|
base?: string;
|
|
15
5
|
staticDir?: string;
|