inlaycms 0.2.0 → 0.4.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 +334 -1
- package/assets/admin/admin.css +1 -1
- package/assets/admin/admin.js +1 -1
- package/assets/admin/editor.js +59 -44
- package/dist/adapters/admin/screens/redirects.js +12 -3
- package/dist/adapters/admin/views/layout.js +27 -7
- package/dist/adapters/cli/remote.js +9 -1
- package/dist/adapters/mcp/tools/redirects.js +16 -2
- package/dist/adapters/site/assets.d.ts +11 -0
- package/dist/adapters/site/assets.js +71 -0
- package/dist/adapters/site/cache.d.ts +2 -1
- package/dist/adapters/site/cache.js +6 -1
- package/dist/adapters/site/context.d.ts +6 -0
- package/dist/adapters/site/context.js +38 -13
- package/dist/adapters/site/head.d.ts +30 -7
- package/dist/adapters/site/head.js +145 -20
- package/dist/adapters/site/headers.d.ts +5 -0
- package/dist/adapters/site/headers.js +31 -0
- package/dist/adapters/site/plugin.d.ts +2 -0
- package/dist/adapters/site/plugin.js +32 -9
- package/dist/adapters/site/public.js +7 -24
- package/dist/adapters/site/render.d.ts +5 -0
- package/dist/adapters/site/render.js +23 -8
- package/dist/adapters/site/routes.d.ts +6 -0
- package/dist/adapters/site/routes.js +8 -3
- package/dist/adapters/site/seo.d.ts +20 -0
- package/dist/adapters/site/seo.js +45 -0
- package/dist/adapters/site/sitemap.d.ts +1 -1
- package/dist/adapters/site/sitemap.js +1 -1
- package/dist/config.js +3 -2
- package/dist/core/agent/format.js +1 -1
- package/dist/core/agent/schema.js +3 -0
- package/dist/core/boot.js +7 -0
- package/dist/core/check.js +6 -1
- package/dist/core/content/dry-run.d.ts +7 -6
- package/dist/core/content/dry-run.js +8 -8
- package/dist/core/content/publish.d.ts +1 -1
- package/dist/core/content/publish.js +5 -3
- package/dist/core/content/read.d.ts +3 -0
- package/dist/core/content/reader.js +3 -2
- package/dist/core/content/refs.js +1 -1
- package/dist/core/content/search.js +2 -2
- package/dist/core/content/shipped-images.d.ts +32 -0
- package/dist/core/content/shipped-images.js +62 -0
- package/dist/core/content/shipped-input.d.ts +9 -3
- package/dist/core/content/shipped-input.js +165 -28
- package/dist/core/content/shipped.js +8 -3
- package/dist/core/content/validate.js +7 -1
- package/dist/core/core.d.ts +5 -1
- package/dist/core/core.js +5 -4
- package/dist/core/db/connection.d.ts +6 -0
- package/dist/core/db/connection.js +13 -1
- package/dist/core/db/migrate.js +2 -0
- package/dist/core/db/migrations/0007_published_at.d.ts +3 -0
- package/dist/core/db/migrations/0007_published_at.js +16 -0
- package/dist/core/db/schema.d.ts +1 -0
- package/dist/core/export/export.js +3 -1
- package/dist/core/export/format.d.ts +8 -1
- package/dist/core/export/format.js +3 -2
- package/dist/core/export/read.js +7 -0
- package/dist/core/export/stage.d.ts +1 -1
- package/dist/core/export/stage.js +24 -1
- package/dist/core/fields/builtins.d.ts +28 -0
- package/dist/core/fields/builtins.js +103 -11
- package/dist/core/fields/define.d.ts +6 -0
- package/dist/core/fields/widgets.d.ts +7 -0
- package/dist/core/fields/widgets.js +77 -28
- package/dist/core/media/library.js +2 -2
- package/dist/core/messages/en.d.ts +72 -6
- package/dist/core/messages/en.js +72 -6
- package/dist/core/redirects/collapse.d.ts +17 -1
- package/dist/core/redirects/collapse.js +19 -3
- package/dist/core/redirects/redirects.d.ts +13 -12
- package/dist/core/redirects/redirects.js +141 -53
- package/dist/core/redirects/rule.d.ts +25 -0
- package/dist/core/redirects/rule.js +55 -0
- package/dist/core/registry/define.d.ts +40 -0
- package/dist/core/registry/define.js +3 -0
- package/dist/core/registry/registry.d.ts +21 -0
- package/dist/core/registry/registry.js +114 -6
- package/dist/core/registry/render.d.ts +37 -1
- package/dist/core/registry/route.d.ts +4 -0
- package/dist/core/release/bundle.js +30 -5
- package/dist/core/release/manifest.d.ts +14 -0
- package/dist/core/release/manifest.js +42 -17
- package/dist/core/release/markdown-file.d.ts +21 -0
- package/dist/core/release/markdown-file.js +172 -0
- package/dist/core/release/pack.js +2 -2
- package/dist/core/release/read.js +4 -1
- package/dist/core/release/site-dir.d.ts +4 -0
- package/dist/core/release/site-dir.js +8 -0
- package/dist/core/report.d.ts +8 -2
- package/dist/core/richtext/html.d.ts +3 -1
- package/dist/core/richtext/html.js +18 -4
- package/dist/core/richtext/inline.d.ts +22 -0
- package/dist/core/richtext/inline.js +33 -0
- package/dist/core/richtext/markdown.d.ts +19 -1
- package/dist/core/richtext/markdown.js +187 -22
- package/dist/core/richtext/schema.d.ts +22 -5
- package/dist/core/richtext/schema.js +81 -4
- package/dist/core/site/live.d.ts +1 -0
- package/dist/core/site/live.js +1 -0
- package/dist/core/site/paths.d.ts +6 -0
- package/dist/core/site/paths.js +13 -0
- package/dist/core/site/resolve.d.ts +1 -1
- package/dist/core/site/resolve.js +119 -30
- package/dist/core/site/sitemap.d.ts +1 -1
- package/dist/core/site/sitemap.js +4 -0
- package/dist/core/types.d.ts +28 -3
- package/dist/host/app.d.ts +5 -0
- package/dist/host/app.js +10 -1
- package/dist/host/dev.js +3 -7
- package/dist/host/diff.d.ts +19 -1
- package/dist/host/diff.js +32 -2
- package/dist/index.d.ts +2 -2
- package/dist/markdown-modules.d.ts +8 -0
- package/dist/markdown-modules.js +1 -0
- package/dist/site.d.ts +8 -3
- package/dist/site.js +6 -0
- package/dist/testing.d.ts +22 -0
- package/dist/testing.js +155 -0
- package/package.json +7 -1
|
@@ -4,10 +4,11 @@ import { contentIssue } from '../content/validate.js';
|
|
|
4
4
|
import { InlayError } from '../errors.js';
|
|
5
5
|
import { t } from '../messages/index.js';
|
|
6
6
|
import { resolveWithoutRules } from '../site/resolve.js';
|
|
7
|
-
import { encodePath,
|
|
7
|
+
import { encodePath, splitLocale, withLocale } from '../site/paths.js';
|
|
8
8
|
import { sitePath } from '../site/live.js';
|
|
9
|
-
import { collapseTargets, fillParams, hasLocalePrefix, isPattern, localize, paramNames, pathTarget, patternRegExp,
|
|
9
|
+
import { collapseTargets, fillParams, hasLocalePrefix, isPattern, localize, nextHops, paramNames, pathTarget, patternRegExp, ruleIndex, } from './collapse.js';
|
|
10
10
|
import { parseCsv } from './csv.js';
|
|
11
|
+
import { cleanPath, defaultLocalePrefix, externalTarget, redirectFrom, targetOf } from './rule.js';
|
|
11
12
|
import { withTransaction } from '../content/dry-run.js';
|
|
12
13
|
const LABELS = {
|
|
13
14
|
from: 'label.redirect.from',
|
|
@@ -15,7 +16,7 @@ const LABELS = {
|
|
|
15
16
|
status: 'label.redirect.status',
|
|
16
17
|
};
|
|
17
18
|
export function rowTarget(row) {
|
|
18
|
-
return row.to_group_id !== null ? { document: row.to_group_id } :
|
|
19
|
+
return row.to_group_id !== null ? { document: row.to_group_id } : targetOf(row.to_path ?? '/');
|
|
19
20
|
}
|
|
20
21
|
function toRedirect(row) {
|
|
21
22
|
return {
|
|
@@ -33,24 +34,33 @@ export async function listRedirects(deps) {
|
|
|
33
34
|
const rows = await deps.db.selectFrom('redirects').selectAll().orderBy('from_path').orderBy('id').execute();
|
|
34
35
|
return rows.map(toRedirect);
|
|
35
36
|
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
export function
|
|
39
|
-
const
|
|
40
|
-
|
|
37
|
+
export { cleanPath, redirectFrom };
|
|
38
|
+
/** The site's code rules, in definition order, each marked when an admin rule or a page answers at its `from` first. */
|
|
39
|
+
export async function listCodeRedirects(deps) {
|
|
40
|
+
const { registry } = deps;
|
|
41
|
+
const stored = await deps.db.selectFrom('redirects').select('from_path').execute();
|
|
42
|
+
const overriding = new Set(stored.map((r) => r.from_path));
|
|
43
|
+
const look = lookup(deps);
|
|
44
|
+
const out = [];
|
|
45
|
+
for (const r of registry.redirects) {
|
|
46
|
+
out.push({
|
|
47
|
+
from: r.from,
|
|
48
|
+
to: targetOf(r.to),
|
|
49
|
+
status: r.status,
|
|
50
|
+
overridden: overriding.has(r.from),
|
|
51
|
+
hidden: !isPattern(r.from) && (await isHidden(registry, look, r.from)),
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
return out;
|
|
41
55
|
}
|
|
42
|
-
/**
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
const
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
if (path === null || isReserved(path) || path.split('/')[1] === registry.defaultLocale)
|
|
51
|
-
return null;
|
|
52
|
-
const names = paramNames(path);
|
|
53
|
-
return new Set(names).size === names.length ? path : null;
|
|
56
|
+
/** The `from` of each code rule that, with the stored rules, sends visitors round a loop with no stop. */
|
|
57
|
+
export async function codeRedirectLoops(deps) {
|
|
58
|
+
const rows = await deps.db.selectFrom('redirects').selectAll().orderBy('id').execute();
|
|
59
|
+
const look = lookup(deps);
|
|
60
|
+
const code = codeRules(deps.registry);
|
|
61
|
+
const isStop = async (path) => deps.registry.getRoutePaths.has(path) || (await look(path)) !== 'not_found';
|
|
62
|
+
const looping = await loopingRules(deps.registry, isStop, rows.map(ruleOf), code, code);
|
|
63
|
+
return code.filter((r) => looping.has(r)).map((r) => r.from);
|
|
54
64
|
}
|
|
55
65
|
function isRoutable(deps, target) {
|
|
56
66
|
const def = deps.registry.types.get(target.type);
|
|
@@ -74,22 +84,40 @@ function lookup(deps) {
|
|
|
74
84
|
return kind;
|
|
75
85
|
};
|
|
76
86
|
}
|
|
77
|
-
/**
|
|
78
|
-
|
|
79
|
-
|
|
87
|
+
/** The paths an exact `from` matches at: in every locale when it is unprefixed, else only itself. */
|
|
88
|
+
function firePaths(registry, from) {
|
|
89
|
+
return splitLocale(registry, from).locale === registry.defaultLocale
|
|
80
90
|
? registry.locales.map((l) => withLocale(registry, l, from))
|
|
81
91
|
: [from];
|
|
82
|
-
|
|
92
|
+
}
|
|
93
|
+
/** Whether an exact `from` is the path of a published page in any locale it matches in. */
|
|
94
|
+
async function isLivePage(registry, look, from) {
|
|
95
|
+
for (const p of firePaths(registry, from)) {
|
|
83
96
|
if ((await look(p)) === 'page')
|
|
84
97
|
return true;
|
|
85
98
|
}
|
|
86
99
|
return false;
|
|
87
100
|
}
|
|
101
|
+
/**
|
|
102
|
+
* Whether a code rule's exact `from` never fires: a published page answers at `from` itself, and
|
|
103
|
+
* something answers before any rule at each of its paths in the other locales.
|
|
104
|
+
*/
|
|
105
|
+
async function isHidden(registry, look, from) {
|
|
106
|
+
if ((await look(from)) !== 'page')
|
|
107
|
+
return false;
|
|
108
|
+
for (const p of firePaths(registry, from)) {
|
|
109
|
+
if ((await look(p)) === 'not_found')
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
114
|
+
/** A `to` that names a URL scheme, so it is checked as an address on another site. */
|
|
115
|
+
const URL_LIKE = /^[a-z][a-z0-9+.-]*:/i;
|
|
88
116
|
/**
|
|
89
117
|
* Checks a rule on its own (not against other rules): the shape of `from`
|
|
90
118
|
* and `to` (neither may start with the default locale, which a request is
|
|
91
|
-
* sent away from first
|
|
92
|
-
* published page.
|
|
119
|
+
* sent away from first; a `to` that isn't a path must be an https URL), the
|
|
120
|
+
* status, and that an exact `from` isn't a published page.
|
|
93
121
|
*/
|
|
94
122
|
export async function checkRedirect(deps, input, look = lookup(deps)) {
|
|
95
123
|
const issues = [];
|
|
@@ -101,12 +129,23 @@ export async function checkRedirect(deps, input, look = lookup(deps)) {
|
|
|
101
129
|
issue('from', 'path_taken', 'issue.redirectLivePath', { path: from });
|
|
102
130
|
}
|
|
103
131
|
let to = null;
|
|
104
|
-
if (typeof input.to === 'string') {
|
|
105
|
-
const
|
|
106
|
-
const
|
|
132
|
+
if (typeof input.to === 'string' && URL_LIKE.test(input.to.trim())) {
|
|
133
|
+
const url = externalTarget(input.to);
|
|
134
|
+
const param = url === null || from === null || !isPattern(from) ? undefined : paramNames(new URL(url).pathname)[0];
|
|
135
|
+
if (url === null)
|
|
136
|
+
issue('to', 'invalid', 'issue.redirectUrl', { path: input.to });
|
|
137
|
+
else if (param !== undefined)
|
|
138
|
+
issue('to', 'invalid', 'issue.redirectUrlParam', { name: param });
|
|
139
|
+
else
|
|
140
|
+
to = { url };
|
|
141
|
+
}
|
|
142
|
+
else if (typeof input.to === 'string') {
|
|
143
|
+
const trimmed = input.to.trim();
|
|
144
|
+
const path = trimmed.startsWith('/') ? cleanPath(trimmed) : null;
|
|
145
|
+
const locale = path === null ? null : defaultLocalePrefix(deps.registry, path);
|
|
107
146
|
if (path === null)
|
|
108
147
|
issue('to', 'invalid', 'issue.redirectTo', { path: input.to });
|
|
109
|
-
else if (
|
|
148
|
+
else if (locale !== null)
|
|
110
149
|
issue('to', 'invalid', 'issue.redirectToDefaultLocale', { path, locale });
|
|
111
150
|
else {
|
|
112
151
|
const allowed = from === null ? [] : paramNames(from);
|
|
@@ -141,7 +180,11 @@ export async function checkRedirect(deps, input, look = lookup(deps)) {
|
|
|
141
180
|
return { rule, issues };
|
|
142
181
|
}
|
|
143
182
|
function columns(to) {
|
|
144
|
-
|
|
183
|
+
if ('path' in to)
|
|
184
|
+
return { to_path: to.path, to_group_id: null };
|
|
185
|
+
if ('url' in to)
|
|
186
|
+
return { to_path: to.url, to_group_id: null };
|
|
187
|
+
return { to_path: null, to_group_id: to.document };
|
|
145
188
|
}
|
|
146
189
|
function invalid(issues) {
|
|
147
190
|
return new InlayError('validation', 'error.redirectInvalid', {}, { issues });
|
|
@@ -152,19 +195,24 @@ const SAMPLE = '\u0000';
|
|
|
152
195
|
function ruleOf(row) {
|
|
153
196
|
return { id: row.id, from: row.from_path, to: rowTarget(row) };
|
|
154
197
|
}
|
|
198
|
+
/** The site's code rules as walked beside the stored ones. `walk` tells rules apart by identity, so build these once per walk. */
|
|
199
|
+
function codeRules(registry) {
|
|
200
|
+
return registry.redirects.map((r) => ({ id: null, from: r.from, to: targetOf(r.to) }));
|
|
201
|
+
}
|
|
155
202
|
/**
|
|
156
203
|
* Settles a write of `incoming` rules over `rules` (every other rule, in id order, with any incoming
|
|
157
204
|
* rule that updates one in its place and new ones last, as they would be numbered). Each incoming
|
|
158
205
|
* exact path target is collapsed through exact rules, and existing rules whose target is an incoming
|
|
159
|
-
* exact `from` are re-pointed to where that rule goes
|
|
160
|
-
*
|
|
161
|
-
*
|
|
206
|
+
* exact `from` are re-pointed to where that rule goes, unless it goes to a URL. Only stored rules are
|
|
207
|
+
* collapsed through, never the site's code rules. Then every changed rule is walked as requests
|
|
208
|
+
* would follow it, code rules included. Rules are changed in place; `repointed` lists the existing
|
|
209
|
+
* ones that changed and `loops` the `blame` of every incoming rule whose write would make a loop.
|
|
162
210
|
*/
|
|
163
211
|
async function settle(deps, look, rules, incoming) {
|
|
164
212
|
const { registry } = deps;
|
|
165
213
|
const others = registry.locales.filter((l) => l !== registry.defaultLocale);
|
|
166
214
|
const before = ruleIndex(rules);
|
|
167
|
-
const isStop = async (path, state) => (await look(path, state)) !== 'not_found';
|
|
215
|
+
const isStop = async (path, state) => registry.getRoutePaths.has(path) || (await look(path, state)) !== 'not_found';
|
|
168
216
|
/** Whether a request for `path` reaches its exact rule in every locale and state the rule fires in. */
|
|
169
217
|
const reaches = async (path) => {
|
|
170
218
|
if ((await isStop(path, 'published')) || (await isStop(path, 'preview')))
|
|
@@ -195,7 +243,7 @@ async function settle(deps, look, rules, incoming) {
|
|
|
195
243
|
const repointed = [];
|
|
196
244
|
for (const r of incoming) {
|
|
197
245
|
const pointing = byTarget.get(r.from);
|
|
198
|
-
if (!pointing || isPattern(r.from) || !(await reaches(r.from)))
|
|
246
|
+
if (!pointing || 'url' in r.to || isPattern(r.from) || !(await reaches(r.from)))
|
|
199
247
|
continue;
|
|
200
248
|
for (const p of pointing) {
|
|
201
249
|
if (('path' in r.to && r.to.path === p.from) || !sameHop(registry, p.from, r.from, r.to))
|
|
@@ -206,20 +254,29 @@ async function settle(deps, look, rules, incoming) {
|
|
|
206
254
|
blame.set(p, r.blame);
|
|
207
255
|
}
|
|
208
256
|
}
|
|
209
|
-
const
|
|
210
|
-
const loops = new Set();
|
|
257
|
+
const looping = await loopingRules(registry, (p) => isStop(p, 'published'), rules, codeRules(registry), changed);
|
|
258
|
+
const loops = new Set([...looping].map((r) => blame.get(r)));
|
|
259
|
+
return { repointed, loops };
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Of `changed`, the rules that send some request they answer round a loop with no stop on it, with
|
|
263
|
+
* `rules` (the stored rules, in id order) tried before `code` (the site's code rules) as requests try them.
|
|
264
|
+
*/
|
|
265
|
+
async function loopingRules(registry, isStop, rules, code, changed) {
|
|
266
|
+
const sets = [ruleIndex(rules), ruleIndex(code)];
|
|
267
|
+
const out = new Set();
|
|
211
268
|
let starts;
|
|
212
|
-
const startsAt = () => (starts ??= startsOf(registry, rules));
|
|
269
|
+
const startsAt = () => (starts ??= startsOf(registry, [...rules, ...code]));
|
|
213
270
|
for (const rule of changed) {
|
|
214
271
|
for (const start of walkStarts(registry, rule, startsAt)) {
|
|
215
|
-
const trail = walk(registry,
|
|
216
|
-
if (trail && !(await someAsync(trail,
|
|
217
|
-
|
|
272
|
+
const trail = walk(registry, sets, rule, start);
|
|
273
|
+
if (trail && !(await someAsync(trail, isStop))) {
|
|
274
|
+
out.add(rule);
|
|
218
275
|
break;
|
|
219
276
|
}
|
|
220
277
|
}
|
|
221
278
|
}
|
|
222
|
-
return
|
|
279
|
+
return out;
|
|
223
280
|
}
|
|
224
281
|
/** What `sameHop` depends on in a rule's `from`: its locale, and whether its first segment is a param. */
|
|
225
282
|
function hopScope(registry, from) {
|
|
@@ -312,22 +369,29 @@ function* walkStarts(registry, rule, starts) {
|
|
|
312
369
|
}
|
|
313
370
|
}
|
|
314
371
|
/**
|
|
315
|
-
* Follows a request for `start` that `rule` answers, matching rules exactly as `resolvePath` does
|
|
316
|
-
* and gives the paths it passes when it goes round in a loop (or on for more than `MAX_HOPS`), or
|
|
372
|
+
* Follows a request for `start` that `rule` answers, matching rules exactly as `resolvePath` does
|
|
373
|
+
* (in `nextHops` order: stored rules, then code rules, then the lowercase fallback), and gives the paths it passes when it goes round in a loop (or on for more than `MAX_HOPS`), or
|
|
317
374
|
* `null`. It is a loop only if none of those paths is a stop, where a page or a path redirect answers
|
|
318
375
|
* before any rule. A rule that would send `start` to itself is a loop. `null` when `rule` doesn't
|
|
319
|
-
* answer `start`. A document target ends the walk.
|
|
376
|
+
* answer `start`. A document or URL target ends the walk; the fallback goes on to the lowercase path.
|
|
320
377
|
*/
|
|
321
|
-
function walk(registry,
|
|
378
|
+
function walk(registry, sets, rule, start) {
|
|
322
379
|
const trail = [start];
|
|
323
380
|
const seen = new Set(trail);
|
|
324
381
|
let path = start;
|
|
325
382
|
for (let hops = 0;; hops++) {
|
|
326
383
|
const { locale } = splitLocale(registry, path);
|
|
327
384
|
let to = null;
|
|
328
|
-
for (const
|
|
385
|
+
for (const hop of nextHops(registry, sets, path)) {
|
|
386
|
+
if ('fallback' in hop) {
|
|
387
|
+
if (hops === 0)
|
|
388
|
+
return null;
|
|
389
|
+
to = { path: hop.fallback };
|
|
390
|
+
break;
|
|
391
|
+
}
|
|
392
|
+
const { candidate } = hop;
|
|
329
393
|
const target = candidate.rule.to;
|
|
330
|
-
to = '
|
|
394
|
+
to = 'path' in target ? { path: pathTarget(registry, locale, target.path, candidate) } : target;
|
|
331
395
|
if ('path' in to && to.path === path) {
|
|
332
396
|
if (hops === 0 && candidate.rule === rule)
|
|
333
397
|
return trail;
|
|
@@ -338,7 +402,7 @@ function walk(registry, index, rule, start) {
|
|
|
338
402
|
return null;
|
|
339
403
|
break;
|
|
340
404
|
}
|
|
341
|
-
if (to === null || '
|
|
405
|
+
if (to === null || !('path' in to))
|
|
342
406
|
return null;
|
|
343
407
|
if (seen.has(to.path) || hops >= MAX_HOPS)
|
|
344
408
|
return trail;
|
|
@@ -387,11 +451,35 @@ export async function createRedirect(deps, input, ctx) {
|
|
|
387
451
|
return toRedirect(created);
|
|
388
452
|
});
|
|
389
453
|
}
|
|
454
|
+
/**
|
|
455
|
+
* Deleting a rule can uncover a code rule with the same `from`, or let the lowercase fallback answer
|
|
456
|
+
* there, and either can close a loop. Refuses the delete when some rule loops after it that didn't before.
|
|
457
|
+
*/
|
|
390
458
|
export async function deleteRedirect(deps, id, ctx) {
|
|
391
459
|
assertCan(ctx, 'redirects.write');
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
460
|
+
await withTransaction(deps, async (tdeps) => {
|
|
461
|
+
const before = await tdeps.db.selectFrom('redirects').selectAll().orderBy('id').execute();
|
|
462
|
+
if (!before.some((r) => r.id === id))
|
|
463
|
+
throw new InlayError('not_found', 'error.redirectNotFound');
|
|
464
|
+
await tdeps.db.deleteFrom('redirects').where('id', '=', id).execute();
|
|
465
|
+
const after = before.filter((r) => r.id !== id);
|
|
466
|
+
const looping = await allLooping(tdeps, after);
|
|
467
|
+
if (looping.size === 0)
|
|
468
|
+
return;
|
|
469
|
+
const was = await allLooping(tdeps, before);
|
|
470
|
+
const from = [...looping].find(([key]) => !was.has(key))?.[1];
|
|
471
|
+
if (from !== undefined)
|
|
472
|
+
throw new InlayError('conflict', 'error.redirectDeleteLoop', { path: from });
|
|
473
|
+
});
|
|
474
|
+
}
|
|
475
|
+
/** Every rule, stored or code, that loops with `rows` as the stored rules, keyed by its id or, for a code rule, its `from`. */
|
|
476
|
+
async function allLooping(deps, rows) {
|
|
477
|
+
const look = lookup(deps);
|
|
478
|
+
const rules = rows.map(ruleOf);
|
|
479
|
+
const code = codeRules(deps.registry);
|
|
480
|
+
const isStop = async (path) => deps.registry.getRoutePaths.has(path) || (await look(path)) !== 'not_found';
|
|
481
|
+
const looping = await loopingRules(deps.registry, isStop, rules, code, [...rules, ...code]);
|
|
482
|
+
return new Map([...looping].map((r) => [r.id === null ? `code ${r.from}` : String(r.id), r.from]));
|
|
395
483
|
}
|
|
396
484
|
/**
|
|
397
485
|
* Called by `publish` inside its transaction, before a document's published
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Registry } from '../registry/registry.js';
|
|
2
|
+
/** A clean decoded site path without a query or fragment, or `null`. */
|
|
3
|
+
export declare function cleanPath(raw: string): string | null;
|
|
4
|
+
/**
|
|
5
|
+
* The stored form of a redirect's `from` (a full http(s) URL loses its origin), or `null` if it can't be
|
|
6
|
+
* used. A default-locale prefix can't be used: such a request is sent to the unprefixed path first.
|
|
7
|
+
*/
|
|
8
|
+
export declare function redirectFrom(registry: Pick<Registry, 'defaultLocale'>, raw: string): string | null;
|
|
9
|
+
/**
|
|
10
|
+
* The first segment of a target path when it is the default locale, or lowercases to it (the lowercase
|
|
11
|
+
* fallback would take it there), else `null`. A request there is sent on to the unprefixed path, so
|
|
12
|
+
* such a target loops back.
|
|
13
|
+
*/
|
|
14
|
+
export declare function defaultLocalePrefix(registry: Pick<Registry, 'defaultLocale'>, path: string): string | null;
|
|
15
|
+
/**
|
|
16
|
+
* The `href` of an absolute https URL target, or `null` if it can't be used. Control characters are refused
|
|
17
|
+
* before parsing because the URL parser silently drops a tab or newline.
|
|
18
|
+
*/
|
|
19
|
+
export declare function externalTarget(raw: string): string | null;
|
|
20
|
+
export declare function isUrlTarget(to: string): boolean;
|
|
21
|
+
export declare function targetOf(to: string): {
|
|
22
|
+
path: string;
|
|
23
|
+
} | {
|
|
24
|
+
url: string;
|
|
25
|
+
};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { isReserved, normalizePath } from '../site/paths.js';
|
|
2
|
+
import { paramNames } from './collapse.js';
|
|
3
|
+
const ORIGIN = /^https?:\/\/[^/?#]*/i;
|
|
4
|
+
/** A clean decoded site path without a query or fragment, or `null`. */
|
|
5
|
+
export function cleanPath(raw) {
|
|
6
|
+
const path = normalizePath(raw);
|
|
7
|
+
return path === null || path.includes('?') || path.includes('#') ? null : path;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* The stored form of a redirect's `from` (a full http(s) URL loses its origin), or `null` if it can't be
|
|
11
|
+
* used. A default-locale prefix can't be used: such a request is sent to the unprefixed path first.
|
|
12
|
+
*/
|
|
13
|
+
export function redirectFrom(registry, raw) {
|
|
14
|
+
const trimmed = raw.trim();
|
|
15
|
+
const stripped = trimmed.replace(ORIGIN, '');
|
|
16
|
+
const path = cleanPath(stripped === '' && stripped !== trimmed ? '/' : stripped);
|
|
17
|
+
if (path === null || isReserved(path) || path.split('/')[1] === registry.defaultLocale)
|
|
18
|
+
return null;
|
|
19
|
+
const names = paramNames(path);
|
|
20
|
+
return new Set(names).size === names.length ? path : null;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* The first segment of a target path when it is the default locale, or lowercases to it (the lowercase
|
|
24
|
+
* fallback would take it there), else `null`. A request there is sent on to the unprefixed path, so
|
|
25
|
+
* such a target loops back.
|
|
26
|
+
*/
|
|
27
|
+
export function defaultLocalePrefix(registry, path) {
|
|
28
|
+
const first = path.split('/')[1] ?? '';
|
|
29
|
+
return first === registry.defaultLocale || first.toLowerCase() === registry.defaultLocale ? first : null;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* The `href` of an absolute https URL target, or `null` if it can't be used. Control characters are refused
|
|
33
|
+
* before parsing because the URL parser silently drops a tab or newline.
|
|
34
|
+
*/
|
|
35
|
+
export function externalTarget(raw) {
|
|
36
|
+
const trimmed = raw.trim();
|
|
37
|
+
if (/[\u0000-\u001f\u007f]/.test(trimmed) || !/^https:\/\//i.test(trimmed))
|
|
38
|
+
return null;
|
|
39
|
+
let url;
|
|
40
|
+
try {
|
|
41
|
+
url = new URL(trimmed);
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
if (url.protocol !== 'https:' || url.username !== '' || url.password !== '' || url.hostname === '')
|
|
47
|
+
return null;
|
|
48
|
+
return url.href;
|
|
49
|
+
}
|
|
50
|
+
export function isUrlTarget(to) {
|
|
51
|
+
return to.startsWith('https://');
|
|
52
|
+
}
|
|
53
|
+
export function targetOf(to) {
|
|
54
|
+
return isUrlTarget(to) ? { url: to } : { path: to };
|
|
55
|
+
}
|
|
@@ -40,6 +40,7 @@ export interface TemplateRenderProps<F extends z.ZodObject, S extends Slots> {
|
|
|
40
40
|
};
|
|
41
41
|
page: PageInfo;
|
|
42
42
|
}
|
|
43
|
+
export type OgType = 'website' | 'article';
|
|
43
44
|
export interface TemplateDef<F extends z.ZodObject = z.ZodObject, S extends Slots = Slots> {
|
|
44
45
|
kind: 'template';
|
|
45
46
|
name: string;
|
|
@@ -47,6 +48,8 @@ export interface TemplateDef<F extends z.ZodObject = z.ZodObject, S extends Slot
|
|
|
47
48
|
fields: F;
|
|
48
49
|
/** Slot order is key order. */
|
|
49
50
|
slots: S;
|
|
51
|
+
/** The page's Open Graph type (default `website`). */
|
|
52
|
+
ogType?: OgType;
|
|
50
53
|
render?(props: TemplateRenderProps<F, S>, ctx: RenderContext): Renderable;
|
|
51
54
|
}
|
|
52
55
|
export interface TemplateInput<F extends z.ZodObject, S extends Slots> {
|
|
@@ -54,6 +57,7 @@ export interface TemplateInput<F extends z.ZodObject, S extends Slots> {
|
|
|
54
57
|
label?: string;
|
|
55
58
|
fields: F;
|
|
56
59
|
slots?: S;
|
|
60
|
+
ogType?: OgType;
|
|
57
61
|
render?: (props: TemplateRenderProps<F, S>, ctx: RenderContext) => Renderable;
|
|
58
62
|
}
|
|
59
63
|
export declare function defineTemplate<F extends z.ZodObject, S extends Slots = {}>(def: TemplateInput<F, S>): TemplateDef<F, S>;
|
|
@@ -84,6 +88,8 @@ export interface CollectionDef<T extends TemplateDef = TemplateDef> {
|
|
|
84
88
|
/** URL pattern containing `:slug`; `null` for a data collection. */
|
|
85
89
|
route: string | null;
|
|
86
90
|
customPaths: boolean;
|
|
91
|
+
/** A heading for this collection in the admin's menu; collections without one go under Content when they have pages of their own, under Building blocks otherwise. */
|
|
92
|
+
group?: string;
|
|
87
93
|
titleField: string;
|
|
88
94
|
}
|
|
89
95
|
export interface CollectionInput<T extends TemplateDef = TemplateDef> {
|
|
@@ -93,6 +99,8 @@ export interface CollectionInput<T extends TemplateDef = TemplateDef> {
|
|
|
93
99
|
template: T;
|
|
94
100
|
route?: string | null;
|
|
95
101
|
customPaths?: boolean;
|
|
102
|
+
/** A heading for this collection in the admin's menu; collections without one go under Content when they have pages of their own, under Building blocks otherwise. */
|
|
103
|
+
group?: string;
|
|
96
104
|
titleField?: string;
|
|
97
105
|
}
|
|
98
106
|
export declare function defineCollection<T extends TemplateDef>(def: CollectionInput<T>): CollectionDef<T>;
|
|
@@ -105,6 +113,24 @@ export interface AdminSettings {
|
|
|
105
113
|
/** A built-in daisyUI theme name, or colors laid over a base theme. */
|
|
106
114
|
theme?: AdminTheme;
|
|
107
115
|
}
|
|
116
|
+
/**
|
|
117
|
+
* A redirect shipped in the site's code. `from` is a path (an old site's full URL is accepted; only its path is
|
|
118
|
+
* used) that may have `:name` params; `to` is a path using those params, or an absolute `https://` URL.
|
|
119
|
+
*/
|
|
120
|
+
export interface RedirectDef {
|
|
121
|
+
from: string;
|
|
122
|
+
to: string;
|
|
123
|
+
/** 301 (default) or 302. */
|
|
124
|
+
status?: 301 | 302;
|
|
125
|
+
}
|
|
126
|
+
/** Search and sharing defaults shipped in the site's code; a global's `f.seoDefaults()` values win over them. */
|
|
127
|
+
export interface SeoSettings {
|
|
128
|
+
/** Contains `%s` exactly once, where a page's title goes, such as `%s | My Site`. */
|
|
129
|
+
titleTemplate?: string;
|
|
130
|
+
description?: string;
|
|
131
|
+
/** A path under `/public/`, such as `/public/share.png`. */
|
|
132
|
+
image?: string;
|
|
133
|
+
}
|
|
108
134
|
export interface SiteDefinition {
|
|
109
135
|
locales: string[];
|
|
110
136
|
defaultLocale: string;
|
|
@@ -117,14 +143,25 @@ export interface SiteDefinition {
|
|
|
117
143
|
content: ShippedContent[];
|
|
118
144
|
guide?: string;
|
|
119
145
|
admin: AdminSettings;
|
|
146
|
+
/** Search and sharing defaults. */
|
|
147
|
+
seo: SeoSettings;
|
|
120
148
|
/** A singleton with `path: null`, rendered with status 404 when no page matches. */
|
|
121
149
|
notFound: string | null;
|
|
122
150
|
/** Exact paths that answer without the site password, such as a health check. */
|
|
123
151
|
publicPaths: string[];
|
|
124
152
|
/** Names of the secrets the host provides, such as `SLACK_WEBHOOK`. */
|
|
125
153
|
secrets: string[];
|
|
154
|
+
/**
|
|
155
|
+
* Response headers laid over Inlay's security defaults, by name; `false` leaves a default out. A header a response
|
|
156
|
+
* sets itself (the admin's CSP, a route's own) wins over both.
|
|
157
|
+
*/
|
|
158
|
+
headers: Record<string, string | false>;
|
|
126
159
|
routes: RouteDef[];
|
|
160
|
+
/** Redirects shipped with the site's code. */
|
|
161
|
+
redirects: RedirectDef[];
|
|
127
162
|
migrations: ReleaseMigration[];
|
|
163
|
+
/** The site's folder as a `file:` URL ending in `/`. Set by `loadConfig` and `loadRelease`, never by a site. */
|
|
164
|
+
siteDir?: string;
|
|
128
165
|
}
|
|
129
166
|
export interface SiteInput {
|
|
130
167
|
locales: string[];
|
|
@@ -136,10 +173,13 @@ export interface SiteInput {
|
|
|
136
173
|
content?: ShippedContent[];
|
|
137
174
|
guide?: string;
|
|
138
175
|
admin?: AdminSettings;
|
|
176
|
+
seo?: SeoSettings;
|
|
139
177
|
notFound?: string | null;
|
|
140
178
|
publicPaths?: string[];
|
|
141
179
|
secrets?: string[];
|
|
180
|
+
headers?: Record<string, string | false>;
|
|
142
181
|
routes?: RouteDef[];
|
|
182
|
+
redirects?: RedirectDef[];
|
|
143
183
|
migrations?: ReleaseMigration[];
|
|
144
184
|
}
|
|
145
185
|
export declare function defineSite(def: SiteInput): SiteDefinition;
|
|
@@ -32,10 +32,13 @@ export function defineSite(def) {
|
|
|
32
32
|
translationFallback: def.translationFallback ?? true,
|
|
33
33
|
content: def.content ?? [],
|
|
34
34
|
admin: def.admin ?? {},
|
|
35
|
+
seo: def.seo ?? {},
|
|
35
36
|
notFound: def.notFound ?? null,
|
|
36
37
|
publicPaths: def.publicPaths ?? [],
|
|
37
38
|
secrets: def.secrets ?? [],
|
|
39
|
+
headers: def.headers ?? {},
|
|
38
40
|
routes: def.routes ?? [],
|
|
41
|
+
redirects: def.redirects ?? [],
|
|
39
42
|
migrations: def.migrations ?? [],
|
|
40
43
|
};
|
|
41
44
|
}
|
|
@@ -1,5 +1,13 @@
|
|
|
1
|
+
import { type RuleIndex } from '../redirects/collapse.js';
|
|
2
|
+
import type { MatchRule } from '../types.js';
|
|
1
3
|
import { type ShippedDocument } from './content.js';
|
|
2
4
|
import type { BlockDef, SiteDefinition, TemplateDef, TypeDef } from './define.js';
|
|
5
|
+
/** A redirect rule from the site's code: `from` in its stored form, `to` a decoded path or an `href`. */
|
|
6
|
+
export interface CodeRule {
|
|
7
|
+
from: string;
|
|
8
|
+
to: string;
|
|
9
|
+
status: 301 | 302;
|
|
10
|
+
}
|
|
3
11
|
export interface Registry {
|
|
4
12
|
site: SiteDefinition;
|
|
5
13
|
locales: readonly string[];
|
|
@@ -11,6 +19,19 @@ export interface Registry {
|
|
|
11
19
|
templates: ReadonlyMap<string, TemplateDef>;
|
|
12
20
|
/** Shipped documents by type. */
|
|
13
21
|
content: ReadonlyMap<string, readonly ShippedDocument[]>;
|
|
22
|
+
/** The site's code redirects, checked, in definition order. */
|
|
23
|
+
redirects: readonly CodeRule[];
|
|
24
|
+
/** The code redirects indexed for matching. */
|
|
25
|
+
redirectIndex: RuleIndex<MatchRule>;
|
|
26
|
+
/** The global and top-level field holding `f.seoDefaults()`, if any. */
|
|
27
|
+
seoDefaults: {
|
|
28
|
+
global: string;
|
|
29
|
+
field: string;
|
|
30
|
+
} | null;
|
|
31
|
+
/** The paths of the GET code routes. */
|
|
32
|
+
getRoutePaths: ReadonlySet<string>;
|
|
33
|
+
/** The site's folder as a `file:` URL ending in `/`, when it is known. */
|
|
34
|
+
siteDir: string | null;
|
|
14
35
|
/** Throws `InlayError('not_found')` for an unknown type. */
|
|
15
36
|
getType(name: string): TypeDef;
|
|
16
37
|
}
|