inlaycms 0.2.0 → 0.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.
Files changed (121) hide show
  1. package/README.md +330 -1
  2. package/assets/admin/admin.css +1 -1
  3. package/assets/admin/admin.js +1 -1
  4. package/assets/admin/editor.js +59 -44
  5. package/dist/adapters/admin/screens/redirects.js +12 -3
  6. package/dist/adapters/admin/views/layout.js +27 -7
  7. package/dist/adapters/cli/remote.js +9 -1
  8. package/dist/adapters/mcp/tools/redirects.js +16 -2
  9. package/dist/adapters/site/assets.d.ts +11 -0
  10. package/dist/adapters/site/assets.js +71 -0
  11. package/dist/adapters/site/cache.d.ts +2 -1
  12. package/dist/adapters/site/cache.js +6 -1
  13. package/dist/adapters/site/context.d.ts +6 -0
  14. package/dist/adapters/site/context.js +38 -13
  15. package/dist/adapters/site/head.d.ts +30 -7
  16. package/dist/adapters/site/head.js +145 -20
  17. package/dist/adapters/site/headers.d.ts +5 -0
  18. package/dist/adapters/site/headers.js +31 -0
  19. package/dist/adapters/site/plugin.d.ts +2 -0
  20. package/dist/adapters/site/plugin.js +32 -9
  21. package/dist/adapters/site/public.js +7 -24
  22. package/dist/adapters/site/render.d.ts +5 -0
  23. package/dist/adapters/site/render.js +23 -8
  24. package/dist/adapters/site/routes.d.ts +6 -0
  25. package/dist/adapters/site/routes.js +8 -3
  26. package/dist/adapters/site/seo.d.ts +20 -0
  27. package/dist/adapters/site/seo.js +45 -0
  28. package/dist/adapters/site/sitemap.d.ts +1 -1
  29. package/dist/adapters/site/sitemap.js +1 -1
  30. package/dist/config.js +3 -2
  31. package/dist/core/agent/format.js +1 -1
  32. package/dist/core/agent/schema.js +3 -0
  33. package/dist/core/boot.js +7 -0
  34. package/dist/core/check.js +6 -1
  35. package/dist/core/content/dry-run.d.ts +7 -6
  36. package/dist/core/content/dry-run.js +8 -8
  37. package/dist/core/content/publish.d.ts +1 -1
  38. package/dist/core/content/publish.js +5 -3
  39. package/dist/core/content/read.d.ts +3 -0
  40. package/dist/core/content/reader.js +3 -2
  41. package/dist/core/content/refs.js +1 -1
  42. package/dist/core/content/search.js +2 -2
  43. package/dist/core/content/shipped-images.d.ts +32 -0
  44. package/dist/core/content/shipped-images.js +62 -0
  45. package/dist/core/content/shipped-input.d.ts +9 -3
  46. package/dist/core/content/shipped-input.js +165 -28
  47. package/dist/core/content/shipped.js +8 -3
  48. package/dist/core/content/validate.js +7 -1
  49. package/dist/core/core.d.ts +5 -1
  50. package/dist/core/core.js +5 -4
  51. package/dist/core/db/connection.d.ts +6 -0
  52. package/dist/core/db/connection.js +13 -1
  53. package/dist/core/db/migrate.js +2 -0
  54. package/dist/core/db/migrations/0007_published_at.d.ts +3 -0
  55. package/dist/core/db/migrations/0007_published_at.js +16 -0
  56. package/dist/core/db/schema.d.ts +1 -0
  57. package/dist/core/export/export.js +3 -1
  58. package/dist/core/export/format.d.ts +8 -1
  59. package/dist/core/export/format.js +3 -2
  60. package/dist/core/export/read.js +7 -0
  61. package/dist/core/export/stage.d.ts +1 -1
  62. package/dist/core/export/stage.js +24 -1
  63. package/dist/core/fields/builtins.d.ts +28 -0
  64. package/dist/core/fields/builtins.js +103 -11
  65. package/dist/core/fields/define.d.ts +6 -0
  66. package/dist/core/fields/widgets.d.ts +7 -0
  67. package/dist/core/fields/widgets.js +76 -28
  68. package/dist/core/messages/en.d.ts +70 -6
  69. package/dist/core/messages/en.js +70 -6
  70. package/dist/core/redirects/collapse.d.ts +17 -1
  71. package/dist/core/redirects/collapse.js +19 -3
  72. package/dist/core/redirects/redirects.d.ts +13 -12
  73. package/dist/core/redirects/redirects.js +141 -53
  74. package/dist/core/redirects/rule.d.ts +25 -0
  75. package/dist/core/redirects/rule.js +55 -0
  76. package/dist/core/registry/define.d.ts +40 -0
  77. package/dist/core/registry/define.js +3 -0
  78. package/dist/core/registry/registry.d.ts +21 -0
  79. package/dist/core/registry/registry.js +114 -6
  80. package/dist/core/registry/render.d.ts +37 -1
  81. package/dist/core/registry/route.d.ts +4 -0
  82. package/dist/core/release/bundle.js +30 -5
  83. package/dist/core/release/manifest.d.ts +14 -0
  84. package/dist/core/release/manifest.js +42 -17
  85. package/dist/core/release/markdown-file.d.ts +21 -0
  86. package/dist/core/release/markdown-file.js +172 -0
  87. package/dist/core/release/pack.js +2 -2
  88. package/dist/core/release/read.js +4 -1
  89. package/dist/core/release/site-dir.d.ts +4 -0
  90. package/dist/core/release/site-dir.js +8 -0
  91. package/dist/core/report.d.ts +8 -2
  92. package/dist/core/richtext/html.d.ts +3 -1
  93. package/dist/core/richtext/html.js +16 -4
  94. package/dist/core/richtext/inline.d.ts +22 -0
  95. package/dist/core/richtext/inline.js +33 -0
  96. package/dist/core/richtext/markdown.d.ts +16 -1
  97. package/dist/core/richtext/markdown.js +137 -14
  98. package/dist/core/richtext/schema.d.ts +16 -4
  99. package/dist/core/richtext/schema.js +71 -1
  100. package/dist/core/site/live.d.ts +1 -0
  101. package/dist/core/site/live.js +1 -0
  102. package/dist/core/site/paths.d.ts +6 -0
  103. package/dist/core/site/paths.js +13 -0
  104. package/dist/core/site/resolve.d.ts +1 -1
  105. package/dist/core/site/resolve.js +119 -30
  106. package/dist/core/site/sitemap.d.ts +1 -1
  107. package/dist/core/site/sitemap.js +4 -0
  108. package/dist/core/types.d.ts +28 -3
  109. package/dist/host/app.d.ts +5 -0
  110. package/dist/host/app.js +8 -0
  111. package/dist/host/dev.js +3 -7
  112. package/dist/host/diff.d.ts +19 -1
  113. package/dist/host/diff.js +32 -2
  114. package/dist/index.d.ts +2 -2
  115. package/dist/markdown-modules.d.ts +8 -0
  116. package/dist/markdown-modules.js +1 -0
  117. package/dist/site.d.ts +8 -3
  118. package/dist/site.js +6 -0
  119. package/dist/testing.d.ts +22 -0
  120. package/dist/testing.js +155 -0
  121. package/package.json +6 -1
