@epekta/armarius-astro 0.0.0-stage → 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/README.md +272 -2
- package/bin/armarius-astro.js +4 -0
- package/components/EditionBoot.astro +62 -0
- package/components/Image.astro +20 -0
- package/components/Text.astro +23 -0
- package/dist/api.d.ts +18 -0
- package/dist/api.js +43 -0
- package/dist/check.d.ts +11 -0
- package/dist/check.js +64 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +13 -0
- package/dist/hash.d.ts +5 -0
- package/dist/hash.js +8 -0
- package/dist/images.d.ts +18 -0
- package/dist/images.js +35 -0
- package/dist/import/command.d.ts +7 -0
- package/dist/import/command.js +255 -0
- package/dist/import/format.d.ts +53 -0
- package/dist/import/format.js +94 -0
- package/dist/import/webservice.d.ts +47 -0
- package/dist/import/webservice.js +218 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +2 -0
- package/dist/integration.d.ts +7 -0
- package/dist/integration.js +75 -0
- package/dist/options.d.ts +39 -0
- package/dist/options.js +44 -0
- package/dist/readers.d.ts +69 -0
- package/dist/readers.js +106 -0
- package/dist/render.d.ts +17 -0
- package/dist/render.js +26 -0
- package/dist/routes.d.ts +36 -0
- package/dist/routes.js +71 -0
- package/dist/runtime.d.ts +46 -0
- package/dist/runtime.js +132 -0
- package/dist/state.d.ts +38 -0
- package/dist/state.js +35 -0
- package/dist/sync.d.ts +55 -0
- package/dist/sync.js +70 -0
- package/package.json +57 -5
package/dist/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { AstroIntegration } from 'astro';
|
|
2
|
+
import { type ArmariusOptions } from './options.js';
|
|
3
|
+
/**
|
|
4
|
+
* The Armarius integration of a static Astro site: reads Armarius at build, writes the sync bodies and the
|
|
5
|
+
* manifest under `.armarius/`, copies the edit layer into the output and prints the texts in conflict.
|
|
6
|
+
*/
|
|
7
|
+
export declare function armarius(options: ArmariusOptions): AstroIntegration;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { mkdir, readFile, writeFile } from 'node:fs/promises';
|
|
2
|
+
import { createRequire } from 'node:module';
|
|
3
|
+
import { dirname } from 'node:path';
|
|
4
|
+
import { fileURLToPath } from 'node:url';
|
|
5
|
+
import { conflictReport, conflicts, MANIFEST_PATH } from './check.js';
|
|
6
|
+
import { resolveOptions } from './options.js';
|
|
7
|
+
import { storedContents } from './readers.js';
|
|
8
|
+
import { kitState, LAYER_PATH, resetBuildState } from './state.js';
|
|
9
|
+
import { ArmariusSyncError, buildManifest, syncBodies } from './sync.js';
|
|
10
|
+
function editionLayerFile() {
|
|
11
|
+
return createRequire(import.meta.url).resolve('@epekta/armarius-edition/layer');
|
|
12
|
+
}
|
|
13
|
+
async function writeJson(file, value) {
|
|
14
|
+
await mkdir(dirname(fileURLToPath(file)), { recursive: true });
|
|
15
|
+
await writeFile(file, `${JSON.stringify(value, null, '\t')}\n`);
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* The Armarius integration of a static Astro site: reads Armarius at build, writes the sync bodies and the
|
|
19
|
+
* manifest under `.armarius/`, copies the edit layer into the output and prints the texts in conflict.
|
|
20
|
+
*/
|
|
21
|
+
export function armarius(options) {
|
|
22
|
+
const resolved = resolveOptions(options);
|
|
23
|
+
let root;
|
|
24
|
+
return {
|
|
25
|
+
name: '@epekta/armarius-astro',
|
|
26
|
+
hooks: {
|
|
27
|
+
'astro:config:setup': ({ config, command }) => {
|
|
28
|
+
const state = kitState();
|
|
29
|
+
state.options = resolved;
|
|
30
|
+
state.command = command;
|
|
31
|
+
state.base = config.base.endsWith('/') ? config.base : `${config.base}/`;
|
|
32
|
+
},
|
|
33
|
+
'astro:config:done': ({ config }) => {
|
|
34
|
+
root = config.root;
|
|
35
|
+
},
|
|
36
|
+
'astro:server:setup': ({ server }) => {
|
|
37
|
+
const layerUrl = `${kitState().base}${LAYER_PATH}`;
|
|
38
|
+
server.middlewares.use((request, response, next) => {
|
|
39
|
+
if (request.url?.split('?')[0] !== layerUrl)
|
|
40
|
+
return next();
|
|
41
|
+
readFile(editionLayerFile())
|
|
42
|
+
.then((layer) => {
|
|
43
|
+
response.setHeader('Content-Type', 'text/javascript; charset=utf-8');
|
|
44
|
+
response.end(layer);
|
|
45
|
+
})
|
|
46
|
+
.catch(next);
|
|
47
|
+
});
|
|
48
|
+
},
|
|
49
|
+
'astro:build:start': () => {
|
|
50
|
+
resetBuildState();
|
|
51
|
+
kitState().command = 'build';
|
|
52
|
+
},
|
|
53
|
+
'astro:build:done': async ({ dir, logger }) => {
|
|
54
|
+
const state = kitState();
|
|
55
|
+
// No path resolved: the pages did not share the integration's state, and the bodies would be empty.
|
|
56
|
+
if (state.paths.size === 0) {
|
|
57
|
+
throw new ArmariusSyncError('armarius-astro: the build resolved no path of Armarius; no sync body is written.');
|
|
58
|
+
}
|
|
59
|
+
const contents = [...state.contents.values()];
|
|
60
|
+
const bodies = syncBodies(resolved.pages, contents);
|
|
61
|
+
const manifest = buildManifest(resolved, contents, new Date());
|
|
62
|
+
await writeJson(new URL('.armarius/sync/pages.json', root), bodies.pages);
|
|
63
|
+
await writeJson(new URL('.armarius/sync/contents.json', root), bodies.contents);
|
|
64
|
+
await writeJson(new URL('.armarius/sync/page-contents.json', root), bodies.pageContents);
|
|
65
|
+
await writeJson(new URL(MANIFEST_PATH, root), manifest);
|
|
66
|
+
const layerTarget = new URL(LAYER_PATH, dir);
|
|
67
|
+
await mkdir(dirname(fileURLToPath(layerTarget)), { recursive: true });
|
|
68
|
+
await writeFile(layerTarget, await readFile(editionLayerFile()));
|
|
69
|
+
const report = conflictReport(conflicts(manifest, await storedContents()));
|
|
70
|
+
for (const line of report)
|
|
71
|
+
(report.length > 1 ? logger.warn : logger.info).call(logger, line);
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/** A page of the site's code: Armarius creates it at the first sync, then owns its slug, title and SEO. */
|
|
2
|
+
export interface ArmariusPage {
|
|
3
|
+
/** Stable name of the page in Armarius (`route_name`), also its `template`. */
|
|
4
|
+
route_name: string;
|
|
5
|
+
/** Default address, without locale prefix: `/` for the home page, `projets` or `/projets` otherwise. */
|
|
6
|
+
slug: string;
|
|
7
|
+
title: string;
|
|
8
|
+
/** Free value the site's catch-all route reads to pick the view it renders. */
|
|
9
|
+
view?: string;
|
|
10
|
+
/** Slug of the CRUD whose records each get a page under this one (`<slug>/<permalink>`). */
|
|
11
|
+
crud?: string;
|
|
12
|
+
}
|
|
13
|
+
export interface ArmariusOptions {
|
|
14
|
+
/** Origin of the Armarius API (`https://app.example.com`); a path is ignored. */
|
|
15
|
+
apiUrl: string;
|
|
16
|
+
/** Locale the site is built and synced in. */
|
|
17
|
+
locale: string;
|
|
18
|
+
pages: ArmariusPage[];
|
|
19
|
+
/** Origin of the admin screen that opens the site in its preview; the API's origin by default. */
|
|
20
|
+
screenOrigin?: string;
|
|
21
|
+
/** Delay of one request to the API, in milliseconds (10 000 by default). */
|
|
22
|
+
timeoutMs?: number;
|
|
23
|
+
/** Attempts after the first one when the API does not answer (2 by default). */
|
|
24
|
+
retries?: number;
|
|
25
|
+
}
|
|
26
|
+
export interface ResolvedOptions {
|
|
27
|
+
apiOrigin: string;
|
|
28
|
+
locale: string;
|
|
29
|
+
pages: ArmariusPage[];
|
|
30
|
+
screenOrigin: string;
|
|
31
|
+
timeoutMs: number;
|
|
32
|
+
retries: number;
|
|
33
|
+
}
|
|
34
|
+
export declare class ArmariusConfigError extends Error {
|
|
35
|
+
name: string;
|
|
36
|
+
}
|
|
37
|
+
export declare function httpOrigin(value: string, label: string): string;
|
|
38
|
+
export declare function normalizeSlug(slug: string): string;
|
|
39
|
+
export declare function resolveOptions(options: ArmariusOptions): ResolvedOptions;
|
package/dist/options.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
export class ArmariusConfigError extends Error {
|
|
2
|
+
name = 'ArmariusConfigError';
|
|
3
|
+
}
|
|
4
|
+
export function httpOrigin(value, label) {
|
|
5
|
+
let url;
|
|
6
|
+
try {
|
|
7
|
+
url = new URL(value);
|
|
8
|
+
}
|
|
9
|
+
catch {
|
|
10
|
+
throw new ArmariusConfigError(`armarius-astro: ${label} "${value}" is not an address.`);
|
|
11
|
+
}
|
|
12
|
+
if (url.protocol !== 'http:' && url.protocol !== 'https:') {
|
|
13
|
+
throw new ArmariusConfigError(`armarius-astro: ${label} "${value}" is not an http(s) address.`);
|
|
14
|
+
}
|
|
15
|
+
return url.origin;
|
|
16
|
+
}
|
|
17
|
+
export function normalizeSlug(slug) {
|
|
18
|
+
const trimmed = slug.trim().replace(/^\/+|\/+$/g, '');
|
|
19
|
+
return trimmed === '' ? '/' : trimmed;
|
|
20
|
+
}
|
|
21
|
+
export function resolveOptions(options) {
|
|
22
|
+
const apiOrigin = httpOrigin(options.apiUrl, 'apiUrl');
|
|
23
|
+
if (options.locale.trim() === '')
|
|
24
|
+
throw new ArmariusConfigError('armarius-astro: locale is empty.');
|
|
25
|
+
if (options.pages.length === 0)
|
|
26
|
+
throw new ArmariusConfigError('armarius-astro: pages is empty.');
|
|
27
|
+
const routeNames = new Set();
|
|
28
|
+
const pages = options.pages.map((page) => {
|
|
29
|
+
if (page.route_name.trim() === '')
|
|
30
|
+
throw new ArmariusConfigError('armarius-astro: a page has an empty route_name.');
|
|
31
|
+
if (routeNames.has(page.route_name))
|
|
32
|
+
throw new ArmariusConfigError(`armarius-astro: two pages are named "${page.route_name}".`);
|
|
33
|
+
routeNames.add(page.route_name);
|
|
34
|
+
return { ...page, slug: normalizeSlug(page.slug) };
|
|
35
|
+
});
|
|
36
|
+
return {
|
|
37
|
+
apiOrigin,
|
|
38
|
+
locale: options.locale.trim(),
|
|
39
|
+
pages,
|
|
40
|
+
screenOrigin: options.screenOrigin === undefined ? apiOrigin : httpOrigin(options.screenOrigin, 'screenOrigin'),
|
|
41
|
+
timeoutMs: options.timeoutMs ?? 10_000,
|
|
42
|
+
retries: options.retries ?? 2,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type { StoredMedia } from './images.js';
|
|
2
|
+
import type { StoredText } from './render.js';
|
|
3
|
+
import { type BuiltPath, type CrudRecord, type RouteTable } from './routes.js';
|
|
4
|
+
/** One content of `GET /api/nuxt/contents`. */
|
|
5
|
+
export interface StoredContent extends StoredText {
|
|
6
|
+
name: string;
|
|
7
|
+
href?: string | null;
|
|
8
|
+
picture?: StoredMedia | null;
|
|
9
|
+
picture_alt?: string | null;
|
|
10
|
+
}
|
|
11
|
+
/** One page of `GET /api/nuxt/pages`. */
|
|
12
|
+
export interface StoredPage {
|
|
13
|
+
id: number;
|
|
14
|
+
route_name: string;
|
|
15
|
+
title?: string | null;
|
|
16
|
+
seo_title?: string | null;
|
|
17
|
+
seo_description?: string | null;
|
|
18
|
+
seo_image?: (StoredMedia & {
|
|
19
|
+
url?: string | null;
|
|
20
|
+
}) | null;
|
|
21
|
+
}
|
|
22
|
+
export interface MenuItem {
|
|
23
|
+
id: number;
|
|
24
|
+
label: string | null;
|
|
25
|
+
path: string | null;
|
|
26
|
+
link: string | null;
|
|
27
|
+
target?: string | null;
|
|
28
|
+
children?: MenuItem[];
|
|
29
|
+
[field: string]: unknown;
|
|
30
|
+
}
|
|
31
|
+
export interface Menu {
|
|
32
|
+
slug: string;
|
|
33
|
+
name: string | null;
|
|
34
|
+
items: MenuItem[];
|
|
35
|
+
}
|
|
36
|
+
export interface PageSeo {
|
|
37
|
+
pageId: number | null;
|
|
38
|
+
title: string;
|
|
39
|
+
description: string;
|
|
40
|
+
imageUrl: string | null;
|
|
41
|
+
}
|
|
42
|
+
/** What `getStaticPaths` of the site's catch-all route returns (`src/pages/[...path].astro`). */
|
|
43
|
+
export interface ArmariusStaticPath {
|
|
44
|
+
params: {
|
|
45
|
+
path: string | undefined;
|
|
46
|
+
};
|
|
47
|
+
props: {
|
|
48
|
+
routeName: string;
|
|
49
|
+
view: string | undefined;
|
|
50
|
+
record: CrudRecord | null;
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
/** Laravel serves an empty keyed collection as `[]`. */
|
|
54
|
+
export declare function keyed<T>(value: unknown): Record<string, T>;
|
|
55
|
+
export declare function storedContents(): Promise<Record<string, StoredContent>>;
|
|
56
|
+
export declare function storedRoutes(): Promise<RouteTable>;
|
|
57
|
+
export declare function storedPages(): Promise<Record<string, StoredPage>>;
|
|
58
|
+
/** The records of a CRUD in the build's locale (list mode: at most `slym.api.list_limit`, 500 by default). */
|
|
59
|
+
export declare function getRecords(crud: string): Promise<CrudRecord[]>;
|
|
60
|
+
/** A setting of Armarius by its key, in the build's locale; `undefined` when unknown. */
|
|
61
|
+
export declare function getSetting<T = unknown>(key: string): Promise<T | undefined>;
|
|
62
|
+
/** An active menu by its slug, `undefined` when unknown; an entry's link a browser would run is `null`. */
|
|
63
|
+
export declare function getMenu(slug: string): Promise<Menu | undefined>;
|
|
64
|
+
/** The SEO of a page of the code, as edited in Armarius; the page's title in the code until it is synced. */
|
|
65
|
+
export declare function getPageSeo(routeName: string): Promise<PageSeo>;
|
|
66
|
+
/** Every path of the site, records included, also recorded for the components that need their page. */
|
|
67
|
+
export declare function resolvePaths(): Promise<BuiltPath[]>;
|
|
68
|
+
/** For `getStaticPaths` of the site's catch-all route `src/pages/[...path].astro`. */
|
|
69
|
+
export declare function armariusPaths(): Promise<ArmariusStaticPath[]>;
|
package/dist/readers.js
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { ArmariusApiError, requestJson } from './api.js';
|
|
2
|
+
import { buildPaths, isSafeLink } from './routes.js';
|
|
3
|
+
import { kitState, requireOptions } from './state.js';
|
|
4
|
+
/**
|
|
5
|
+
* One answer per key and build, shared by every reader that asks for it. A failure is forgotten, so that
|
|
6
|
+
* `astro dev` asks again at the next page instead of keeping it until it restarts.
|
|
7
|
+
*/
|
|
8
|
+
function memo(key, compute) {
|
|
9
|
+
const { requests } = kitState();
|
|
10
|
+
let pending = requests.get(key);
|
|
11
|
+
if (pending === undefined) {
|
|
12
|
+
pending = compute();
|
|
13
|
+
requests.set(key, pending);
|
|
14
|
+
pending.catch(() => requests.delete(key));
|
|
15
|
+
}
|
|
16
|
+
return pending;
|
|
17
|
+
}
|
|
18
|
+
function once(key, path, request = {}) {
|
|
19
|
+
return memo(key, () => requestJson(requireOptions(), path, request));
|
|
20
|
+
}
|
|
21
|
+
/** Laravel serves an empty keyed collection as `[]`. */
|
|
22
|
+
export function keyed(value) {
|
|
23
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value) ? value : {};
|
|
24
|
+
}
|
|
25
|
+
export async function storedContents() {
|
|
26
|
+
const body = await once('contents', '/api/nuxt/contents');
|
|
27
|
+
return keyed(body.data);
|
|
28
|
+
}
|
|
29
|
+
export async function storedRoutes() {
|
|
30
|
+
const body = await once('routes', '/api/nuxt/routes');
|
|
31
|
+
return keyed(body.routes);
|
|
32
|
+
}
|
|
33
|
+
export async function storedPages() {
|
|
34
|
+
const body = await once('pages', '/api/nuxt/pages', { query: { include: 'seo_image' } });
|
|
35
|
+
return keyed(body.data);
|
|
36
|
+
}
|
|
37
|
+
async function siteConfig() {
|
|
38
|
+
const body = await once('configs', '/api/nuxt/configs', { method: 'POST' });
|
|
39
|
+
return { settings: keyed(body.data.settings), menus: keyed(body.data.menus) };
|
|
40
|
+
}
|
|
41
|
+
/** The records of a CRUD in the build's locale (list mode: at most `slym.api.list_limit`, 500 by default). */
|
|
42
|
+
export async function getRecords(crud) {
|
|
43
|
+
const path = `/api/cruds/from-slug/${encodeURIComponent(crud)}/items`;
|
|
44
|
+
const body = await once(`records:${crud}`, path, { query: { list: '1' } });
|
|
45
|
+
// Without a list, the build would end green without the records' pages.
|
|
46
|
+
if (!Array.isArray(body))
|
|
47
|
+
throw new ArmariusApiError(`armarius-astro: GET ${path} did not answer a list of records.`);
|
|
48
|
+
return body;
|
|
49
|
+
}
|
|
50
|
+
/** A setting of Armarius by its key, in the build's locale; `undefined` when unknown. */
|
|
51
|
+
export async function getSetting(key) {
|
|
52
|
+
return (await siteConfig()).settings[key];
|
|
53
|
+
}
|
|
54
|
+
/** An active menu by its slug, `undefined` when unknown; an entry's link a browser would run is `null`. */
|
|
55
|
+
export async function getMenu(slug) {
|
|
56
|
+
const menu = (await siteConfig()).menus[slug];
|
|
57
|
+
return menu === undefined ? undefined : { ...menu, items: menu.items.map(safeMenuItem) };
|
|
58
|
+
}
|
|
59
|
+
function safeMenuItem(item) {
|
|
60
|
+
const safe = {
|
|
61
|
+
...item,
|
|
62
|
+
path: item.path !== null && isSafeLink(item.path) ? item.path : null,
|
|
63
|
+
link: item.link !== null && isSafeLink(item.link) ? item.link : null,
|
|
64
|
+
};
|
|
65
|
+
if (item.children !== undefined)
|
|
66
|
+
safe.children = item.children.map(safeMenuItem);
|
|
67
|
+
return safe;
|
|
68
|
+
}
|
|
69
|
+
/** The SEO of a page of the code, as edited in Armarius; the page's title in the code until it is synced. */
|
|
70
|
+
export async function getPageSeo(routeName) {
|
|
71
|
+
const page = (await storedPages())[routeName];
|
|
72
|
+
const codePage = requireOptions().pages.find(candidate => candidate.route_name === routeName);
|
|
73
|
+
const title = nonBlank(page?.seo_title) ?? nonBlank(page?.title) ?? codePage?.title ?? '';
|
|
74
|
+
return {
|
|
75
|
+
pageId: page?.id ?? null,
|
|
76
|
+
title,
|
|
77
|
+
description: nonBlank(page?.seo_description) ?? '',
|
|
78
|
+
imageUrl: page?.seo_image?.url ?? null,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
function nonBlank(value) {
|
|
82
|
+
return typeof value === 'string' && value.trim() !== '' ? value : null;
|
|
83
|
+
}
|
|
84
|
+
/** Every path of the site, records included, also recorded for the components that need their page. */
|
|
85
|
+
export function resolvePaths() {
|
|
86
|
+
return memo('paths', computePaths);
|
|
87
|
+
}
|
|
88
|
+
async function computePaths() {
|
|
89
|
+
const options = requireOptions();
|
|
90
|
+
const cruds = [...new Set(options.pages.flatMap((page) => page.crud === undefined ? [] : [page.crud]))];
|
|
91
|
+
const [routes, ...records] = await Promise.all([storedRoutes(), ...cruds.map(crud => getRecords(crud))]);
|
|
92
|
+
const recordsByCrud = Object.fromEntries(cruds.map((crud, index) => [crud, records[index]]));
|
|
93
|
+
const built = buildPaths(options.pages, routes, options.locale, recordsByCrud);
|
|
94
|
+
const { paths } = kitState();
|
|
95
|
+
for (const entry of built) {
|
|
96
|
+
paths.set(entry.path, { routeName: entry.page.route_name, recordId: entry.record?.id ?? null });
|
|
97
|
+
}
|
|
98
|
+
return built;
|
|
99
|
+
}
|
|
100
|
+
/** For `getStaticPaths` of the site's catch-all route `src/pages/[...path].astro`. */
|
|
101
|
+
export async function armariusPaths() {
|
|
102
|
+
return (await resolvePaths()).map(entry => ({
|
|
103
|
+
params: { path: entry.path === '/' ? undefined : entry.path.slice(1) },
|
|
104
|
+
props: { routeName: entry.page.route_name, view: entry.page.view, record: entry.record },
|
|
105
|
+
}));
|
|
106
|
+
}
|
package/dist/render.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/** What `GET /api/nuxt/contents` serves for one text, with `nuxt-sync.replace_untouched_content` on. */
|
|
2
|
+
export interface StoredText {
|
|
3
|
+
content?: string | null;
|
|
4
|
+
code_hash?: string | null;
|
|
5
|
+
code_intact?: boolean;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* The text a page shows, from the value of the code and the one Armarius stores: the code's when
|
|
9
|
+
* Armarius has none, or when the code changed and nobody edited the text in the admin, since the sync about
|
|
10
|
+
* to run replaces it; the stored one otherwise.
|
|
11
|
+
*/
|
|
12
|
+
export declare function textToRender(codeValue: string, stored: StoredText | undefined): string;
|
|
13
|
+
/**
|
|
14
|
+
* The code changed since the last sync (hash of its value given), and the admin edited the text: the sync
|
|
15
|
+
* keeps the admin's text and logs a conflict.
|
|
16
|
+
*/
|
|
17
|
+
export declare function isConflict(codeValueHash: string, stored: StoredText | undefined): boolean;
|
package/dist/render.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { codeHash } from './hash.js';
|
|
2
|
+
/**
|
|
3
|
+
* The text a page shows, from the value of the code and the one Armarius stores: the code's when
|
|
4
|
+
* Armarius has none, or when the code changed and nobody edited the text in the admin, since the sync about
|
|
5
|
+
* to run replaces it; the stored one otherwise.
|
|
6
|
+
*/
|
|
7
|
+
export function textToRender(codeValue, stored) {
|
|
8
|
+
const storedValue = stored?.content ?? '';
|
|
9
|
+
if (storedValue === '')
|
|
10
|
+
return codeValue;
|
|
11
|
+
if (codeChangedSinceSync(codeHash(codeValue), stored) && stored?.code_intact === true)
|
|
12
|
+
return codeValue;
|
|
13
|
+
return storedValue;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* The code changed since the last sync (hash of its value given), and the admin edited the text: the sync
|
|
17
|
+
* keeps the admin's text and logs a conflict.
|
|
18
|
+
*/
|
|
19
|
+
export function isConflict(codeValueHash, stored) {
|
|
20
|
+
return codeChangedSinceSync(codeValueHash, stored)
|
|
21
|
+
&& stored?.code_intact !== true
|
|
22
|
+
&& (stored?.content ?? '') !== '';
|
|
23
|
+
}
|
|
24
|
+
function codeChangedSinceSync(codeValueHash, stored) {
|
|
25
|
+
return typeof stored?.code_hash === 'string' && stored.code_hash !== codeValueHash;
|
|
26
|
+
}
|
package/dist/routes.d.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { ArmariusPage } from './options.js';
|
|
2
|
+
/** `GET /api/nuxt/routes`: per `route_name`, the path of each locale (`/` or `/a/b`). */
|
|
3
|
+
export type RouteTable = Record<string, Record<string, string>>;
|
|
4
|
+
/** A record of a CRUD, as `GET /api/cruds/from-slug/{slug}/items?list=1` serves it. */
|
|
5
|
+
export interface CrudRecord {
|
|
6
|
+
id: number | string;
|
|
7
|
+
permalink?: string | null;
|
|
8
|
+
[field: string]: unknown;
|
|
9
|
+
}
|
|
10
|
+
export interface BuiltPath {
|
|
11
|
+
/** `/` or `/a/b`, without locale prefix nor trailing slash. */
|
|
12
|
+
path: string;
|
|
13
|
+
page: ArmariusPage;
|
|
14
|
+
record: CrudRecord | null;
|
|
15
|
+
}
|
|
16
|
+
export declare class ArmariusRouteError extends Error {
|
|
17
|
+
name: string;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Whether a link is not blank and leads nowhere a browser would run (`javascript:`, `data:`), parsed as a
|
|
21
|
+
* browser parses it: leading control characters and tabs inside the scheme do not hide it.
|
|
22
|
+
*/
|
|
23
|
+
export declare function isSafeLink(value: string): boolean;
|
|
24
|
+
/** `/` for the home page, `/a/b` otherwise: no trailing slash, no `index`, no `.html`. */
|
|
25
|
+
export declare function normalizePath(path: string): string;
|
|
26
|
+
/**
|
|
27
|
+
* The path of a page of the code: the one Armarius serves for the locale, pending slug included, else the
|
|
28
|
+
* page's slug in the code, for a page Armarius does not know yet. A path taken from the files instead would keep
|
|
29
|
+
* an old address once a slug renamed in the admin is promoted at the end of the build.
|
|
30
|
+
*/
|
|
31
|
+
export declare function pagePath(page: ArmariusPage, routes: RouteTable, locale: string): string;
|
|
32
|
+
/**
|
|
33
|
+
* Every path the site builds: each page of the code, and under a page tied to a CRUD one path per record,
|
|
34
|
+
* `<page path>/<permalink>`. Two pages on one path fail the build.
|
|
35
|
+
*/
|
|
36
|
+
export declare function buildPaths(pages: ArmariusPage[], routes: RouteTable, locale: string, recordsByCrud: Record<string, CrudRecord[]>): BuiltPath[];
|
package/dist/routes.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
export class ArmariusRouteError extends Error {
|
|
2
|
+
name = 'ArmariusRouteError';
|
|
3
|
+
}
|
|
4
|
+
/** The schemes a link may lead to; a path or an anchor resolves to the base's `https:`. */
|
|
5
|
+
const SAFE_LINK_PROTOCOLS = new Set(['http:', 'https:', 'mailto:', 'tel:']);
|
|
6
|
+
/**
|
|
7
|
+
* Whether a link is not blank and leads nowhere a browser would run (`javascript:`, `data:`), parsed as a
|
|
8
|
+
* browser parses it: leading control characters and tabs inside the scheme do not hide it.
|
|
9
|
+
*/
|
|
10
|
+
export function isSafeLink(value) {
|
|
11
|
+
if (value.trim() === '')
|
|
12
|
+
return false;
|
|
13
|
+
try {
|
|
14
|
+
return SAFE_LINK_PROTOCOLS.has(new URL(value, 'https://site.invalid/').protocol);
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/** `/` for the home page, `/a/b` otherwise: no trailing slash, no `index`, no `.html`. */
|
|
21
|
+
export function normalizePath(path) {
|
|
22
|
+
const trimmed = path
|
|
23
|
+
.replace(/[?#].*$/, '')
|
|
24
|
+
.replace(/\.html$/, '')
|
|
25
|
+
.replace(/(^|\/)index$/, '')
|
|
26
|
+
.replace(/^\/+|\/+$/g, '');
|
|
27
|
+
return trimmed === '' ? '/' : `/${trimmed}`;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The path of a page of the code: the one Armarius serves for the locale, pending slug included, else the
|
|
31
|
+
* page's slug in the code, for a page Armarius does not know yet. A path taken from the files instead would keep
|
|
32
|
+
* an old address once a slug renamed in the admin is promoted at the end of the build.
|
|
33
|
+
*/
|
|
34
|
+
export function pagePath(page, routes, locale) {
|
|
35
|
+
const served = routes[page.route_name]?.[locale];
|
|
36
|
+
return normalizePath(typeof served === 'string' && served !== '' ? served : page.slug);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Every path the site builds: each page of the code, and under a page tied to a CRUD one path per record,
|
|
40
|
+
* `<page path>/<permalink>`. Two pages on one path fail the build.
|
|
41
|
+
*/
|
|
42
|
+
export function buildPaths(pages, routes, locale, recordsByCrud) {
|
|
43
|
+
const built = [];
|
|
44
|
+
const owners = new Map();
|
|
45
|
+
const add = (entry, owner) => {
|
|
46
|
+
const previous = owners.get(entry.path);
|
|
47
|
+
if (previous !== undefined) {
|
|
48
|
+
throw new ArmariusRouteError(`armarius-astro: ${previous} and ${owner} are both built on ${entry.path}.`);
|
|
49
|
+
}
|
|
50
|
+
owners.set(entry.path, owner);
|
|
51
|
+
built.push(entry);
|
|
52
|
+
};
|
|
53
|
+
for (const page of pages) {
|
|
54
|
+
const path = pagePath(page, routes, locale);
|
|
55
|
+
add({ path, page, record: null }, `page "${page.route_name}"`);
|
|
56
|
+
if (page.crud === undefined)
|
|
57
|
+
continue;
|
|
58
|
+
for (const record of recordsByCrud[page.crud] ?? []) {
|
|
59
|
+
const permalink = (record.permalink ?? '').replace(/^\/+|\/+$/g, '');
|
|
60
|
+
if (permalink === '') {
|
|
61
|
+
throw new ArmariusRouteError(`armarius-astro: record ${record.id} of "${page.crud}" has no permalink in ${locale}.`);
|
|
62
|
+
}
|
|
63
|
+
// A `..` segment would write the record's page over another one, out of the duplicate check.
|
|
64
|
+
if (permalink.includes('\\') || permalink.split('/').some(segment => segment === '.' || segment === '..')) {
|
|
65
|
+
throw new ArmariusRouteError(`armarius-astro: record ${record.id} of "${page.crud}" has the permalink "${permalink}", which leaves its page.`);
|
|
66
|
+
}
|
|
67
|
+
add({ path: normalizePath(path === '/' ? permalink : `${path}/${permalink}`), page, record }, `record ${record.id} of "${page.crud}"`);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return built;
|
|
71
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { type GlideParams } from './images.js';
|
|
2
|
+
export declare class ArmariusContentError extends Error {
|
|
3
|
+
name: string;
|
|
4
|
+
}
|
|
5
|
+
export interface TextInput {
|
|
6
|
+
name: string;
|
|
7
|
+
/** The text of the code: the component's slot, rendered. */
|
|
8
|
+
value: string;
|
|
9
|
+
tag: string;
|
|
10
|
+
pathname: string;
|
|
11
|
+
class?: string;
|
|
12
|
+
/** A link whose destination is a path or an address. */
|
|
13
|
+
href?: string;
|
|
14
|
+
/** A link to a page of the code, by its `route_name`. */
|
|
15
|
+
routeName?: string;
|
|
16
|
+
}
|
|
17
|
+
export interface RenderedText {
|
|
18
|
+
html: string;
|
|
19
|
+
tag: string;
|
|
20
|
+
isLink: boolean;
|
|
21
|
+
href: string | undefined;
|
|
22
|
+
}
|
|
23
|
+
export interface ImageInput {
|
|
24
|
+
name: string;
|
|
25
|
+
/** The image of the code, a path of the site (`public/`). */
|
|
26
|
+
src: string;
|
|
27
|
+
alt: string;
|
|
28
|
+
glide: GlideParams;
|
|
29
|
+
pathname: string;
|
|
30
|
+
}
|
|
31
|
+
export interface RenderedImage {
|
|
32
|
+
src: string;
|
|
33
|
+
alt: string;
|
|
34
|
+
}
|
|
35
|
+
export interface EditionBootData {
|
|
36
|
+
locale: string;
|
|
37
|
+
pageId: number | null;
|
|
38
|
+
apiOrigin: string;
|
|
39
|
+
screenOrigin: string;
|
|
40
|
+
layerUrl: string;
|
|
41
|
+
}
|
|
42
|
+
/** The value of the code a text sends and hashes: the slot's HTML without the marks of scoped styles. */
|
|
43
|
+
export declare function codeValueOf(slotHtml: string): string;
|
|
44
|
+
export declare function renderText(input: TextInput): Promise<RenderedText>;
|
|
45
|
+
export declare function renderImage(input: ImageInput): Promise<RenderedImage>;
|
|
46
|
+
export declare function editionBoot(pathname: string): Promise<EditionBootData>;
|