mdorigin 0.7.2 → 0.8.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/dist/adapters/cloudflare.js +45 -0
- package/dist/core/asset-urls.d.ts +34 -0
- package/dist/core/asset-urls.js +90 -0
- package/dist/core/markdown.d.ts +14 -1
- package/dist/core/markdown.js +43 -2
- package/dist/core/request-handler.d.ts +8 -0
- package/dist/core/request-handler.js +14 -4
- package/dist/core/site-config.d.ts +15 -0
- package/dist/core/site-config.js +22 -0
- package/dist/index-builder.js +15 -1
- package/package.json +1 -1
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { MemoryContentStore } from '../core/content-store.js';
|
|
2
|
+
import { createExternalAssetUrlResolver } from '../core/asset-urls.js';
|
|
2
3
|
import { handleSiteRequest } from '../core/request-handler.js';
|
|
3
4
|
import { resolveRequest } from '../core/router.js';
|
|
4
5
|
import { createSearchApiFromBundle, createSearchApiFromExternalBundle, } from '../search.js';
|
|
@@ -6,6 +7,22 @@ const BROWSER_CACHE_CONTROL = 'public, max-age=120, stale-while-revalidate=60480
|
|
|
6
7
|
const CDN_CACHE_CONTROL = 'max-age=86400';
|
|
7
8
|
const L2_CACHE_CONTROL = 'public, max-age=86400';
|
|
8
9
|
const L2_CACHE_KEY_ORIGIN = 'https://cache.mdorigin.internal';
|
|
10
|
+
/**
|
|
11
|
+
* Browser cache policy for R2-backed binary assets in external binary mode.
|
|
12
|
+
* Public paths stay stable while the underlying object can change on a
|
|
13
|
+
* redeploy, so responses must stay revalidatable (no immutable): browsers
|
|
14
|
+
* reuse images for a day, then revalidate in the background while stale
|
|
15
|
+
* content keeps rendering. Without this header browsers fall back to
|
|
16
|
+
* Cloudflare's default max-age=0 must-revalidate and pay one conditional
|
|
17
|
+
* round trip per image on every visit.
|
|
18
|
+
*/
|
|
19
|
+
const EXTERNAL_BINARY_BROWSER_CACHE_CONTROL = 'public, max-age=86400, stale-while-revalidate=604800';
|
|
20
|
+
/**
|
|
21
|
+
* Edge cache policy for R2-backed binary assets. Mirrors the HTML CDN policy
|
|
22
|
+
* so edge hits are deterministic while a redeploy can serve stale bytes for
|
|
23
|
+
* at most one day.
|
|
24
|
+
*/
|
|
25
|
+
const EXTERNAL_BINARY_CDN_CACHE_CONTROL = 'max-age=86400';
|
|
9
26
|
const CACHE_DEBUG_HEADER = 'x-mdorigin-cache';
|
|
10
27
|
const RESPONSE_CACHE_LIMIT_DEFAULT = 100;
|
|
11
28
|
const SEARCH_CACHE_LIMIT_DEFAULT = 100;
|
|
@@ -49,6 +66,12 @@ export function createCloudflareWorker(manifest, options = {}) {
|
|
|
49
66
|
: undefined;
|
|
50
67
|
const externalSearchApis = new WeakMap();
|
|
51
68
|
let defaultExternalSearchApi;
|
|
69
|
+
/**
|
|
70
|
+
* External asset origin rewriting. Only R2-backed binary entries are
|
|
71
|
+
* rewritten: `assets`-staged binaries live in the Workers static assets,
|
|
72
|
+
* not in the R2 bucket, so an R2 custom domain would not serve them.
|
|
73
|
+
*/
|
|
74
|
+
const resolveAssetUrl = createWorkerAssetUrlResolver(manifest);
|
|
52
75
|
return {
|
|
53
76
|
async fetch(request, env, ctx) {
|
|
54
77
|
const url = new URL(request.url);
|
|
@@ -129,6 +152,7 @@ export function createCloudflareWorker(manifest, options = {}) {
|
|
|
129
152
|
requestUrl: request.url,
|
|
130
153
|
searchApi: inlineSearchApi ?? externalSearchApi,
|
|
131
154
|
plugins: options.plugins,
|
|
155
|
+
resolveAssetUrl,
|
|
132
156
|
});
|
|
133
157
|
const headers = new Headers(siteResponse.headers);
|
|
134
158
|
const cacheHeaders = collectResponseCacheHeaders(siteResponse, request, url, etag);
|
|
@@ -465,7 +489,11 @@ async function tryServeExternalBinary(manifest, request, env, resolved) {
|
|
|
465
489
|
}
|
|
466
490
|
const headers = new Headers({
|
|
467
491
|
'content-type': manifestEntry.mediaType,
|
|
492
|
+
'cache-control': EXTERNAL_BINARY_BROWSER_CACHE_CONTROL,
|
|
493
|
+
'cdn-cache-control': EXTERNAL_BINARY_CDN_CACHE_CONTROL,
|
|
468
494
|
});
|
|
495
|
+
// Keep the R2 etag so browsers and the edge can revalidate cheaply once
|
|
496
|
+
// the freshness window expires.
|
|
469
497
|
if (object.httpEtag) {
|
|
470
498
|
headers.set('etag', object.httpEtag);
|
|
471
499
|
}
|
|
@@ -501,6 +529,23 @@ async function tryServeExternalBinary(manifest, request, env, resolved) {
|
|
|
501
529
|
function isExternalBinaryEntry(entry) {
|
|
502
530
|
return entry.kind === 'binary' && 'storageKind' in entry;
|
|
503
531
|
}
|
|
532
|
+
/**
|
|
533
|
+
* Builds the asset-origin rewriter for a manifest, or undefined when the site
|
|
534
|
+
* does not configure `assets.baseUrl` or has no R2-backed binary assets.
|
|
535
|
+
*/
|
|
536
|
+
function createWorkerAssetUrlResolver(manifest) {
|
|
537
|
+
const baseUrl = manifest.siteConfig?.assets?.baseUrl;
|
|
538
|
+
if (baseUrl === undefined) {
|
|
539
|
+
return undefined;
|
|
540
|
+
}
|
|
541
|
+
const externalAssetPaths = new Set(manifest.entries
|
|
542
|
+
.filter((entry) => isExternalBinaryEntry(entry) && entry.storageKind === 'r2')
|
|
543
|
+
.map((entry) => entry.path));
|
|
544
|
+
if (externalAssetPaths.size === 0) {
|
|
545
|
+
return undefined;
|
|
546
|
+
}
|
|
547
|
+
return createExternalAssetUrlResolver(baseUrl, externalAssetPaths);
|
|
548
|
+
}
|
|
504
549
|
class CloudflareManifestContentStore {
|
|
505
550
|
storeIndex;
|
|
506
551
|
request;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* External asset origin support.
|
|
3
|
+
*
|
|
4
|
+
* Sites deployed with external binary mode can mirror their R2 bucket on a
|
|
5
|
+
* custom origin (R2 custom domain or CDN). When `assets.baseUrl` is
|
|
6
|
+
* configured, rendered asset references are rewritten to that origin while
|
|
7
|
+
* keeping the same public path structure, so the Worker route keeps working
|
|
8
|
+
* as a fallback for crawlers and old links.
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* Resolves a rendered asset href to an external absolute URL.
|
|
12
|
+
*
|
|
13
|
+
* @param href Asset reference as written in content: relative (`./a.png`),
|
|
14
|
+
* root-relative (`/a.png`), or already absolute (returned unchanged by
|
|
15
|
+
* concrete implementations).
|
|
16
|
+
* @param pageRequestPath URL path of the page being rendered, used to resolve
|
|
17
|
+
* relative references the same way a browser would.
|
|
18
|
+
* @returns The absolute external URL, or undefined when the reference should
|
|
19
|
+
* not be rewritten.
|
|
20
|
+
*/
|
|
21
|
+
export type AssetUrlResolver = (href: string, pageRequestPath: string) => string | undefined;
|
|
22
|
+
/**
|
|
23
|
+
* Builds a resolver that rewrites hrefs resolving to one of the known
|
|
24
|
+
* external binary asset paths. Asset paths use content-path form (decoded,
|
|
25
|
+
* no leading slash) so they match manifest entries directly.
|
|
26
|
+
*/
|
|
27
|
+
export declare function createExternalAssetUrlResolver(baseUrl: string, assetPaths: ReadonlySet<string>): AssetUrlResolver;
|
|
28
|
+
/** Applies a resolver to an optional href, leaving non-matching values unchanged. */
|
|
29
|
+
export declare function applyAssetUrlResolver(href: string | undefined, pageRequestPath: string, resolver: AssetUrlResolver | undefined): string | undefined;
|
|
30
|
+
/**
|
|
31
|
+
* Normalizes an asset base URL: must be an absolute http(s) URL; trailing
|
|
32
|
+
* slashes are removed so `${baseUrl}${path}` concatenation stays correct.
|
|
33
|
+
*/
|
|
34
|
+
export declare function normalizeAssetBaseUrl(value: string): string;
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* External asset origin support.
|
|
3
|
+
*
|
|
4
|
+
* Sites deployed with external binary mode can mirror their R2 bucket on a
|
|
5
|
+
* custom origin (R2 custom domain or CDN). When `assets.baseUrl` is
|
|
6
|
+
* configured, rendered asset references are rewritten to that origin while
|
|
7
|
+
* keeping the same public path structure, so the Worker route keeps working
|
|
8
|
+
* as a fallback for crawlers and old links.
|
|
9
|
+
*/
|
|
10
|
+
const RESOLUTION_BASE_ORIGIN = 'https://asset.mdorigin.invalid';
|
|
11
|
+
/**
|
|
12
|
+
* Builds a resolver that rewrites hrefs resolving to one of the known
|
|
13
|
+
* external binary asset paths. Asset paths use content-path form (decoded,
|
|
14
|
+
* no leading slash) so they match manifest entries directly.
|
|
15
|
+
*/
|
|
16
|
+
export function createExternalAssetUrlResolver(baseUrl, assetPaths) {
|
|
17
|
+
const normalizedBaseUrl = normalizeAssetBaseUrl(baseUrl);
|
|
18
|
+
return (href, pageRequestPath) => {
|
|
19
|
+
if (href === '' || assetPaths.size === 0) {
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
// Already absolute or protocol-relative references stay untouched.
|
|
23
|
+
if (href.startsWith('//') || /^[a-zA-Z][a-zA-Z\d+.-]*:/.test(href)) {
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
const directory = pageRequestPath.includes('/')
|
|
27
|
+
? pageRequestPath.slice(0, pageRequestPath.lastIndexOf('/') + 1)
|
|
28
|
+
: '/';
|
|
29
|
+
let resolved;
|
|
30
|
+
try {
|
|
31
|
+
resolved = new URL(href, `${RESOLUTION_BASE_ORIGIN}${directory}`);
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
if (resolved.origin !== RESOLUTION_BASE_ORIGIN) {
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
const assetPath = toContentPathForm(resolved.pathname);
|
|
40
|
+
if (assetPath === null || !assetPaths.has(assetPath)) {
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
// Emit the URL-encoded path: percent-encoded hrefs stay encoded and
|
|
44
|
+
// raw unicode hrefs become correctly encoded absolute URLs. Query and
|
|
45
|
+
// fragment (cache-busting, anchors) are preserved as written.
|
|
46
|
+
return `${normalizedBaseUrl}${resolved.pathname}${resolved.search}${resolved.hash}`;
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
/** Applies a resolver to an optional href, leaving non-matching values unchanged. */
|
|
50
|
+
export function applyAssetUrlResolver(href, pageRequestPath, resolver) {
|
|
51
|
+
if (href === undefined || resolver === undefined) {
|
|
52
|
+
return href;
|
|
53
|
+
}
|
|
54
|
+
return resolver(href, pageRequestPath) ?? href;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Normalizes an asset base URL: must be an absolute http(s) URL; trailing
|
|
58
|
+
* slashes are removed so `${baseUrl}${path}` concatenation stays correct.
|
|
59
|
+
*/
|
|
60
|
+
export function normalizeAssetBaseUrl(value) {
|
|
61
|
+
let parsed;
|
|
62
|
+
try {
|
|
63
|
+
parsed = new URL(value);
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
throw new Error(`Invalid asset base URL: ${value} is not an absolute URL`);
|
|
67
|
+
}
|
|
68
|
+
if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') {
|
|
69
|
+
throw new Error(`Invalid asset base URL: ${value} must use http or https`);
|
|
70
|
+
}
|
|
71
|
+
return parsed.toString().replace(/\/+$/, '');
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Converts a percent-encoded URL pathname to content-path form (decoded, no
|
|
75
|
+
* leading slash), matching how the router resolves request paths to manifest
|
|
76
|
+
* entries. Returns null when the pathname cannot be decoded.
|
|
77
|
+
*/
|
|
78
|
+
function toContentPathForm(pathname) {
|
|
79
|
+
try {
|
|
80
|
+
const decoded = decodeURIComponent(pathname);
|
|
81
|
+
const withoutLeadingSlash = decoded.replace(/^\/+/, '');
|
|
82
|
+
if (withoutLeadingSlash === '') {
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
return withoutLeadingSlash;
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
}
|
package/dist/core/markdown.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import matter from 'gray-matter';
|
|
2
2
|
import type { ContentEntry } from './content-store.js';
|
|
3
|
+
import type { AssetUrlResolver } from './asset-urls.js';
|
|
3
4
|
import { type PageRobots } from './seo.js';
|
|
4
5
|
export interface ParsedDocumentMeta {
|
|
5
6
|
title?: string;
|
|
@@ -52,7 +53,19 @@ export interface MarkdownHeading {
|
|
|
52
53
|
}
|
|
53
54
|
export declare function getDocumentTitle(meta: ParsedDocumentMeta, body: string, fallback: string): string;
|
|
54
55
|
export declare function getDocumentSummary(meta: ParsedDocumentMeta, body: string): string | undefined;
|
|
55
|
-
export
|
|
56
|
+
export interface ParseMarkdownDocumentOptions {
|
|
57
|
+
/**
|
|
58
|
+
* URL path of the page being rendered. Relative media sources resolve
|
|
59
|
+
* against this path the same way a browser would resolve them.
|
|
60
|
+
*/
|
|
61
|
+
requestPath?: string;
|
|
62
|
+
/**
|
|
63
|
+
* Optional rewriter mapping binary asset references to an external asset
|
|
64
|
+
* origin (see `assets.baseUrl`). Only matching references change.
|
|
65
|
+
*/
|
|
66
|
+
resolveAssetUrl?: AssetUrlResolver;
|
|
67
|
+
}
|
|
68
|
+
export declare function parseMarkdownDocument(sourcePath: string, markdown: string, options?: ParseMarkdownDocumentOptions): Promise<ParsedDocument>;
|
|
56
69
|
export declare function parseDocumentMeta(sourcePath: string, markdown: string): ParsedDocumentLite;
|
|
57
70
|
/** Read original scalar values without applying page metadata normalization. */
|
|
58
71
|
export declare function parseMarkdownFrontmatter(markdown: string): matter.GrayMatterFile<string>;
|
package/dist/core/markdown.js
CHANGED
|
@@ -15,9 +15,9 @@ export function getDocumentSummary(meta, body) {
|
|
|
15
15
|
return (firstNonEmptyString(meta.summary, meta.description) ??
|
|
16
16
|
extractFirstParagraph(body));
|
|
17
17
|
}
|
|
18
|
-
export async function parseMarkdownDocument(sourcePath, markdown) {
|
|
18
|
+
export async function parseMarkdownDocument(sourcePath, markdown, options = {}) {
|
|
19
19
|
const { body, meta } = parseDocumentMeta(sourcePath, markdown);
|
|
20
|
-
const html = rewriteMarkdownLinksInHtml(await renderMarkdown(body));
|
|
20
|
+
const html = enhanceHtmlMediaSources(rewriteMarkdownLinksInHtml(await renderMarkdown(body)), options);
|
|
21
21
|
return {
|
|
22
22
|
sourcePath,
|
|
23
23
|
body,
|
|
@@ -121,6 +121,47 @@ function assignHeadingIds(root, headings) {
|
|
|
121
121
|
export function rewriteMarkdownLinksInHtml(html) {
|
|
122
122
|
return html.replaceAll(/(<a\b[^>]*?\shref=")([^"]+)(")/g, (_match, prefix, href, suffix) => `${prefix}${rewriteMarkdownHref(href)}${suffix}`);
|
|
123
123
|
}
|
|
124
|
+
const MEDIA_SOURCE_TAG_PATTERN = /(<(?:img|source|video|audio)\b[^>]*?\ssrc=")([^"]+)(")/g;
|
|
125
|
+
const IMG_TAG_PATTERN = /<img\b[^>]*>/g;
|
|
126
|
+
/**
|
|
127
|
+
* Applies asset-origin and lazy-loading enhancements to media elements in
|
|
128
|
+
* rendered HTML:
|
|
129
|
+
*
|
|
130
|
+
* - When an asset resolver is configured, `src` attributes on media elements
|
|
131
|
+
* that resolve to a known external binary asset are rewritten to the
|
|
132
|
+
* external origin.
|
|
133
|
+
* - Rendered `<img>` tags get `loading="lazy"` and `decoding="async"` so
|
|
134
|
+
* off-screen images stay out of the critical path. Attributes the author
|
|
135
|
+
* already set are preserved.
|
|
136
|
+
*/
|
|
137
|
+
function enhanceHtmlMediaSources(html, options) {
|
|
138
|
+
const { requestPath, resolveAssetUrl } = options;
|
|
139
|
+
let result = html;
|
|
140
|
+
if (resolveAssetUrl !== undefined && requestPath !== undefined) {
|
|
141
|
+
result = result.replaceAll(MEDIA_SOURCE_TAG_PATTERN, (match, prefix, src, suffix) => {
|
|
142
|
+
const rewritten = resolveAssetUrl(src, requestPath);
|
|
143
|
+
return rewritten === undefined ? match : `${prefix}${rewritten}${suffix}`;
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
return result.replaceAll(IMG_TAG_PATTERN, addLazyLoadingAttributes);
|
|
147
|
+
}
|
|
148
|
+
function addLazyLoadingAttributes(tag) {
|
|
149
|
+
let enhanced = tag;
|
|
150
|
+
if (!/\bloading\s*=/.test(enhanced)) {
|
|
151
|
+
enhanced = insertAttributeBeforeTagEnd(enhanced, ' loading="lazy"');
|
|
152
|
+
}
|
|
153
|
+
if (!/\bdecoding\s*=/.test(enhanced)) {
|
|
154
|
+
enhanced = insertAttributeBeforeTagEnd(enhanced, ' decoding="async"');
|
|
155
|
+
}
|
|
156
|
+
return enhanced;
|
|
157
|
+
}
|
|
158
|
+
function insertAttributeBeforeTagEnd(tag, attribute) {
|
|
159
|
+
const endMatch = tag.match(/\s*\/?>$/);
|
|
160
|
+
if (endMatch === null) {
|
|
161
|
+
return tag;
|
|
162
|
+
}
|
|
163
|
+
return tag.slice(0, tag.length - endMatch[0].length) + attribute + endMatch[0];
|
|
164
|
+
}
|
|
124
165
|
export function stripManagedIndexBlock(markdown) {
|
|
125
166
|
return markdown.replace(/\n?<!-- INDEX:START -->[\s\S]*?<!-- INDEX:END -->\n?/g, '\n').trimEnd();
|
|
126
167
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { ContentStore } from './content-store.js';
|
|
2
|
+
import { type AssetUrlResolver } from './asset-urls.js';
|
|
2
3
|
import type { MdoPlugin } from './extensions.js';
|
|
3
4
|
import type { ResolvedSiteConfig } from './site-config.js';
|
|
4
5
|
import type { SearchApi } from '../search.js';
|
|
@@ -13,6 +14,13 @@ export interface HandleSiteRequestOptions {
|
|
|
13
14
|
requestUrl?: string;
|
|
14
15
|
searchApi?: SearchApi;
|
|
15
16
|
plugins?: MdoPlugin[];
|
|
17
|
+
/**
|
|
18
|
+
* Optional external asset origin rewriter for rendered binary asset
|
|
19
|
+
* references (markdown media sources, cover / social images). Supplied by
|
|
20
|
+
* deployments that know which paths live on the external origin, e.g. the
|
|
21
|
+
* Cloudflare worker from its manifest.
|
|
22
|
+
*/
|
|
23
|
+
resolveAssetUrl?: AssetUrlResolver;
|
|
16
24
|
}
|
|
17
25
|
export interface SiteResponse {
|
|
18
26
|
status: number;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import path from 'node:path';
|
|
2
|
+
import { applyAssetUrlResolver } from './asset-urls.js';
|
|
2
3
|
import { isIgnoredContentName } from './content-store.js';
|
|
3
4
|
import { inferDirectoryContentType, resolveContentType, } from './content-type.js';
|
|
4
5
|
import { getDirectoryIndexCandidates, isCanonicalContentSource } from './directory-index.js';
|
|
@@ -174,7 +175,10 @@ async function handleSiteRequestWithoutLocaleDetection(store, pathname, options)
|
|
|
174
175
|
return renderListingPostsFragment(listingEntries, listingFragmentRequest);
|
|
175
176
|
}
|
|
176
177
|
const documentBody = listingEntries.length > 0 ? stripManagedIndexBlock(renderedBody) : renderedBody;
|
|
177
|
-
const renderedParsed = await parseMarkdownDocument(resolved.sourcePath, documentBody
|
|
178
|
+
const renderedParsed = await parseMarkdownDocument(resolved.sourcePath, documentBody, {
|
|
179
|
+
requestPath: resolved.requestPath,
|
|
180
|
+
resolveAssetUrl: options.resolveAssetUrl,
|
|
181
|
+
});
|
|
178
182
|
return renderStructuredPage({
|
|
179
183
|
requestPath: resolved.requestPath,
|
|
180
184
|
sourcePath: resolved.sourcePath,
|
|
@@ -184,6 +188,7 @@ async function handleSiteRequestWithoutLocaleDetection(store, pathname, options)
|
|
|
184
188
|
topNav: navigation.items,
|
|
185
189
|
listingEntries,
|
|
186
190
|
searchEnabled,
|
|
191
|
+
resolveAssetUrl: options.resolveAssetUrl,
|
|
187
192
|
plugins,
|
|
188
193
|
store,
|
|
189
194
|
requestLocale,
|
|
@@ -297,8 +302,8 @@ function buildPageRenderModel(options) {
|
|
|
297
302
|
siteDescription: options.siteDescription ?? options.siteConfig.siteDescription,
|
|
298
303
|
siteUrl: options.siteConfig.siteUrl,
|
|
299
304
|
favicon: options.siteConfig.favicon,
|
|
300
|
-
socialImage: resolvePageSocialImage(options.parsed.meta.socialImage, options.resolvedRequestPath) ??
|
|
301
|
-
options.siteConfig.socialImage,
|
|
305
|
+
socialImage: applyAssetUrlResolver(resolvePageSocialImage(options.parsed.meta.socialImage, options.resolvedRequestPath) ??
|
|
306
|
+
options.siteConfig.socialImage, options.resolvedRequestPath, options.resolveAssetUrl),
|
|
302
307
|
logo: options.siteConfig.logo,
|
|
303
308
|
title: getDocumentTitle(options.parsed),
|
|
304
309
|
meta: options.parsed.meta,
|
|
@@ -343,6 +348,7 @@ async function renderStructuredPage(options) {
|
|
|
343
348
|
siteDescription: getEffectiveSiteDescription(options.siteConfig, localeConfig),
|
|
344
349
|
footerNav: getEffectiveFooterNav(options.siteConfig, localeConfig),
|
|
345
350
|
topNav: options.topNav,
|
|
351
|
+
resolveAssetUrl: options.resolveAssetUrl,
|
|
346
352
|
listingEntries: options.listingEntries,
|
|
347
353
|
searchEnabled: options.searchEnabled,
|
|
348
354
|
});
|
|
@@ -1155,7 +1161,10 @@ async function tryRenderAlternateDirectoryIndex(store, requestPath, options) {
|
|
|
1155
1161
|
return renderListingPostsFragment(listingEntries, listingFragmentRequest);
|
|
1156
1162
|
}
|
|
1157
1163
|
const documentBody = listingEntries.length > 0 ? stripManagedIndexBlock(renderedBody) : renderedBody;
|
|
1158
|
-
const renderedParsed = await parseMarkdownDocument(candidatePath, documentBody
|
|
1164
|
+
const renderedParsed = await parseMarkdownDocument(candidatePath, documentBody, {
|
|
1165
|
+
requestPath,
|
|
1166
|
+
resolveAssetUrl: options.resolveAssetUrl,
|
|
1167
|
+
});
|
|
1159
1168
|
return renderStructuredPage({
|
|
1160
1169
|
requestPath,
|
|
1161
1170
|
sourcePath: candidatePath,
|
|
@@ -1165,6 +1174,7 @@ async function tryRenderAlternateDirectoryIndex(store, requestPath, options) {
|
|
|
1165
1174
|
topNav: navigation.items,
|
|
1166
1175
|
listingEntries,
|
|
1167
1176
|
searchEnabled: options.searchApi !== undefined,
|
|
1177
|
+
resolveAssetUrl: options.resolveAssetUrl,
|
|
1168
1178
|
plugins,
|
|
1169
1179
|
store,
|
|
1170
1180
|
requestLocale,
|
|
@@ -63,6 +63,19 @@ export interface SiteSearchConfig {
|
|
|
63
63
|
scoreAdjustment?: SiteSearchScoreAdjustmentConfig;
|
|
64
64
|
policy?: SiteSearchPolicyConfig;
|
|
65
65
|
}
|
|
66
|
+
export interface SiteAssetsConfig {
|
|
67
|
+
/**
|
|
68
|
+
* Absolute http(s) base URL of an external asset origin, e.g. an R2 custom
|
|
69
|
+
* domain or a CDN in front of the same bucket. Requires external binary
|
|
70
|
+
* mode: rendered references to R2-backed binary assets are rewritten to
|
|
71
|
+
* `${baseUrl}<same path>`, while the Worker route keeps serving the same
|
|
72
|
+
* paths as a fallback for crawlers and old links.
|
|
73
|
+
*/
|
|
74
|
+
baseUrl?: string;
|
|
75
|
+
}
|
|
76
|
+
export interface ResolvedSiteAssetsConfig {
|
|
77
|
+
baseUrl: string;
|
|
78
|
+
}
|
|
66
79
|
export interface SiteConfig {
|
|
67
80
|
siteTitle?: string;
|
|
68
81
|
siteDescription?: string;
|
|
@@ -103,6 +116,7 @@ export interface SiteConfig {
|
|
|
103
116
|
listingInitialPostCount?: number;
|
|
104
117
|
listingLoadMoreStep?: number;
|
|
105
118
|
search?: SiteSearchConfig;
|
|
119
|
+
assets?: SiteAssetsConfig;
|
|
106
120
|
}
|
|
107
121
|
export interface UserSiteConfig extends SiteConfig {
|
|
108
122
|
plugins?: MdoPlugin[];
|
|
@@ -133,6 +147,7 @@ export interface ResolvedSiteConfig {
|
|
|
133
147
|
listingInitialPostCount: number;
|
|
134
148
|
listingLoadMoreStep: number;
|
|
135
149
|
search?: SiteSearchConfig;
|
|
150
|
+
assets?: ResolvedSiteAssetsConfig;
|
|
136
151
|
stylesheetContent?: string;
|
|
137
152
|
siteTitleConfigured: boolean;
|
|
138
153
|
siteDescriptionConfigured: boolean;
|
package/dist/core/site-config.js
CHANGED
|
@@ -5,6 +5,7 @@ import { tsImport } from 'tsx/esm/api';
|
|
|
5
5
|
import { getDirectoryIndexCandidates } from './directory-index.js';
|
|
6
6
|
import { parseEntryDocumentMeta } from './markdown.js';
|
|
7
7
|
import { normalizeSiteRobots } from './seo.js';
|
|
8
|
+
import { normalizeAssetBaseUrl } from './asset-urls.js';
|
|
8
9
|
import { DEFAULT_SITE_LOCALE, validateSiteMessages, } from '../i18n/messages.js';
|
|
9
10
|
export async function loadSiteConfig(options = {}) {
|
|
10
11
|
return (await loadUserSiteConfig(options)).siteConfig;
|
|
@@ -62,6 +63,7 @@ export async function loadUserSiteConfig(options = {}) {
|
|
|
62
63
|
listingInitialPostCount: normalizePositiveInteger(parsedConfig.listingInitialPostCount ?? legacyConfig.catalogInitialPostCount, 10),
|
|
63
64
|
listingLoadMoreStep: normalizePositiveInteger(parsedConfig.listingLoadMoreStep ?? legacyConfig.catalogLoadMoreStep, 10),
|
|
64
65
|
search: normalizeSearchConfig(parsedConfig.search, configFilePath),
|
|
66
|
+
assets: resolveAssetsConfig(parsedConfig.assets, configFilePath),
|
|
65
67
|
stylesheetContent,
|
|
66
68
|
siteTitleConfigured: typeof parsedConfig.siteTitle === 'string' && parsedConfig.siteTitle !== '',
|
|
67
69
|
siteDescriptionConfigured: typeof parsedConfig.siteDescription === 'string' &&
|
|
@@ -122,6 +124,26 @@ function resolveLocaleDetectionConfig(value, configFilePath) {
|
|
|
122
124
|
redirect: 'root',
|
|
123
125
|
};
|
|
124
126
|
}
|
|
127
|
+
function resolveAssetsConfig(value, configFilePath) {
|
|
128
|
+
if (value === undefined) {
|
|
129
|
+
return undefined;
|
|
130
|
+
}
|
|
131
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
132
|
+
throw new Error(`Invalid "assets" in ${configFilePath}: expected an object`);
|
|
133
|
+
}
|
|
134
|
+
if (value.baseUrl === undefined) {
|
|
135
|
+
return undefined;
|
|
136
|
+
}
|
|
137
|
+
if (typeof value.baseUrl !== 'string' || value.baseUrl === '') {
|
|
138
|
+
throw new Error(`Invalid "assets.baseUrl" in ${configFilePath}: expected a non-empty string`);
|
|
139
|
+
}
|
|
140
|
+
try {
|
|
141
|
+
return { baseUrl: normalizeAssetBaseUrl(value.baseUrl) };
|
|
142
|
+
}
|
|
143
|
+
catch (error) {
|
|
144
|
+
throw new Error(`Invalid "assets.baseUrl" in ${configFilePath}: ${error instanceof Error ? error.message : String(error)}`);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
125
147
|
function resolveMessageOverrides(value, configFilePath) {
|
|
126
148
|
if (value === undefined) {
|
|
127
149
|
return {};
|
package/dist/index-builder.js
CHANGED
|
@@ -141,7 +141,8 @@ export async function buildManagedIndexBlock(directoryPath, plugins = [], exclud
|
|
|
141
141
|
kind: 'article',
|
|
142
142
|
title: entry.title,
|
|
143
143
|
href: entry.link,
|
|
144
|
-
detail: [entry.date, entry.summary].filter(Boolean).join(' · ') ||
|
|
144
|
+
detail: [formatIndexEntryDate(entry.date), entry.summary].filter(Boolean).join(' · ') ||
|
|
145
|
+
undefined,
|
|
145
146
|
})),
|
|
146
147
|
], plugins, {
|
|
147
148
|
mode: 'build',
|
|
@@ -166,6 +167,19 @@ export function upsertManagedIndexBlock(source, block, options = {}) {
|
|
|
166
167
|
}
|
|
167
168
|
return `${trimmed}\n\n${block}\n`;
|
|
168
169
|
}
|
|
170
|
+
/**
|
|
171
|
+
* Managed index lines are human-facing. Date-only values keep their authored
|
|
172
|
+
* spelling, while normalized UTC timestamps render as `YYYY-MM-DD HH:mm`
|
|
173
|
+
* instead of raw ISO. Sorting still compares the normalized meta.date values.
|
|
174
|
+
*/
|
|
175
|
+
function formatIndexEntryDate(value) {
|
|
176
|
+
if (!value)
|
|
177
|
+
return undefined;
|
|
178
|
+
if (/^\d{4}-\d{2}-\d{2}$/.test(value))
|
|
179
|
+
return value;
|
|
180
|
+
const match = /^(\d{4}-\d{2}-\d{2})T(\d{2}:\d{2})/.exec(value);
|
|
181
|
+
return match ? `${match[1]} ${match[2]}` : value;
|
|
182
|
+
}
|
|
169
183
|
function renderManagedIndexBlock(entries) {
|
|
170
184
|
const lines = [INDEX_START_MARKER, ''];
|
|
171
185
|
if (entries.length > 0) {
|