@@ -1,17 +1,34 @@
1
1
  import { resolveTheme } from '../theme.js';
2
2
  import { InlayError } from '../errors.js';
3
+ import { isTitleTemplate } from '../fields/builtins.js';
3
4
  import { fieldMeta } from '../fields/define.js';
4
5
  import { defOf, unwrap } from '../fields/zod-internals.js';
5
6
  import { isReserved, normalizePath, routeRegExp, singletonPath, withLocale } from '../site/paths.js';
7
+ import { paramNames, ruleIndex } from '../redirects/collapse.js';
8
+ import { cleanPath, defaultLocalePrefix, externalTarget, redirectFrom } from '../redirects/rule.js';
6
9
  import { KEY_PATTERN } from './content.js';
7
10
  const LOCALE = /^[a-z]{2,3}(-[A-Za-z0-9]+)*$/;
8
11
  const NAME = /^[a-z][A-Za-z0-9_]*$/;
9
12
  const SECRET = /^[A-Z][A-Z0-9_]*$/;
13
+ /** An HTTP header name (RFC 9110's token). */
14
+ const HEADER_NAME = /^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/;
15
+ /** Headers Inlay sets on its own responses, which a site's `headers` can't replace. */
16
+ const OWN_HEADERS = new Set([
17
+ 'cache-control',
18
+ 'connection',
19
+ 'content-length',
20
+ 'content-type',
21
+ 'etag',
22
+ 'location',
23
+ 'set-cookie',
24
+ 'transfer-encoding',
25
+ 'vary',
26
+ ]);
10
27
  function fail(messageId, params) {
11
28
  throw new InlayError('definition', messageId, params);
12
29
  }
13
30
  /** Checks every field of `shape` at any depth; `where` is the developer-facing dotted path prefix. */
14
- function checkShape(shape, where, collections) {
31
+ function checkShape(shape, where, collections, nested) {
15
32
  for (const [key, schema] of Object.entries(shape)) {
16
33
  const field = `${where}.${key}`;
17
34
  if (!NAME.test(key))
@@ -22,13 +39,17 @@ function checkShape(shape, where, collections) {
22
39
  if (meta.kind === 'ref' && (meta.target === undefined || !collections.has(meta.target))) {
23
40
  fail('definition.refUnknown', { field, target: meta.target ?? '' });
24
41
  }
42
+ if (meta.seoDefaults && nested)
43
+ fail('definition.seoDefaultsPlace', { field });
25
44
  const def = defOf(unwrap(schema));
26
45
  if (meta.kind === 'group' && def.shape)
27
- checkShape(def.shape, field, collections);
46
+ checkShape(def.shape, field, collections, true);
28
47
  if (meta.kind === 'list' && def.element) {
48
+ if (fieldMeta(def.element)?.seoDefaults)
49
+ fail('definition.seoDefaultsPlace', { field });
29
50
  const item = defOf(unwrap(def.element));
30
51
  if (item.shape)
31
- checkShape(item.shape, field, collections);
52
+ checkShape(item.shape, field, collections, true);
32
53
  }
33
54
  }
34
55
  }
@@ -36,11 +57,14 @@ function checkTemplate(template, collections, checked) {
36
57
  if (checked.has(template))
37
58
  return;
38
59
  checked.add(template);
39
- checkShape(template.fields.shape, template.name, collections);
60
+ if (template.ogType !== undefined && template.ogType !== 'website' && template.ogType !== 'article') {
61
+ fail('definition.ogType', { name: template.name, value: String(template.ogType) });
62
+ }
63
+ checkShape(template.fields.shape, template.name, collections, false);
40
64
  for (const [key, slot] of Object.entries(template.slots)) {
41
65
  if (!NAME.test(key))
42
66
  fail('definition.slotName', { name: key, template: template.name });
43
- checkShape(slot.block.schema.shape, slot.block.name, collections);
67
+ checkShape(slot.block.schema.shape, slot.block.name, collections, true);
44
68
  }
45
69
  }
46
70
  function checkPath(name, path, locales) {
@@ -82,6 +106,15 @@ function checkType(type, site) {
82
106
  }
83
107
  function checkSettings(site, types) {
84
108
  resolveTheme(site.admin.theme);
109
+ const { titleTemplate, description, image } = site.seo;
110
+ if (titleTemplate !== undefined && (typeof titleTemplate !== 'string' || !isTitleTemplate(titleTemplate))) {
111
+ fail('definition.seoTitleTemplate', { value: String(titleTemplate) });
112
+ }
113
+ if (image !== undefined && (typeof image !== 'string' || !image.startsWith('/public/') || image.length === '/public/'.length)) {
114
+ fail('definition.seoImage', { value: String(image) });
115
+ }
116
+ if (description !== undefined && typeof description !== 'string')
117
+ fail('definition.seoDescription', {});
85
118
  if (site.notFound !== null) {
86
119
  const def = types.get(site.notFound);
87
120
  if (!def || def.kind !== 'singleton' || def.path !== null || !def.template.render) {
@@ -98,6 +131,16 @@ function checkSettings(site, types) {
98
131
  fail('definition.secretName', { name: String(name) });
99
132
  secrets.add(name);
100
133
  }
134
+ const headers = new Set();
135
+ for (const [name, value] of Object.entries(site.headers)) {
136
+ const lower = name.toLowerCase();
137
+ if (!HEADER_NAME.test(name) || OWN_HEADERS.has(lower) || headers.has(lower))
138
+ fail('definition.headerName', { name });
139
+ if (value !== false && (typeof value !== 'string' || /[\u0000-\u001f\u007f]/.test(value))) {
140
+ fail('definition.headerValue', { name });
141
+ }
142
+ headers.add(lower);
143
+ }
101
144
  const migrations = new Set();
102
145
  for (const { name } of site.migrations) {
103
146
  if (typeof name !== 'string' || name.trim() === '' || migrations.has(name))
@@ -127,12 +170,14 @@ function shadowedType(registry, path) {
127
170
  }
128
171
  function checkRoutes(registry) {
129
172
  const seen = new Set();
130
- for (const { method, path } of registry.site.routes) {
173
+ for (const { method, path, sitemap } of registry.site.routes) {
131
174
  if (method !== 'GET' && method !== 'POST')
132
175
  fail('definition.routeMethod', { method: String(method), path: String(path) });
133
176
  if (typeof path !== 'string' || normalizePath(path) !== path || /[:*?#%]/.test(path)) {
134
177
  fail('definition.routePath', { path: String(path) });
135
178
  }
179
+ if (sitemap !== undefined && (sitemap !== true || method !== 'GET'))
180
+ fail('definition.routeSitemap', { method, path });
136
181
  if (isRouteReserved(path))
137
182
  fail('definition.routeReserved', { path });
138
183
  const name = shadowedType(registry, path);
@@ -144,6 +189,49 @@ function checkRoutes(registry) {
144
189
  seen.add(key);
145
190
  }
146
191
  }
192
+ function checkRedirects(site, getPaths) {
193
+ const rules = [];
194
+ const seen = new Set();
195
+ for (const def of site.redirects) {
196
+ const shown = typeof def?.from === 'string' ? def.from : String(def?.from);
197
+ const from = typeof def?.from === 'string' ? redirectFrom({ defaultLocale: site.defaultLocale }, def.from) : null;
198
+ if (from === null)
199
+ fail('definition.redirectFrom', { from: shown });
200
+ const status = def.status ?? 301;
201
+ if (status !== 301 && status !== 302)
202
+ fail('definition.redirectStatus', { from, status: String(status) });
203
+ const bad = () => fail('definition.redirectTo', { from, to: String(def.to) });
204
+ if (typeof def.to !== 'string')
205
+ bad();
206
+ const names = paramNames(from);
207
+ let to;
208
+ if (def.to.startsWith('/')) {
209
+ const path = cleanPath(def.to);
210
+ if (path === null || defaultLocalePrefix(site, path) !== null)
211
+ bad();
212
+ to = path;
213
+ for (const name of paramNames(to))
214
+ if (!names.includes(name))
215
+ fail('definition.redirectParam', { from, name });
216
+ }
217
+ else {
218
+ const href = externalTarget(def.to);
219
+ if (href === null)
220
+ bad();
221
+ to = href;
222
+ const [name] = names.length > 0 ? paramNames(new URL(to).pathname) : [];
223
+ if (name !== undefined)
224
+ fail('definition.redirectUrlParam', { from, name });
225
+ }
226
+ if (seen.has(from))
227
+ fail('definition.redirectDuplicate', { from });
228
+ seen.add(from);
229
+ if (getPaths.has(from))
230
+ fail('definition.redirectRoute', { from });
231
+ rules.push({ from, to, status });
232
+ }
233
+ return rules;
234
+ }
147
235
  /** Checks a site definition and indexes its types. Throws `InlayError('definition')` naming the offender. */
148
236
  export function buildRegistry(site) {
149
237
  const seenLocales = new Set();
@@ -199,6 +287,19 @@ export function buildRegistry(site) {
199
287
  }
200
288
  }
201
289
  checkSettings(site, types);
290
+ let seoDefaults = null;
291
+ for (const type of all) {
292
+ for (const [key, schema] of Object.entries(type.template.fields.shape)) {
293
+ if (!fieldMeta(schema)?.seoDefaults)
294
+ continue;
295
+ const here = `${type.name}.${key}`;
296
+ if (type.kind !== 'global')
297
+ fail('definition.seoDefaultsPlace', { field: `${type.template.name}.${key}` });
298
+ if (seoDefaults)
299
+ fail('definition.seoDefaultsTwice', { first: `${seoDefaults.global}.${seoDefaults.field}`, second: here });
300
+ seoDefaults = { global: type.name, field: key };
301
+ }
302
+ }
202
303
  const content = new Map();
203
304
  for (const shipped of site.content) {
204
305
  const type = types.get(shipped.type);
@@ -235,6 +336,8 @@ export function buildRegistry(site) {
235
336
  }
236
337
  content.set(shipped.type, shipped.documents);
237
338
  }
339
+ const getRoutePaths = new Set(site.routes.filter((r) => r.method === 'GET').map((r) => r.path));
340
+ const redirects = checkRedirects(site, getRoutePaths);
238
341
  const registry = {
239
342
  site,
240
343
  locales: [...site.locales],
@@ -243,6 +346,11 @@ export function buildRegistry(site) {
243
346
  blocks,
244
347
  templates,
245
348
  content,
349
+ redirects,
350
+ seoDefaults,
351
+ redirectIndex: ruleIndex(redirects.map((r) => ({ from: r.from, to_path: r.to, to_group_id: null, status: r.status }))),
352
+ getRoutePaths,
353
+ siteDir: site.siteDir ?? null,
246
354
  getType(name) {
247
355
  const type = types.get(name);
248
356
  if (!type)
@@ -42,11 +42,13 @@ export interface Entry<F = Record<string, unknown>> {
42
42
  title: string;
43
43
  slug: string | null;
44
44
  updatedAt: string;
45
+ /** When Inlay first published it; `null` if it never has been (a draft in preview). */
46
+ publishedAt: string | null;
45
47
  fields: F;
46
48
  }
47
49
  export interface QueryOptions<F = Record<string, unknown>> {
48
50
  filter?: (e: Entry<F>) => boolean;
49
- sort?: 'newest' | 'oldest' | {
51
+ sort?: 'newest' | 'oldest' | 'published' | {
50
52
  field: string;
51
53
  direction?: 'asc' | 'desc';
52
54
  };
@@ -70,8 +72,36 @@ export interface PageInfo {
70
72
  fallback: boolean;
71
73
  /** The decoded site path; `''` for a page without one. */
72
74
  path: string;
75
+ /** When Inlay first published the page's document; `null` for a route or a never-published draft. */
76
+ publishedAt: string | null;
73
77
  page: number;
74
78
  }
79
+ export type HeadMeta = {
80
+ name: string;
81
+ content: string;
82
+ } | {
83
+ property: string;
84
+ content: string;
85
+ };
86
+ export interface HeadLink {
87
+ rel: string;
88
+ href: string;
89
+ hreflang?: string;
90
+ type?: string;
91
+ sizes?: string;
92
+ media?: string;
93
+ as?: string;
94
+ crossorigin?: string;
95
+ }
96
+ /** Adds tags to the page's `<head>`, after Inlay's own. */
97
+ export interface HeadApi {
98
+ /** A `<script type="application/ld+json">` of `value`. Throws when `value` can't be written as JSON. */
99
+ jsonLd(value: unknown): void;
100
+ /** Replaces Inlay's meta with the same name or property; the last call for a key wins. */
101
+ meta(attrs: HeadMeta): void;
102
+ /** Replaces Inlay's link with the same `rel` and `hreflang`. Throws on an unsafe `href`. */
103
+ link(attrs: HeadLink): void;
104
+ }
75
105
  export interface RenderContext {
76
106
  locale: string;
77
107
  defaultLocale: string;
@@ -79,6 +109,10 @@ export interface RenderContext {
79
109
  preview: boolean;
80
110
  page: number;
81
111
  baseUrl: string;
112
+ /** The `/public/` URL for a file of the site's `public/` folder, with `?v=<hash>` when the file exists. Never throws. */
113
+ asset(path: string): string;
114
+ /** Tags for the page's `<head>`, collected during this render. */
115
+ head: HeadApi;
82
116
  alternates: Array<{
83
117
  locale: string;
84
118
  path: string;
@@ -90,6 +124,8 @@ export interface RenderContext {
90
124
  /** `null` when the target document isn't live. Text is the value's, then the target's title, then the URL. */
91
125
  link(v: LinkValue | null | undefined): ResolvedLink | null;
92
126
  richText(doc: RichTextDoc | null | undefined): SafeHtml;
127
+ /** An inline-text value's first paragraph without its `<p>`, for a heading or a tagline. A string is escaped. */
128
+ inline(value: RichTextDoc | string | null | undefined): SafeHtml;
93
129
  /** Live entries in the given order; ones that aren't live are skipped. */
94
130
  entries<F = Record<string, unknown>>(ids: string | readonly string[] | null | undefined): Promise<Entry<F>[]>;
95
131
  /** Every live entry of the collection, filtered, sorted (`newest` by default) and paginated. */
@@ -32,11 +32,15 @@ export interface RouteContext {
32
32
  secrets: Readonly<Record<string, string>>;
33
33
  redirect(location: string, status?: 302 | 303): RouteResponse;
34
34
  baseUrl: string;
35
+ /** The `/public/` URL for a file of the site's `public/` folder, with `?v=<hash>` when the file exists. Never throws. */
36
+ asset(path: string): string;
35
37
  locale: string;
36
38
  }
37
39
  export interface RouteDef {
38
40
  method: RouteMethod;
39
41
  path: string;
42
+ /** List this GET route in the sitemap; its rendered pages get a canonical link and can be indexed. */
43
+ sitemap?: true;
40
44
  handler: (request: RouteRequest, ctx: RouteContext) => Promise<RouteResponse> | RouteResponse;
41
45
  }
42
46
  export declare function defineRoute(def: RouteDef): RouteDef;
@@ -1,12 +1,15 @@
1
1
  import { randomBytes } from 'node:crypto';
2
- import { existsSync } from 'node:fs';
2
+ import { existsSync, realpathSync } from 'node:fs';
3
3
  import { readFile } from 'node:fs/promises';
4
4
  import { builtinModules } from 'node:module';
5
5
  import { dirname, extname, isAbsolute, join, relative, resolve } from 'node:path';
6
6
  import { fileURLToPath, pathToFileURL } from 'node:url';
7
7
  import { build, transform } from 'esbuild';
8
8
  import { InlayError } from '../errors.js';
9
+ import { t } from '../messages/index.js';
10
+ import { rewriteImageTargets } from './markdown-file.js';
9
11
  import { INLAY_ENTRIES } from './read.js';
12
+ import { SITE_DIR_EXPORT, siteDirUrl } from './site-dir.js';
10
13
  /** What a release's `site.js` may import; everything else is bundled into it or refused. */
11
14
  export const RELEASE_IMPORTS = ['inlaycms/site', 'inlaycms/jsx-runtime', 'inlaycms/jsx-dev-runtime'];
12
15
  /** Free names site code may not use: they reach the server's process or run code from text. */
@@ -111,6 +114,27 @@ async function withOwnUrl(text, file, loader, tsconfigRaw) {
111
114
  });
112
115
  return { contents: out.code, loader: loader === 'tsx' || loader === 'jsx' ? 'jsx' : 'js' };
113
116
  }
117
+ /** `file` relative to the site's folder `base`, or as it is when it isn't inside it. */
118
+ function shownPath(base, file) {
119
+ const rel = relative(base, file);
120
+ return rel === '' || rel.startsWith('..') || isAbsolute(rel) ? file : rel;
121
+ }
122
+ /**
123
+ * A `.md` file at `file` as a module whose default export is its text, with its images' targets rewritten from
124
+ * the file's own folder; a reference-style image is refused with `error.markdownImageReference`, and one whose
125
+ * place in the file can't be found with `error.markdownImageUnplaced`.
126
+ */
127
+ async function loadMarkdown(file, base) {
128
+ const result = rewriteImageTargets(await readFile(file, 'utf8'), file);
129
+ const line = result.references[0];
130
+ if (line !== undefined)
131
+ return { errors: [{ text: t('error.markdownImageReference', { file: shownPath(base, file), line }) }] };
132
+ const unplaced = result.unplaced[0];
133
+ if (unplaced !== undefined) {
134
+ return { errors: [{ text: t('error.markdownImageUnplaced', { file: shownPath(base, file), line: unplaced }) }] };
135
+ }
136
+ return { contents: result.text, loader: 'text' };
137
+ }
114
138
  /** The esbuild options `bundleSite` and `bundleConfig` share. */
115
139
  function buildOptions(configFile, outFile, plugin) {
116
140
  return {
@@ -124,6 +148,7 @@ function buildOptions(configFile, outFile, plugin) {
124
148
  jsxImportSource: 'inlaycms',
125
149
  define: { ...DEFINES, 'import.meta.url': JSON.stringify(pathToFileURL(resolve(configFile)).href) },
126
150
  logLevel: 'silent',
151
+ footer: { js: `export const ${SITE_DIR_EXPORT} = ${JSON.stringify(siteDirUrl(realpathSync(dirname(resolve(configFile)))))};` },
127
152
  plugins: [plugin],
128
153
  };
129
154
  }
@@ -137,6 +162,7 @@ export async function bundleConfig(configFile, outFile) {
137
162
  const tsconfigFor = tsconfigs();
138
163
  // The running copy's own folder (`src/` or `dist/`): a file in it is imported, never bundled as a second copy.
139
164
  const inlayDir = dirname(INLAY_ENTRIES.inlaycms ?? '');
165
+ const base = dirname(resolve(configFile));
140
166
  const resolving = Symbol('resolving');
141
167
  const plugin = {
142
168
  name: 'inlay-config',
@@ -167,6 +193,7 @@ export async function bundleConfig(configFile, outFile) {
167
193
  return undefined;
168
194
  return withOwnUrl(await readFile(args.path, 'utf8'), args.path, loader, await tsconfigFor(dirname(args.path)));
169
195
  });
196
+ b.onLoad({ filter: /\.md$/ }, (args) => (args.namespace === 'file' ? loadMarkdown(args.path, base) : undefined));
170
197
  },
171
198
  };
172
199
  try {
@@ -188,10 +215,7 @@ export async function bundleConfig(configFile, outFile) {
188
215
  */
189
216
  export async function bundleSite(configFile, outFile) {
190
217
  const base = dirname(resolve(configFile));
191
- const shown = (file) => {
192
- const rel = relative(base, file);
193
- return rel === '' || rel.startsWith('..') || isAbsolute(rel) ? file : rel;
194
- };
218
+ const shown = (file) => shownPath(base, file);
195
219
  const refused = [];
196
220
  const forbidden = [];
197
221
  const tsconfigFor = tsconfigs();
@@ -221,6 +245,7 @@ export async function bundleSite(configFile, outFile) {
221
245
  }
222
246
  return withOwnUrl(text, args.path, loader, await tsconfigFor(dirname(args.path)));
223
247
  });
248
+ b.onLoad({ filter: /\.md$/ }, (args) => (args.namespace === 'file' ? loadMarkdown(args.path, base) : undefined));
224
249
  },
225
250
  };
226
251
  let result;
@@ -1,6 +1,11 @@
1
1
  import { z } from 'zod';
2
2
  import type { Registry } from '../registry/registry.js';
3
3
  export declare const MANIFEST_FORMAT = 1;
4
+ export type ManifestRedirect = {
5
+ from: string;
6
+ to: string;
7
+ status: 301 | 302;
8
+ };
4
9
  /** What `manifest.json` in a release says; `manifest.sig` signs its bytes. */
5
10
  export interface ReleaseManifest {
6
11
  format: 1;
@@ -36,7 +41,10 @@ export interface ReleaseManifest {
36
41
  routes: Array<{
37
42
  method: string;
38
43
  path: string;
44
+ sitemap?: true;
39
45
  }>;
46
+ /** The site's code redirects in stored form; absent in manifests made before 0.3, which means none. */
47
+ redirects?: ManifestRedirect[];
40
48
  locales: string[];
41
49
  /** Each type's kind and slot names, by type name. */
42
50
  types: Record<string, {
@@ -69,7 +77,13 @@ export declare const manifestSchema: z.ZodObject<{
69
77
  routes: z.ZodArray<z.ZodObject<{
70
78
  method: z.ZodString;
71
79
  path: z.ZodString;
80
+ sitemap: z.ZodOptional<z.ZodLiteral<true>>;
72
81
  }, z.core.$strict>>;
82
+ redirects: z.ZodOptional<z.ZodArray<z.ZodObject<{
83
+ from: z.ZodString;
84
+ to: z.ZodString;
85
+ status: z.ZodUnion<readonly [z.ZodLiteral<301>, z.ZodLiteral<302>]>;
86
+ }, z.core.$strict>>>;
73
87
  locales: z.ZodArray<z.ZodString>;
74
88
  types: z.ZodRecord<z.ZodString, z.ZodObject<{
75
89
  kind: z.ZodEnum<{
@@ -2,9 +2,12 @@ import { createHash } from 'node:crypto';
2
2
  import { readFileSync } from 'node:fs';
3
3
  import { extname } from 'node:path';
4
4
  import { z } from 'zod';
5
- import { shippedFiles } from '../content/shipped-input.js';
5
+ import { imageTarget } from '../content/shipped-images.js';
6
+ import { mapRichText, shippedFiles } from '../content/shipped-input.js';
6
7
  import { typeSchemas } from '../export/schema.js';
8
+ import { markdownImages } from '../richtext/markdown.js';
7
9
  import { inlayVersion } from '../version.js';
10
+ import { replaceImageTargets } from './markdown-file.js';
8
11
  export const MANIFEST_FORMAT = 1;
9
12
  const SHA256 = /^[0-9a-f]{64}$/;
10
13
  export const manifestSchema = z.strictObject({
@@ -23,7 +26,10 @@ export const manifestSchema = z.strictObject({
23
26
  locales: z.record(z.string(), z.string().regex(SHA256)),
24
27
  })),
25
28
  migrations: z.array(z.string()),
26
- routes: z.array(z.strictObject({ method: z.string(), path: z.string() })),
29
+ routes: z.array(z.strictObject({ method: z.string(), path: z.string(), sitemap: z.literal(true).optional() })),
30
+ redirects: z
31
+ .array(z.strictObject({ from: z.string(), to: z.string(), status: z.union([z.literal(301), z.literal(302)]) }))
32
+ .optional(),
27
33
  locales: z.array(z.string()),
28
34
  types: z.record(z.string(), z.strictObject({ kind: z.enum(['singleton', 'collection', 'global']), slots: z.array(z.string()) })),
29
35
  });
@@ -77,17 +83,35 @@ export function canonicalJson(value) {
77
83
  }
78
84
  /** `value` with each `{ file }` whose source `paths` knows naming the release's copy instead. */
79
85
  function withReleaseFiles(value, paths) {
80
- if (Array.isArray(value))
81
- return value.map((v) => withReleaseFiles(v, paths));
82
- if (value === null || typeof value !== 'object' || value instanceof URL || value instanceof Date)
83
- return value;
84
- const out = {};
85
- for (const [key, v] of Object.entries(value)) {
86
- const source = key === 'file' ? (v instanceof URL ? v.href : typeof v === 'string' ? v : null) : null;
87
- const path = source === null ? undefined : paths.get(source);
88
- out[key] = path ?? withReleaseFiles(v, paths);
89
- }
90
- return out;
86
+ const walk = (value) => {
87
+ if (Array.isArray(value))
88
+ return value.map(walk);
89
+ if (value === null || typeof value !== 'object' || value instanceof URL || value instanceof Date)
90
+ return value;
91
+ const out = {};
92
+ for (const [key, v] of Object.entries(value)) {
93
+ const source = key === 'file' ? (v instanceof URL ? v.href : typeof v === 'string' ? v : null) : null;
94
+ const path = source === null ? undefined : paths.get(source);
95
+ out[key] = path ?? walk(v);
96
+ }
97
+ return out;
98
+ };
99
+ return walk(value);
100
+ }
101
+ /**
102
+ * Rich-text Markdown with each image naming a file `paths` knows (by a relative path or a `file:` URL) naming the
103
+ * release's copy instead, so its hash follows the file's bytes and not where the site sits. An image whose place in
104
+ * the text can't be found is listed after the text by where the release keeps it.
105
+ */
106
+ function withReleaseImages(markdown, siteDir, paths) {
107
+ const releaseCopy = (src) => {
108
+ const target = imageTarget(src, siteDir);
109
+ return target.kind === 'file' ? (paths.get(target.url) ?? null) : null;
110
+ };
111
+ const { text, complete } = replaceImageTargets(markdown, releaseCopy);
112
+ if (complete)
113
+ return text;
114
+ return { markdown: text, images: markdownImages(markdown).map(({ src }) => releaseCopy(src) ?? src) };
91
115
  }
92
116
  /**
93
117
  * The manifest of the site `registry` describes, without its files: what `packRelease` writes, given the shipped
@@ -102,9 +126,9 @@ export function manifestOfRegistry(registry, opts) {
102
126
  paths.set(source, path);
103
127
  }
104
128
  else {
105
- for (const { source, path } of shippedFiles(registry)) {
129
+ for (const { source, path, read } of shippedFiles(registry)) {
106
130
  try {
107
- paths.set(source, contentPath(path, readFileSync(path)));
131
+ paths.set(source, contentPath(path, readFileSync(read)));
108
132
  }
109
133
  catch {
110
134
  // Left as its source; applying the content reports it.
@@ -127,11 +151,12 @@ export function manifestOfRegistry(registry, opts) {
127
151
  edition: doc.edition,
128
152
  locales: Object.fromEntries(Object.entries(doc.content).map(([locale, input]) => [
129
153
  locale,
130
- sha256Hex(Buffer.from(canonicalJson(withReleaseFiles(input, paths)))),
154
+ sha256Hex(Buffer.from(canonicalJson(withReleaseFiles(mapRichText(registry, type, input, (markdown) => withReleaseImages(markdown, registry.siteDir, paths)), paths)))),
131
155
  ])),
132
156
  }))),
133
157
  migrations: site.migrations.map((m) => m.name),
134
- routes: site.routes.map(({ method, path }) => ({ method, path })),
158
+ routes: site.routes.map((r) => ({ method: r.method, path: r.path, ...(r.sitemap === true && { sitemap: true }) })),
159
+ redirects: registry.redirects.map(({ from, to, status }) => ({ from, to, status })),
135
160
  locales: [...registry.locales],
136
161
  types: Object.fromEntries([...registry.types].map(([name, def]) => [name, { kind: def.kind, slots: Object.keys(def.template.slots) }])),
137
162
  };
@@ -0,0 +1,21 @@
1
+ /**
2
+ * The Markdown file `text`, read from `file`, with each relative inline image target rewritten to the `file:` URL it
3
+ * names from the file's own folder, so the image resolves the same once the text is bundled. Everything else, line
4
+ * endings included, is left as written, and every line stays where it was. `references` lists the line (from 1) of
5
+ * each reference-style image, which is never resolved. `unplaced` lists the line (from 1) of each relative image
6
+ * whose place in the source can't be found (a line with a NUL character, say), left as written; one in a table cell
7
+ * with an escaped pipe isn't listed, since such a cell is refused when the text is read as rich text.
8
+ */
9
+ export declare function rewriteImageTargets(text: string, file: string): {
10
+ text: string;
11
+ references: number[];
12
+ unplaced: number[];
13
+ };
14
+ /**
15
+ * `markdown` with each inline image target for which `to` gives a new target rewritten to it, the way
16
+ * `rewriteImageTargets` rewrites them. `complete` is false if one of them couldn't be placed and was left as written.
17
+ */
18
+ export declare function replaceImageTargets(markdown: string, to: (src: string) => string | null): {
19
+ text: string;
20
+ complete: boolean;
21
+ };