mdorigin 0.5.1 → 0.5.3
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.d.ts +31 -2
- package/dist/adapters/cloudflare.js +277 -17
- package/dist/cloudflare.js +10 -1
- package/dist/core/markdown.d.ts +23 -6
- package/dist/core/markdown.js +27 -2
- package/dist/core/request-handler.js +61 -37
- package/dist/core/site-config.d.ts +16 -0
- package/dist/core/site-config.js +13 -2
- package/dist/html/template.js +44 -4
- package/dist/html/theme.js +64 -23
- package/package.json +1 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { MdoPlugin } from '../core/extensions.js';
|
|
2
2
|
import type { ResolvedSiteConfig } from '../core/site-config.js';
|
|
3
|
-
import { type ExternalSearchBundleEntry, type SearchBundleEntry } from '../search.js';
|
|
3
|
+
import { type ExternalSearchBundleEntry, type SearchApi, type SearchBundleEntry } from '../search.js';
|
|
4
4
|
export interface TextCloudflareManifestEntry {
|
|
5
5
|
path: string;
|
|
6
6
|
kind: 'text';
|
|
@@ -32,6 +32,13 @@ export interface CloudflareManifest {
|
|
|
32
32
|
searchEntries?: SearchBundleEntry[];
|
|
33
33
|
externalSearchEntries?: ExternalSearchBundleEntry[];
|
|
34
34
|
runtime?: CloudflareBundleRuntimeConfig;
|
|
35
|
+
/**
|
|
36
|
+
* Build-time deploy version. Bundle content is immutable within one deploy,
|
|
37
|
+
* so this value doubles as a strong ETag and lets If-None-Match matches skip
|
|
38
|
+
* rendering entirely. A new deploy produces a new version and invalidates
|
|
39
|
+
* every cached response.
|
|
40
|
+
*/
|
|
41
|
+
deployVersion?: string;
|
|
35
42
|
}
|
|
36
43
|
export interface CloudflareAssetsBindingLike {
|
|
37
44
|
fetch(request: Request): Promise<Response>;
|
|
@@ -52,9 +59,31 @@ export interface CloudflareWorkerEnv {
|
|
|
52
59
|
[binding: string]: unknown;
|
|
53
60
|
}
|
|
54
61
|
export interface ExportedHandlerLike {
|
|
55
|
-
fetch(request: Request, env?: CloudflareWorkerEnv, ctx?:
|
|
62
|
+
fetch(request: Request, env?: CloudflareWorkerEnv, ctx?: CloudflareWorkerExecutionContextLike): Promise<Response>;
|
|
63
|
+
}
|
|
64
|
+
export interface CloudflareWorkerExecutionContextLike {
|
|
65
|
+
waitUntil(promise: Promise<unknown>): void;
|
|
66
|
+
}
|
|
67
|
+
export interface CloudflareCacheLike {
|
|
68
|
+
match(request: Request): Promise<Response | undefined>;
|
|
69
|
+
put(request: Request, response: Response): Promise<void>;
|
|
56
70
|
}
|
|
57
71
|
export interface CreateCloudflareWorkerOptions {
|
|
58
72
|
plugins?: MdoPlugin[];
|
|
73
|
+
/**
|
|
74
|
+
* Per-colo L2 cache. Defaults to the runtime `caches.default` when the
|
|
75
|
+
* runtime exposes one. Pass `null` to disable L2 caching.
|
|
76
|
+
*/
|
|
77
|
+
cache?: CloudflareCacheLike | null;
|
|
78
|
+
/** Maximum isolate-memoized rendered responses. Defaults to 100. */
|
|
79
|
+
responseCacheLimit?: number;
|
|
80
|
+
/** Maximum isolate-memoized search results. Defaults to 100. */
|
|
81
|
+
searchCacheLimit?: number;
|
|
59
82
|
}
|
|
60
83
|
export declare function createCloudflareWorker(manifest: CloudflareManifest, options?: CreateCloudflareWorkerOptions): ExportedHandlerLike;
|
|
84
|
+
/**
|
|
85
|
+
* Memoizes deterministic search results per isolate. The search index is
|
|
86
|
+
* static within one deploy, so identical (query, options) calls return the
|
|
87
|
+
* same hits and repeated queries skip retrieval entirely.
|
|
88
|
+
*/
|
|
89
|
+
export declare function withSearchResultCache(api: SearchApi, limit: number): SearchApi;
|
|
@@ -2,6 +2,21 @@ import { MemoryContentStore } from '../core/content-store.js';
|
|
|
2
2
|
import { handleSiteRequest } from '../core/request-handler.js';
|
|
3
3
|
import { resolveRequest } from '../core/router.js';
|
|
4
4
|
import { createSearchApiFromBundle, createSearchApiFromExternalBundle, } from '../search.js';
|
|
5
|
+
const BROWSER_CACHE_CONTROL = 'public, max-age=120, stale-while-revalidate=604800';
|
|
6
|
+
const CDN_CACHE_CONTROL = 'max-age=86400';
|
|
7
|
+
const L2_CACHE_CONTROL = 'public, max-age=86400';
|
|
8
|
+
const L2_CACHE_KEY_ORIGIN = 'https://cache.mdorigin.internal';
|
|
9
|
+
const CACHE_DEBUG_HEADER = 'x-mdorigin-cache';
|
|
10
|
+
const RESPONSE_CACHE_LIMIT_DEFAULT = 100;
|
|
11
|
+
const SEARCH_CACHE_LIMIT_DEFAULT = 100;
|
|
12
|
+
const CACHEABLE_CONTENT_TYPE_PREFIXES = [
|
|
13
|
+
'text/html',
|
|
14
|
+
'text/markdown',
|
|
15
|
+
'application/xml',
|
|
16
|
+
'text/xml',
|
|
17
|
+
'application/rss+xml',
|
|
18
|
+
'application/atom+xml',
|
|
19
|
+
];
|
|
5
20
|
export function createCloudflareWorker(manifest, options = {}) {
|
|
6
21
|
const storeIndex = new MemoryContentStore(manifest.entries.map((entry) => {
|
|
7
22
|
if (entry.kind === 'text') {
|
|
@@ -26,22 +41,60 @@ export function createCloudflareWorker(manifest, options = {}) {
|
|
|
26
41
|
bytes: decodeBase64(entry.base64 ?? ''),
|
|
27
42
|
};
|
|
28
43
|
}));
|
|
44
|
+
const responseCache = new LruCache(options.responseCacheLimit ?? RESPONSE_CACHE_LIMIT_DEFAULT);
|
|
45
|
+
const searchCacheLimit = options.searchCacheLimit ?? SEARCH_CACHE_LIMIT_DEFAULT;
|
|
46
|
+
const cache = options.cache === undefined ? resolveDefaultCache() : options.cache ?? undefined;
|
|
29
47
|
const inlineSearchApi = manifest.searchEntries && manifest.searchEntries.length > 0
|
|
30
|
-
? createSearchApiFromBundle(manifest.searchEntries, manifest.siteConfig?.search)
|
|
48
|
+
? withSearchResultCache(createSearchApiFromBundle(manifest.searchEntries, manifest.siteConfig?.search), searchCacheLimit)
|
|
31
49
|
: undefined;
|
|
32
50
|
const externalSearchApis = new WeakMap();
|
|
33
51
|
let defaultExternalSearchApi;
|
|
34
52
|
return {
|
|
35
|
-
async fetch(request, env) {
|
|
53
|
+
async fetch(request, env, ctx) {
|
|
36
54
|
const url = new URL(request.url);
|
|
37
|
-
const externalSearchApi = getExternalSearchApi(manifest, env, externalSearchApis, defaultExternalSearchApi);
|
|
55
|
+
const externalSearchApi = getExternalSearchApi(manifest, env, externalSearchApis, defaultExternalSearchApi, searchCacheLimit);
|
|
38
56
|
if (env === undefined && externalSearchApi !== undefined) {
|
|
39
57
|
defaultExternalSearchApi = externalSearchApi;
|
|
40
58
|
}
|
|
41
|
-
const
|
|
59
|
+
const resolvedRequest = resolveRequest(url.pathname);
|
|
60
|
+
const directBinaryResponse = await tryServeExternalBinary(manifest, request, env, resolvedRequest);
|
|
42
61
|
if (directBinaryResponse !== null) {
|
|
43
62
|
return directBinaryResponse;
|
|
44
63
|
}
|
|
64
|
+
const etag = manifest.deployVersion === undefined
|
|
65
|
+
? undefined
|
|
66
|
+
: `"${manifest.deployVersion}"`;
|
|
67
|
+
if (etag !== undefined &&
|
|
68
|
+
isCacheableRequest(request, url) &&
|
|
69
|
+
requestMatchesEtag(request.headers.get('if-none-match'), etag)) {
|
|
70
|
+
return new Response(null, {
|
|
71
|
+
status: 304,
|
|
72
|
+
headers: {
|
|
73
|
+
etag,
|
|
74
|
+
'cache-control': BROWSER_CACHE_CONTROL,
|
|
75
|
+
},
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
const cacheDebugEnabled = isCacheDebugEnabled(env);
|
|
79
|
+
const responseCacheKey = manifest.deployVersion !== undefined && isCacheableRequest(request, url)
|
|
80
|
+
? buildResponseCacheKey(manifest.deployVersion, resolvedRequest, request.headers.get('accept'), url.pathname)
|
|
81
|
+
: undefined;
|
|
82
|
+
if (responseCacheKey !== undefined) {
|
|
83
|
+
const memoized = responseCache.get(responseCacheKey);
|
|
84
|
+
if (memoized !== undefined) {
|
|
85
|
+
return toWorkerResponse(memoized, cacheDebugEnabled ? 'l1' : undefined);
|
|
86
|
+
}
|
|
87
|
+
if (cache !== undefined) {
|
|
88
|
+
const cached = await cache.match(toL2CacheRequest(responseCacheKey));
|
|
89
|
+
if (cached !== undefined) {
|
|
90
|
+
const entry = await cachedResponseFromL2(cached);
|
|
91
|
+
if (entry !== null) {
|
|
92
|
+
responseCache.set(responseCacheKey, entry);
|
|
93
|
+
return toWorkerResponse(entry, cacheDebugEnabled ? 'l2' : undefined);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
45
98
|
const store = new CloudflareManifestContentStore(manifest, storeIndex, request, env);
|
|
46
99
|
const siteResponse = await handleSiteRequest(store, url.pathname, {
|
|
47
100
|
draftMode: 'exclude',
|
|
@@ -72,31 +125,239 @@ export function createCloudflareWorker(manifest, options = {}) {
|
|
|
72
125
|
plugins: options.plugins,
|
|
73
126
|
});
|
|
74
127
|
const headers = new Headers(siteResponse.headers);
|
|
75
|
-
const
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
128
|
+
const cacheHeaders = collectResponseCacheHeaders(siteResponse, request, url, etag);
|
|
129
|
+
if (cacheHeaders !== undefined) {
|
|
130
|
+
for (const [name, value] of Object.entries(cacheHeaders)) {
|
|
131
|
+
headers.set(name, value);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
const rendered = {
|
|
81
135
|
status: siteResponse.status,
|
|
82
|
-
headers,
|
|
83
|
-
|
|
136
|
+
headers: Object.fromEntries(headers.entries()),
|
|
137
|
+
body: siteResponse.body ?? '',
|
|
138
|
+
};
|
|
139
|
+
if (responseCacheKey !== undefined &&
|
|
140
|
+
siteResponse.status === 200 &&
|
|
141
|
+
isCacheableContentType(siteResponse.headers['content-type'])) {
|
|
142
|
+
responseCache.set(responseCacheKey, rendered);
|
|
143
|
+
if (cache !== undefined) {
|
|
144
|
+
const putPromise = cache.put(toL2CacheRequest(responseCacheKey), toL2CacheResponse(rendered));
|
|
145
|
+
if (ctx?.waitUntil !== undefined) {
|
|
146
|
+
ctx.waitUntil(putPromise);
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
await putPromise;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return toWorkerResponse(rendered, cacheDebugEnabled && responseCacheKey !== undefined ? 'miss' : undefined);
|
|
84
154
|
},
|
|
85
155
|
};
|
|
86
156
|
}
|
|
87
|
-
function
|
|
157
|
+
function isCacheableRequest(request, url) {
|
|
158
|
+
if (request.method !== 'GET' && request.method !== 'HEAD') {
|
|
159
|
+
return false;
|
|
160
|
+
}
|
|
161
|
+
if (url.search !== '') {
|
|
162
|
+
return false;
|
|
163
|
+
}
|
|
164
|
+
return url.pathname !== '/api' && !url.pathname.startsWith('/api/');
|
|
165
|
+
}
|
|
166
|
+
function isCacheableContentType(contentType) {
|
|
167
|
+
if (contentType === undefined) {
|
|
168
|
+
return false;
|
|
169
|
+
}
|
|
170
|
+
const mediaType = contentType.split(';', 1)[0]?.trim().toLowerCase();
|
|
171
|
+
if (mediaType === undefined || mediaType === '') {
|
|
172
|
+
return false;
|
|
173
|
+
}
|
|
174
|
+
return CACHEABLE_CONTENT_TYPE_PREFIXES.some((prefix) => mediaType === prefix || mediaType.startsWith(`${prefix}/`));
|
|
175
|
+
}
|
|
176
|
+
function requestMatchesEtag(ifNoneMatch, etag) {
|
|
177
|
+
if (ifNoneMatch === null || ifNoneMatch.trim() === '') {
|
|
178
|
+
return false;
|
|
179
|
+
}
|
|
180
|
+
if (ifNoneMatch.trim() === '*') {
|
|
181
|
+
return true;
|
|
182
|
+
}
|
|
183
|
+
return ifNoneMatch.split(',').some((candidate) => {
|
|
184
|
+
const trimmed = candidate.trim();
|
|
185
|
+
const value = trimmed.startsWith('W/') ? trimmed.slice(2) : trimmed;
|
|
186
|
+
return value === etag;
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
function collectResponseCacheHeaders(siteResponse, request, url, etag) {
|
|
190
|
+
if (etag === undefined ||
|
|
191
|
+
siteResponse.status !== 200 ||
|
|
192
|
+
!isCacheableRequest(request, url) ||
|
|
193
|
+
!isCacheableContentType(siteResponse.headers['content-type'])) {
|
|
194
|
+
return undefined;
|
|
195
|
+
}
|
|
196
|
+
const cacheHeaders = {
|
|
197
|
+
etag,
|
|
198
|
+
'cache-control': BROWSER_CACHE_CONTROL,
|
|
199
|
+
};
|
|
200
|
+
const varyValues = siteResponse.headers['vary']
|
|
201
|
+
?.split(',')
|
|
202
|
+
.map((value) => value.trim().toLowerCase()) ?? [];
|
|
203
|
+
// The Cloudflare edge cache does not vary on Accept, so extensionless
|
|
204
|
+
// content-negotiated routes must stay out of the CDN cache. They still get
|
|
205
|
+
// ETag/304 handling and browser revalidation.
|
|
206
|
+
if (!varyValues.includes('accept')) {
|
|
207
|
+
cacheHeaders['cdn-cache-control'] = CDN_CACHE_CONTROL;
|
|
208
|
+
}
|
|
209
|
+
return cacheHeaders;
|
|
210
|
+
}
|
|
211
|
+
class LruCache {
|
|
212
|
+
limit;
|
|
213
|
+
entries = new Map();
|
|
214
|
+
constructor(limit) {
|
|
215
|
+
this.limit = limit;
|
|
216
|
+
}
|
|
217
|
+
get(key) {
|
|
218
|
+
const cached = this.entries.get(key);
|
|
219
|
+
if (cached === undefined) {
|
|
220
|
+
return undefined;
|
|
221
|
+
}
|
|
222
|
+
this.entries.delete(key);
|
|
223
|
+
this.entries.set(key, cached);
|
|
224
|
+
return cached;
|
|
225
|
+
}
|
|
226
|
+
set(key, value) {
|
|
227
|
+
if (this.limit <= 0) {
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
this.entries.delete(key);
|
|
231
|
+
this.entries.set(key, value);
|
|
232
|
+
while (this.entries.size > this.limit) {
|
|
233
|
+
const oldest = this.entries.keys().next().value;
|
|
234
|
+
if (oldest === undefined) {
|
|
235
|
+
break;
|
|
236
|
+
}
|
|
237
|
+
this.entries.delete(oldest);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Memoizes deterministic search results per isolate. The search index is
|
|
243
|
+
* static within one deploy, so identical (query, options) calls return the
|
|
244
|
+
* same hits and repeated queries skip retrieval entirely.
|
|
245
|
+
*/
|
|
246
|
+
export function withSearchResultCache(api, limit) {
|
|
247
|
+
if (limit <= 0) {
|
|
248
|
+
return api;
|
|
249
|
+
}
|
|
250
|
+
const cache = new LruCache(limit);
|
|
251
|
+
return {
|
|
252
|
+
async search(query, options) {
|
|
253
|
+
const key = JSON.stringify([query, options ?? null]);
|
|
254
|
+
const cached = cache.get(key);
|
|
255
|
+
if (cached !== undefined) {
|
|
256
|
+
return cached;
|
|
257
|
+
}
|
|
258
|
+
const hits = await api.search(query, options);
|
|
259
|
+
cache.set(key, hits);
|
|
260
|
+
return hits;
|
|
261
|
+
},
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
function resolveDefaultCache() {
|
|
265
|
+
const cachesRef = globalThis
|
|
266
|
+
.caches;
|
|
267
|
+
const defaultCache = cachesRef?.default;
|
|
268
|
+
if (defaultCache === undefined ||
|
|
269
|
+
typeof defaultCache.match !== 'function' ||
|
|
270
|
+
typeof defaultCache.put !== 'function') {
|
|
271
|
+
return undefined;
|
|
272
|
+
}
|
|
273
|
+
return defaultCache;
|
|
274
|
+
}
|
|
275
|
+
function isCacheDebugEnabled(env) {
|
|
276
|
+
const value = env?.MDORIGIN_CACHE_DEBUG;
|
|
277
|
+
return value === '1' || value === 'true';
|
|
278
|
+
}
|
|
279
|
+
function buildResponseCacheKey(deployVersion, resolved, acceptHeader, pathname) {
|
|
280
|
+
const variant = resolveCacheVariant(resolved, acceptHeader);
|
|
281
|
+
return `${deployVersion}/${variant}${pathname}`;
|
|
282
|
+
}
|
|
283
|
+
function resolveCacheVariant(resolved, acceptHeader) {
|
|
284
|
+
if (resolved.kind === 'markdown') {
|
|
285
|
+
return 'md';
|
|
286
|
+
}
|
|
287
|
+
if (resolved.kind === 'html' &&
|
|
288
|
+
!resolved.requestPath.endsWith('.html') &&
|
|
289
|
+
acceptsMarkdown(acceptHeader)) {
|
|
290
|
+
return 'md';
|
|
291
|
+
}
|
|
292
|
+
return 'html';
|
|
293
|
+
}
|
|
294
|
+
function acceptsMarkdown(acceptHeader) {
|
|
295
|
+
if (!acceptHeader) {
|
|
296
|
+
return false;
|
|
297
|
+
}
|
|
298
|
+
return acceptHeader
|
|
299
|
+
.split(',')
|
|
300
|
+
.map((part) => part.split(';', 1)[0]?.trim().toLowerCase())
|
|
301
|
+
.includes('text/markdown');
|
|
302
|
+
}
|
|
303
|
+
function toL2CacheRequest(cacheKey) {
|
|
304
|
+
return new Request(`${L2_CACHE_KEY_ORIGIN}/${cacheKey}`);
|
|
305
|
+
}
|
|
306
|
+
function toL2CacheResponse(entry) {
|
|
307
|
+
const headers = new Headers(entry.headers);
|
|
308
|
+
headers.set('cache-control', L2_CACHE_CONTROL);
|
|
309
|
+
return new Response(toResponseBody(entry), {
|
|
310
|
+
status: entry.status,
|
|
311
|
+
headers,
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
async function cachedResponseFromL2(cached) {
|
|
315
|
+
if (cached.status !== 200) {
|
|
316
|
+
return null;
|
|
317
|
+
}
|
|
318
|
+
const headers = new Headers(cached.headers);
|
|
319
|
+
headers.set('cache-control', BROWSER_CACHE_CONTROL);
|
|
320
|
+
const headerEntries = {};
|
|
321
|
+
headers.forEach((value, name) => {
|
|
322
|
+
headerEntries[name] = value;
|
|
323
|
+
});
|
|
324
|
+
return {
|
|
325
|
+
status: cached.status,
|
|
326
|
+
headers: headerEntries,
|
|
327
|
+
body: new Uint8Array(await cached.arrayBuffer()),
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
function toWorkerResponse(entry, cacheStatus) {
|
|
331
|
+
const headers = new Headers(entry.headers);
|
|
332
|
+
if (cacheStatus !== undefined) {
|
|
333
|
+
headers.set(CACHE_DEBUG_HEADER, cacheStatus);
|
|
334
|
+
}
|
|
335
|
+
return new Response(toResponseBody(entry), {
|
|
336
|
+
status: entry.status,
|
|
337
|
+
headers,
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
function toResponseBody(entry) {
|
|
341
|
+
if (entry.body instanceof Uint8Array) {
|
|
342
|
+
return new Blob([Uint8Array.from(entry.body)], {
|
|
343
|
+
type: entry.headers['content-type'],
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
return entry.body;
|
|
347
|
+
}
|
|
348
|
+
function getExternalSearchApi(manifest, env, cache, defaultApi, searchCacheLimit) {
|
|
88
349
|
if (!manifest.externalSearchEntries || manifest.externalSearchEntries.length === 0) {
|
|
89
350
|
return undefined;
|
|
90
351
|
}
|
|
91
352
|
if (!env) {
|
|
92
353
|
return (defaultApi ??
|
|
93
|
-
createSearchApiFromExternalBundle(manifest.externalSearchEntries, async (entry) => loadExternalSearchEntryResponse(entry, undefined, manifest.runtime?.r2Binding), manifest.siteConfig?.search));
|
|
354
|
+
withSearchResultCache(createSearchApiFromExternalBundle(manifest.externalSearchEntries, async (entry) => loadExternalSearchEntryResponse(entry, undefined, manifest.runtime?.r2Binding), manifest.siteConfig?.search), searchCacheLimit));
|
|
94
355
|
}
|
|
95
356
|
const cached = cache.get(env);
|
|
96
357
|
if (cached) {
|
|
97
358
|
return cached;
|
|
98
359
|
}
|
|
99
|
-
const searchApi = createSearchApiFromExternalBundle(manifest.externalSearchEntries, async (entry) => loadExternalSearchEntryResponse(entry, env, manifest.runtime?.r2Binding), manifest.siteConfig?.search);
|
|
360
|
+
const searchApi = withSearchResultCache(createSearchApiFromExternalBundle(manifest.externalSearchEntries, async (entry) => loadExternalSearchEntryResponse(entry, env, manifest.runtime?.r2Binding), manifest.siteConfig?.search), searchCacheLimit);
|
|
100
361
|
cache.set(env, searchApi);
|
|
101
362
|
return searchApi;
|
|
102
363
|
}
|
|
@@ -162,8 +423,7 @@ async function loadExternalSearchEntryResponse(entry, env, r2Binding) {
|
|
|
162
423
|
headers,
|
|
163
424
|
});
|
|
164
425
|
}
|
|
165
|
-
async function tryServeExternalBinary(manifest, request, env) {
|
|
166
|
-
const resolved = resolveRequest(new URL(request.url).pathname);
|
|
426
|
+
async function tryServeExternalBinary(manifest, request, env, resolved) {
|
|
167
427
|
if (resolved.kind !== 'asset' || !resolved.sourcePath) {
|
|
168
428
|
return null;
|
|
169
429
|
}
|
package/dist/cloudflare.js
CHANGED
|
@@ -82,6 +82,13 @@ export async function writeCloudflareBundle(options) {
|
|
|
82
82
|
assetsMaxBytes,
|
|
83
83
|
r2Binding,
|
|
84
84
|
});
|
|
85
|
+
// Deploy version = hash of the manifest payload (all entries, config, and
|
|
86
|
+
// search bundle references). Workers use it as a strong ETag so content
|
|
87
|
+
// edits produce a new version and invalidate every cached response.
|
|
88
|
+
manifest.deployVersion = createHash('sha256')
|
|
89
|
+
.update(JSON.stringify(manifest))
|
|
90
|
+
.digest('hex')
|
|
91
|
+
.slice(0, 16);
|
|
85
92
|
const packageImport = options.packageImport ?? 'mdorigin/cloudflare-runtime';
|
|
86
93
|
const workerFile = path.join(outDir, 'worker.mjs');
|
|
87
94
|
const bundleFile = path.join(outDir, BUNDLE_FILE_NAME);
|
|
@@ -163,7 +170,9 @@ export async function initCloudflareProject(options) {
|
|
|
163
170
|
' "assets": {',
|
|
164
171
|
` "directory": ${JSON.stringify(toPosixPath(path.relative(projectDir, path.join(path.dirname(options.workerEntry), bundleMetadata.assetsDir))))},`,
|
|
165
172
|
` "binding": ${JSON.stringify(DEFAULT_ASSETS_BINDING)},`,
|
|
166
|
-
' "run_worker_first":
|
|
173
|
+
' "run_worker_first": false,',
|
|
174
|
+
' "html_handling": "none",',
|
|
175
|
+
' "not_found_handling": "none"',
|
|
167
176
|
' }',
|
|
168
177
|
].join('\n')
|
|
169
178
|
: '',
|
package/dist/core/markdown.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { ContentEntry } from './content-store.js';
|
|
1
2
|
export interface ParsedDocumentMeta {
|
|
2
3
|
title?: string;
|
|
3
4
|
name?: string;
|
|
@@ -10,21 +11,37 @@ export interface ParsedDocumentMeta {
|
|
|
10
11
|
aliases?: string[];
|
|
11
12
|
[key: string]: unknown;
|
|
12
13
|
}
|
|
13
|
-
export interface ParsedDocument {
|
|
14
|
-
sourcePath: string;
|
|
15
|
-
body: string;
|
|
16
|
-
html: string;
|
|
17
|
-
meta: ParsedDocumentMeta;
|
|
18
|
-
}
|
|
19
14
|
export interface ManagedIndexEntry {
|
|
20
15
|
kind: 'directory' | 'article';
|
|
21
16
|
title: string;
|
|
22
17
|
href: string;
|
|
23
18
|
detail?: string;
|
|
24
19
|
}
|
|
20
|
+
/**
|
|
21
|
+
* Parsed document without rendered HTML. Extracting frontmatter metadata is
|
|
22
|
+
* orders of magnitude cheaper than rendering markdown to HTML, so metadata
|
|
23
|
+
* consumers (listings, sitemaps, feeds, alias scans) use this shape.
|
|
24
|
+
*/
|
|
25
|
+
export interface ParsedDocumentLite {
|
|
26
|
+
sourcePath: string;
|
|
27
|
+
body: string;
|
|
28
|
+
meta: ParsedDocumentMeta;
|
|
29
|
+
}
|
|
30
|
+
export interface ParsedDocument extends ParsedDocumentLite {
|
|
31
|
+
html: string;
|
|
32
|
+
}
|
|
25
33
|
export declare function getDocumentTitle(meta: ParsedDocumentMeta, body: string, fallback: string): string;
|
|
26
34
|
export declare function getDocumentSummary(meta: ParsedDocumentMeta, body: string): string | undefined;
|
|
27
35
|
export declare function parseMarkdownDocument(sourcePath: string, markdown: string): Promise<ParsedDocument>;
|
|
36
|
+
export declare function parseDocumentMeta(sourcePath: string, markdown: string): ParsedDocumentLite;
|
|
37
|
+
/**
|
|
38
|
+
* Parse a store entry's frontmatter metadata, memoized by entry identity.
|
|
39
|
+
* Immutable in-memory stores return the same entry object per path, so
|
|
40
|
+
* repeated metadata reads (alias scans, listings, feeds) parse each document
|
|
41
|
+
* at most once per isolate. File-system stores return fresh entry objects,
|
|
42
|
+
* which naturally bypasses the cache after content edits.
|
|
43
|
+
*/
|
|
44
|
+
export declare function parseEntryDocumentMeta(entry: ContentEntry): ParsedDocumentLite;
|
|
28
45
|
export declare function renderMarkdown(markdown: string): Promise<string>;
|
|
29
46
|
export declare function rewriteMarkdownLinksInHtml(html: string): string;
|
|
30
47
|
export declare function stripManagedIndexBlock(markdown: string): string;
|
package/dist/core/markdown.js
CHANGED
|
@@ -14,15 +14,40 @@ export function getDocumentSummary(meta, body) {
|
|
|
14
14
|
extractFirstParagraph(body));
|
|
15
15
|
}
|
|
16
16
|
export async function parseMarkdownDocument(sourcePath, markdown) {
|
|
17
|
+
const { body, meta } = parseDocumentMeta(sourcePath, markdown);
|
|
18
|
+
const html = rewriteMarkdownLinksInHtml(await renderMarkdown(body));
|
|
19
|
+
return {
|
|
20
|
+
sourcePath,
|
|
21
|
+
body,
|
|
22
|
+
html,
|
|
23
|
+
meta,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
export function parseDocumentMeta(sourcePath, markdown) {
|
|
17
27
|
const parsed = matter(markdown);
|
|
18
|
-
const html = rewriteMarkdownLinksInHtml(await renderMarkdown(parsed.content));
|
|
19
28
|
return {
|
|
20
29
|
sourcePath,
|
|
21
30
|
body: parsed.content,
|
|
22
|
-
html,
|
|
23
31
|
meta: normalizeMeta(parsed.data),
|
|
24
32
|
};
|
|
25
33
|
}
|
|
34
|
+
const entryDocumentMetaCache = new WeakMap();
|
|
35
|
+
/**
|
|
36
|
+
* Parse a store entry's frontmatter metadata, memoized by entry identity.
|
|
37
|
+
* Immutable in-memory stores return the same entry object per path, so
|
|
38
|
+
* repeated metadata reads (alias scans, listings, feeds) parse each document
|
|
39
|
+
* at most once per isolate. File-system stores return fresh entry objects,
|
|
40
|
+
* which naturally bypasses the cache after content edits.
|
|
41
|
+
*/
|
|
42
|
+
export function parseEntryDocumentMeta(entry) {
|
|
43
|
+
const cached = entryDocumentMetaCache.get(entry);
|
|
44
|
+
if (cached !== undefined) {
|
|
45
|
+
return cached;
|
|
46
|
+
}
|
|
47
|
+
const parsed = parseDocumentMeta(entry.path, entry.text ?? '');
|
|
48
|
+
entryDocumentMetaCache.set(entry, parsed);
|
|
49
|
+
return parsed;
|
|
50
|
+
}
|
|
26
51
|
export async function renderMarkdown(markdown) {
|
|
27
52
|
const output = await remark()
|
|
28
53
|
.use(remarkGfm)
|
|
@@ -2,7 +2,7 @@ import path from 'node:path';
|
|
|
2
2
|
import { isIgnoredContentName } from './content-store.js';
|
|
3
3
|
import { inferDirectoryContentType, resolveContentType, } from './content-type.js';
|
|
4
4
|
import { getDirectoryIndexCandidates } from './directory-index.js';
|
|
5
|
-
import { extractManagedIndexEntries, getDocumentSummary, getDocumentTitle as getParsedDocumentTitle, parseMarkdownDocument, stripManagedIndexBlock, stripManagedIndexLinks, stripMachineOnlyMarkdownComments, } from './markdown.js';
|
|
5
|
+
import { extractManagedIndexEntries, getDocumentSummary, getDocumentTitle as getParsedDocumentTitle, parseEntryDocumentMeta, parseMarkdownDocument, stripManagedIndexBlock, stripManagedIndexLinks, stripMachineOnlyMarkdownComments, } from './markdown.js';
|
|
6
6
|
import { ensureTrailingSlash, trimLeadingSlash } from './site-url.js';
|
|
7
7
|
import { applyIndexTransforms, renderFooterOverride, renderHeaderOverride, renderPageWithPlugins, transformHtmlWithPlugins, } from './extensions.js';
|
|
8
8
|
import { handleApiRoute } from './api.js';
|
|
@@ -82,7 +82,7 @@ export async function handleSiteRequest(store, pathname, options) {
|
|
|
82
82
|
return renderNotFoundForResolvedRequest(store, resolved, options, negotiatedMarkdown, requestLocale);
|
|
83
83
|
}
|
|
84
84
|
if (resolved.kind === 'markdown' || negotiatedMarkdown) {
|
|
85
|
-
const parsed =
|
|
85
|
+
const parsed = parseEntryDocumentMeta(entry);
|
|
86
86
|
if (parsed.meta.draft === true && options.draftMode === 'exclude') {
|
|
87
87
|
return renderNotFoundForResolvedRequest(store, resolved, options, negotiatedMarkdown, requestLocale);
|
|
88
88
|
}
|
|
@@ -94,11 +94,11 @@ export async function handleSiteRequest(store, pathname, options) {
|
|
|
94
94
|
body: entry.text,
|
|
95
95
|
};
|
|
96
96
|
}
|
|
97
|
-
const parsed =
|
|
97
|
+
const parsed = parseEntryDocumentMeta(entry);
|
|
98
98
|
if (parsed.meta.draft === true && options.draftMode === 'exclude') {
|
|
99
99
|
return renderNotFoundForResolvedRequest(store, resolved, options, negotiatedMarkdown, requestLocale);
|
|
100
100
|
}
|
|
101
|
-
const navigation = await resolveTopNav(store, options.siteConfig);
|
|
101
|
+
const navigation = await resolveTopNav(store, options.siteConfig, requestLocale);
|
|
102
102
|
const renderedBody = isRootHomeRequest(resolved.requestPath) && !options.siteConfig.showHomeIndex
|
|
103
103
|
? stripManagedIndexBlock(entry.text)
|
|
104
104
|
: isRootHomeRequest(resolved.requestPath) && navigation.items.length > 0
|
|
@@ -114,9 +114,7 @@ export async function handleSiteRequest(store, pathname, options) {
|
|
|
114
114
|
return renderListingPostsFragment(listingEntries, listingFragmentRequest);
|
|
115
115
|
}
|
|
116
116
|
const documentBody = listingEntries.length > 0 ? stripManagedIndexBlock(renderedBody) : renderedBody;
|
|
117
|
-
const renderedParsed =
|
|
118
|
-
? parsed
|
|
119
|
-
: await parseMarkdownDocument(resolved.sourcePath, documentBody);
|
|
117
|
+
const renderedParsed = await parseMarkdownDocument(resolved.sourcePath, documentBody);
|
|
120
118
|
return renderStructuredPage({
|
|
121
119
|
requestPath: resolved.requestPath,
|
|
122
120
|
sourcePath: resolved.sourcePath,
|
|
@@ -166,8 +164,8 @@ function buildPageRenderModel(options) {
|
|
|
166
164
|
sourcePath: options.sourcePath,
|
|
167
165
|
locale: options.locale,
|
|
168
166
|
languages: options.languages,
|
|
169
|
-
siteTitle: options.siteConfig.siteTitle,
|
|
170
|
-
siteDescription: options.siteConfig.siteDescription,
|
|
167
|
+
siteTitle: options.siteTitle ?? options.siteConfig.siteTitle,
|
|
168
|
+
siteDescription: options.siteDescription ?? options.siteConfig.siteDescription,
|
|
171
169
|
siteUrl: options.siteConfig.siteUrl,
|
|
172
170
|
favicon: options.siteConfig.favicon,
|
|
173
171
|
socialImage: options.siteConfig.socialImage,
|
|
@@ -182,7 +180,7 @@ function buildPageRenderModel(options) {
|
|
|
182
180
|
showSummary: options.siteConfig.showSummary,
|
|
183
181
|
showDate: options.siteConfig.showDate,
|
|
184
182
|
topNav: options.topNav,
|
|
185
|
-
footerNav: options.siteConfig.footerNav,
|
|
183
|
+
footerNav: options.footerNav ?? options.siteConfig.footerNav,
|
|
186
184
|
footerText: options.siteConfig.footerText,
|
|
187
185
|
socialLinks: options.siteConfig.socialLinks,
|
|
188
186
|
editLink: options.siteConfig.editLink,
|
|
@@ -213,6 +211,9 @@ async function renderStructuredPage(options) {
|
|
|
213
211
|
renderedBodyHtml: options.renderedParsed.html,
|
|
214
212
|
parsed: options.parsed,
|
|
215
213
|
siteConfig: options.siteConfig,
|
|
214
|
+
siteTitle: getEffectiveSiteTitle(options.siteConfig, localeConfig),
|
|
215
|
+
siteDescription: getEffectiveSiteDescription(options.siteConfig, localeConfig),
|
|
216
|
+
footerNav: getEffectiveFooterNav(options.siteConfig, localeConfig),
|
|
216
217
|
topNav: options.topNav,
|
|
217
218
|
listingEntries: options.listingEntries,
|
|
218
219
|
searchEnabled: options.searchEnabled,
|
|
@@ -334,8 +335,8 @@ async function renderNotFoundForResolvedRequest(store, resolved, options, negoti
|
|
|
334
335
|
return renderHtmlNotFound(store, resolved.requestPath, options, varyOnAccept, requestLocale);
|
|
335
336
|
}
|
|
336
337
|
async function renderHtmlNotFound(store, requestPath, options, varyOnAccept, requestLocale) {
|
|
337
|
-
const navigation = await resolveTopNav(store, options.siteConfig);
|
|
338
338
|
const localeConfig = requestLocale;
|
|
339
|
+
const navigation = await resolveTopNav(store, options.siteConfig, localeConfig);
|
|
339
340
|
const locale = localeConfig?.code ?? options.siteConfig.locale;
|
|
340
341
|
const messages = getEffectiveLocaleMessages(options.siteConfig, localeConfig);
|
|
341
342
|
const languages = await buildLanguageOptions(store, {}, localeConfig, options.siteConfig, options.draftMode);
|
|
@@ -351,8 +352,8 @@ async function renderHtmlNotFound(store, requestPath, options, varyOnAccept, req
|
|
|
351
352
|
'content-type': 'text/html; charset=utf-8',
|
|
352
353
|
}, varyOnAccept),
|
|
353
354
|
body: renderDocument({
|
|
354
|
-
siteTitle: options.siteConfig
|
|
355
|
-
siteDescription: options.siteConfig
|
|
355
|
+
siteTitle: getEffectiveSiteTitle(options.siteConfig, localeConfig),
|
|
356
|
+
siteDescription: getEffectiveSiteDescription(options.siteConfig, localeConfig),
|
|
356
357
|
siteUrl: options.siteConfig.siteUrl,
|
|
357
358
|
favicon: options.siteConfig.favicon,
|
|
358
359
|
socialImage: options.siteConfig.socialImage,
|
|
@@ -365,7 +366,7 @@ async function renderHtmlNotFound(store, requestPath, options, varyOnAccept, req
|
|
|
365
366
|
showSummary: false,
|
|
366
367
|
showDate: false,
|
|
367
368
|
topNav: navigation.items,
|
|
368
|
-
footerNav: options.siteConfig
|
|
369
|
+
footerNav: getEffectiveFooterNav(options.siteConfig, localeConfig),
|
|
369
370
|
footerText: options.siteConfig.footerText,
|
|
370
371
|
socialLinks: options.siteConfig.socialLinks,
|
|
371
372
|
stylesheetContent: options.siteConfig.stylesheetContent,
|
|
@@ -402,6 +403,18 @@ function getEffectiveLocaleMessages(siteConfig, localeConfig) {
|
|
|
402
403
|
...localeConfig.messages,
|
|
403
404
|
});
|
|
404
405
|
}
|
|
406
|
+
function getEffectiveSiteTitle(siteConfig, localeConfig) {
|
|
407
|
+
return localeConfig?.siteTitle ?? siteConfig.siteTitle;
|
|
408
|
+
}
|
|
409
|
+
function getEffectiveSiteDescription(siteConfig, localeConfig) {
|
|
410
|
+
return localeConfig?.siteDescription ?? siteConfig.siteDescription;
|
|
411
|
+
}
|
|
412
|
+
function getEffectiveFooterNav(siteConfig, localeConfig) {
|
|
413
|
+
if (localeConfig !== null && (localeConfig.footerNav?.length ?? 0) > 0) {
|
|
414
|
+
return localeConfig.footerNav;
|
|
415
|
+
}
|
|
416
|
+
return siteConfig.footerNav;
|
|
417
|
+
}
|
|
405
418
|
function getFrontmatterLocale(meta) {
|
|
406
419
|
if (typeof meta.lang !== 'string') {
|
|
407
420
|
return null;
|
|
@@ -473,7 +486,7 @@ async function hasPublishedContent(store, contentPath, draftMode) {
|
|
|
473
486
|
return false;
|
|
474
487
|
}
|
|
475
488
|
if (draftMode === 'exclude') {
|
|
476
|
-
const parsed =
|
|
489
|
+
const parsed = parseEntryDocumentMeta(entry);
|
|
477
490
|
if (parsed.meta.draft === true) {
|
|
478
491
|
return false;
|
|
479
492
|
}
|
|
@@ -552,8 +565,9 @@ async function renderRssFeed(store, options, locale) {
|
|
|
552
565
|
const items = await collectRssFeedItems(store, locale?.contentBase ?? '', options, excludedLocaleDirectories);
|
|
553
566
|
const limitedItems = items.slice(0, options.siteConfig.rss?.maxItems ?? 20);
|
|
554
567
|
const rssFeedUrl = getRssFeedUrl(options.siteConfig.siteUrl, options.siteConfig, locale);
|
|
555
|
-
const title = options.siteConfig.rss?.title ?? options.siteConfig
|
|
556
|
-
const description = options.siteConfig.rss?.description ??
|
|
568
|
+
const title = options.siteConfig.rss?.title ?? getEffectiveSiteTitle(options.siteConfig, locale);
|
|
569
|
+
const description = options.siteConfig.rss?.description ??
|
|
570
|
+
getEffectiveSiteDescription(options.siteConfig, locale);
|
|
557
571
|
const lastBuildDate = limitedItems[0]?.pubDate.toUTCString();
|
|
558
572
|
const body = [
|
|
559
573
|
'<?xml version="1.0" encoding="UTF-8"?>',
|
|
@@ -642,7 +656,7 @@ async function collectSitemapEntries(store, directoryPath, options) {
|
|
|
642
656
|
if (document === null || document.kind !== 'text' || document.text === undefined) {
|
|
643
657
|
continue;
|
|
644
658
|
}
|
|
645
|
-
const parsed =
|
|
659
|
+
const parsed = parseEntryDocumentMeta(document);
|
|
646
660
|
if (parsed.meta.draft === true && options.draftMode === 'exclude') {
|
|
647
661
|
continue;
|
|
648
662
|
}
|
|
@@ -690,7 +704,7 @@ async function collectRssFeedItems(store, directoryPath, options, excludedDirect
|
|
|
690
704
|
if (document === null || document.kind !== 'text' || document.text === undefined) {
|
|
691
705
|
continue;
|
|
692
706
|
}
|
|
693
|
-
const parsed =
|
|
707
|
+
const parsed = parseEntryDocumentMeta(document);
|
|
694
708
|
if (parsed.meta.draft === true && options.draftMode === 'exclude') {
|
|
695
709
|
continue;
|
|
696
710
|
}
|
|
@@ -746,8 +760,8 @@ async function renderDirectoryListing(store, requestPath, siteConfig, searchEnab
|
|
|
746
760
|
return notFound();
|
|
747
761
|
}
|
|
748
762
|
const visibleEntries = entries.filter(isVisibleDirectoryEntry);
|
|
749
|
-
const navigation = await resolveTopNav(store, siteConfig);
|
|
750
763
|
const locale = requestLocale?.code ?? siteConfig.locale;
|
|
764
|
+
const navigation = await resolveTopNav(store, siteConfig, requestLocale);
|
|
751
765
|
const messages = getEffectiveLocaleMessages(siteConfig, requestLocale);
|
|
752
766
|
const languages = await buildLanguageOptions(store, { directoryPath }, requestLocale, siteConfig, 'exclude');
|
|
753
767
|
const listItems = visibleEntries
|
|
@@ -765,8 +779,8 @@ async function renderDirectoryListing(store, requestPath, siteConfig, searchEnab
|
|
|
765
779
|
'content-type': 'text/html; charset=utf-8',
|
|
766
780
|
},
|
|
767
781
|
body: renderDocument({
|
|
768
|
-
siteTitle: siteConfig
|
|
769
|
-
siteDescription: siteConfig
|
|
782
|
+
siteTitle: getEffectiveSiteTitle(siteConfig, requestLocale),
|
|
783
|
+
siteDescription: getEffectiveSiteDescription(siteConfig, requestLocale),
|
|
770
784
|
locale,
|
|
771
785
|
messages,
|
|
772
786
|
languages,
|
|
@@ -778,7 +792,7 @@ async function renderDirectoryListing(store, requestPath, siteConfig, searchEnab
|
|
|
778
792
|
showSummary: false,
|
|
779
793
|
showDate: false,
|
|
780
794
|
topNav: navigation.items,
|
|
781
|
-
footerNav: siteConfig
|
|
795
|
+
footerNav: getEffectiveFooterNav(siteConfig, requestLocale),
|
|
782
796
|
footerText: siteConfig.footerText,
|
|
783
797
|
socialLinks: siteConfig.socialLinks,
|
|
784
798
|
stylesheetContent: siteConfig.stylesheetContent,
|
|
@@ -826,11 +840,12 @@ async function tryRenderAlternateDirectoryIndex(store, requestPath, options) {
|
|
|
826
840
|
if (entry === null || entry.kind !== 'text' || entry.text === undefined) {
|
|
827
841
|
continue;
|
|
828
842
|
}
|
|
829
|
-
const parsed =
|
|
843
|
+
const parsed = parseEntryDocumentMeta(entry);
|
|
830
844
|
if (parsed.meta.draft === true && options.draftMode === 'exclude') {
|
|
831
845
|
return notFound();
|
|
832
846
|
}
|
|
833
|
-
const
|
|
847
|
+
const requestLocale = matchRequestLocale(requestPath, options.siteConfig.locales);
|
|
848
|
+
const navigation = await resolveTopNav(store, options.siteConfig, requestLocale);
|
|
834
849
|
const renderedBody = isRootHomeRequest(requestPath) && !options.siteConfig.showHomeIndex
|
|
835
850
|
? stripManagedIndexBlock(entry.text)
|
|
836
851
|
: isRootHomeRequest(requestPath) && navigation.items.length > 0
|
|
@@ -847,9 +862,7 @@ async function tryRenderAlternateDirectoryIndex(store, requestPath, options) {
|
|
|
847
862
|
return renderListingPostsFragment(listingEntries, listingFragmentRequest);
|
|
848
863
|
}
|
|
849
864
|
const documentBody = listingEntries.length > 0 ? stripManagedIndexBlock(renderedBody) : renderedBody;
|
|
850
|
-
const renderedParsed =
|
|
851
|
-
? parsed
|
|
852
|
-
: await parseMarkdownDocument(candidatePath, documentBody);
|
|
865
|
+
const renderedParsed = await parseMarkdownDocument(candidatePath, documentBody);
|
|
853
866
|
return renderStructuredPage({
|
|
854
867
|
requestPath,
|
|
855
868
|
sourcePath: candidatePath,
|
|
@@ -861,8 +874,12 @@ async function tryRenderAlternateDirectoryIndex(store, requestPath, options) {
|
|
|
861
874
|
searchEnabled: options.searchApi !== undefined,
|
|
862
875
|
plugins,
|
|
863
876
|
store,
|
|
864
|
-
requestLocale
|
|
877
|
+
requestLocale,
|
|
865
878
|
draftMode: options.draftMode,
|
|
879
|
+
// This branch only renders extensionless directory routes, which serve
|
|
880
|
+
// a raw markdown variant through Accept negotiation; the HTML variant
|
|
881
|
+
// must therefore declare Vary: Accept like every other negotiated route.
|
|
882
|
+
varyOnAccept: true,
|
|
866
883
|
});
|
|
867
884
|
}
|
|
868
885
|
return null;
|
|
@@ -883,7 +900,7 @@ async function tryServeAlternateDirectoryMarkdown(store, resolved, options, nego
|
|
|
883
900
|
if (entry === null || entry.kind !== 'text' || entry.text === undefined) {
|
|
884
901
|
continue;
|
|
885
902
|
}
|
|
886
|
-
const parsed =
|
|
903
|
+
const parsed = parseEntryDocumentMeta(entry);
|
|
887
904
|
if (parsed.meta.draft === true && options.draftMode === 'exclude') {
|
|
888
905
|
return notFound();
|
|
889
906
|
}
|
|
@@ -914,7 +931,7 @@ async function tryRedirectAlternateDirectoryMarkdown(store, resolved, options) {
|
|
|
914
931
|
if (entry === null || entry.kind !== 'text' || entry.text === undefined) {
|
|
915
932
|
continue;
|
|
916
933
|
}
|
|
917
|
-
const parsed =
|
|
934
|
+
const parsed = parseEntryDocumentMeta(entry);
|
|
918
935
|
if (parsed.meta.draft === true && options.draftMode === 'exclude') {
|
|
919
936
|
return null;
|
|
920
937
|
}
|
|
@@ -994,7 +1011,7 @@ async function findAliasRedirectLocation(store, directoryPath, requestPath, opti
|
|
|
994
1011
|
if (document === null || document.kind !== 'text' || document.text === undefined) {
|
|
995
1012
|
continue;
|
|
996
1013
|
}
|
|
997
|
-
const parsed =
|
|
1014
|
+
const parsed = parseEntryDocumentMeta(document);
|
|
998
1015
|
if (parsed.meta.draft === true && options.draftMode === 'exclude') {
|
|
999
1016
|
continue;
|
|
1000
1017
|
}
|
|
@@ -1053,14 +1070,21 @@ function getEditLinkHref(siteConfig, sourcePath) {
|
|
|
1053
1070
|
}
|
|
1054
1071
|
return `${siteConfig.editLink.baseUrl}${sourcePath}`;
|
|
1055
1072
|
}
|
|
1056
|
-
async function resolveTopNav(store, siteConfig) {
|
|
1073
|
+
async function resolveTopNav(store, siteConfig, localeConfig = null) {
|
|
1074
|
+
if (localeConfig !== null && (localeConfig.topNav?.length ?? 0) > 0) {
|
|
1075
|
+
return {
|
|
1076
|
+
items: localeConfig.topNav,
|
|
1077
|
+
autoGenerated: false,
|
|
1078
|
+
};
|
|
1079
|
+
}
|
|
1057
1080
|
if (siteConfig.topNav.length > 0) {
|
|
1058
1081
|
return {
|
|
1059
1082
|
items: siteConfig.topNav,
|
|
1060
1083
|
autoGenerated: false,
|
|
1061
1084
|
};
|
|
1062
1085
|
}
|
|
1063
|
-
const
|
|
1086
|
+
const contentRoot = localeConfig?.contentBase ?? '';
|
|
1087
|
+
const rootEntries = await store.listDirectory(contentRoot);
|
|
1064
1088
|
if (rootEntries === null) {
|
|
1065
1089
|
return {
|
|
1066
1090
|
items: [],
|
|
@@ -1074,7 +1098,7 @@ async function resolveTopNav(store, siteConfig) {
|
|
|
1074
1098
|
const navItems = [];
|
|
1075
1099
|
const orderedNavItems = [];
|
|
1076
1100
|
for (const entry of directories) {
|
|
1077
|
-
if (localeDirectoryNames.has(entry.name)) {
|
|
1101
|
+
if (contentRoot === '' && localeDirectoryNames.has(entry.name)) {
|
|
1078
1102
|
continue;
|
|
1079
1103
|
}
|
|
1080
1104
|
const resolved = await resolveDirectoryNav(store, entry);
|
|
@@ -1083,7 +1107,7 @@ async function resolveTopNav(store, siteConfig) {
|
|
|
1083
1107
|
}
|
|
1084
1108
|
orderedNavItems.push({
|
|
1085
1109
|
label: resolved.title,
|
|
1086
|
-
href:
|
|
1110
|
+
href: `${localeConfig?.pathPrefix ?? ''}/${entry.name}/`,
|
|
1087
1111
|
order: resolved.order,
|
|
1088
1112
|
});
|
|
1089
1113
|
}
|
|
@@ -1113,7 +1137,7 @@ async function resolveDirectoryNav(store, entry) {
|
|
|
1113
1137
|
if (contentEntry === null || contentEntry.kind !== 'text' || contentEntry.text === undefined) {
|
|
1114
1138
|
continue;
|
|
1115
1139
|
}
|
|
1116
|
-
const parsed =
|
|
1140
|
+
const parsed = parseEntryDocumentMeta(contentEntry);
|
|
1117
1141
|
const shape = await inspectDirectoryShape(store, entry.path);
|
|
1118
1142
|
return {
|
|
1119
1143
|
title: getParsedDocumentTitle(parsed.meta, parsed.body, entry.name),
|
|
@@ -147,6 +147,14 @@ export interface LocaleConfigInput {
|
|
|
147
147
|
default?: boolean;
|
|
148
148
|
/** UI message overrides for this locale; win over global `messages`. */
|
|
149
149
|
messages?: Partial<SiteMessages>;
|
|
150
|
+
/** Site title override for this locale; wins over the global `siteTitle`. */
|
|
151
|
+
siteTitle?: string;
|
|
152
|
+
/** Site description override for this locale; wins over the global `siteDescription`. */
|
|
153
|
+
siteDescription?: string;
|
|
154
|
+
/** Explicit top navigation for this locale; wins over the global `topNav`. */
|
|
155
|
+
topNav?: SiteNavItem[];
|
|
156
|
+
/** Explicit footer navigation for this locale; wins over the global `footerNav`. */
|
|
157
|
+
footerNav?: SiteNavItem[];
|
|
150
158
|
}
|
|
151
159
|
export interface ResolvedLocaleConfig {
|
|
152
160
|
code: string;
|
|
@@ -156,6 +164,14 @@ export interface ResolvedLocaleConfig {
|
|
|
156
164
|
/** Content base directory: '' (content root) for an unprefixed default locale, otherwise `{code}`. */
|
|
157
165
|
contentBase: string;
|
|
158
166
|
messages: Partial<SiteMessages>;
|
|
167
|
+
/** Site title override; undefined falls back to the global `siteTitle`. */
|
|
168
|
+
siteTitle?: string;
|
|
169
|
+
/** Site description override; undefined falls back to the global `siteDescription`. */
|
|
170
|
+
siteDescription?: string;
|
|
171
|
+
/** Explicit top navigation override; empty array falls back to the global `topNav`. */
|
|
172
|
+
topNav: SiteNavItem[];
|
|
173
|
+
/** Explicit footer navigation override; empty array falls back to the global `footerNav`. */
|
|
174
|
+
footerNav: SiteNavItem[];
|
|
159
175
|
}
|
|
160
176
|
export declare function loadSiteConfig(options?: LoadSiteConfigOptions): Promise<ResolvedSiteConfig>;
|
|
161
177
|
export declare function loadUserSiteConfig(options?: LoadSiteConfigOptions): Promise<LoadedSiteConfig>;
|
package/dist/core/site-config.js
CHANGED
|
@@ -3,7 +3,7 @@ import path from 'node:path';
|
|
|
3
3
|
import { pathToFileURL } from 'node:url';
|
|
4
4
|
import { tsImport } from 'tsx/esm/api';
|
|
5
5
|
import { getDirectoryIndexCandidates } from './directory-index.js';
|
|
6
|
-
import {
|
|
6
|
+
import { parseEntryDocumentMeta } from './markdown.js';
|
|
7
7
|
import { DEFAULT_SITE_LOCALE, validateSiteMessages, } from '../i18n/messages.js';
|
|
8
8
|
export async function loadSiteConfig(options = {}) {
|
|
9
9
|
return (await loadUserSiteConfig(options)).siteConfig;
|
|
@@ -78,7 +78,7 @@ export async function applySiteConfigFrontmatterDefaults(store, siteConfig) {
|
|
|
78
78
|
if (entry === null || entry.kind !== 'text' || entry.text === undefined) {
|
|
79
79
|
continue;
|
|
80
80
|
}
|
|
81
|
-
const parsed =
|
|
81
|
+
const parsed = parseEntryDocumentMeta(entry);
|
|
82
82
|
return {
|
|
83
83
|
...siteConfig,
|
|
84
84
|
siteTitle: siteConfig.siteTitleConfigured
|
|
@@ -151,6 +151,10 @@ function resolveLocalesConfig(parsedConfig, configFilePath) {
|
|
|
151
151
|
pathPrefix,
|
|
152
152
|
isDefault,
|
|
153
153
|
messages,
|
|
154
|
+
siteTitle: normalizeOptionalSiteText(locale.siteTitle),
|
|
155
|
+
siteDescription: normalizeOptionalSiteText(locale.siteDescription),
|
|
156
|
+
topNav: normalizeTopNav(locale.topNav),
|
|
157
|
+
footerNav: normalizeTopNav(locale.footerNav),
|
|
154
158
|
});
|
|
155
159
|
}
|
|
156
160
|
const defaultCount = parsed.filter((locale) => locale.isDefault).length;
|
|
@@ -283,6 +287,13 @@ function normalizeTopNav(value) {
|
|
|
283
287
|
return [];
|
|
284
288
|
});
|
|
285
289
|
}
|
|
290
|
+
function normalizeOptionalSiteText(value) {
|
|
291
|
+
if (typeof value !== 'string') {
|
|
292
|
+
return undefined;
|
|
293
|
+
}
|
|
294
|
+
const trimmed = value.trim();
|
|
295
|
+
return trimmed === '' ? undefined : trimmed;
|
|
296
|
+
}
|
|
286
297
|
function normalizePositiveInteger(value, fallback) {
|
|
287
298
|
if (typeof value === 'number' && Number.isInteger(value) && value > 0) {
|
|
288
299
|
return value;
|
package/dist/html/template.js
CHANGED
|
@@ -60,10 +60,19 @@ export function renderDocument(options) {
|
|
|
60
60
|
'</div>',
|
|
61
61
|
].join('')
|
|
62
62
|
: '';
|
|
63
|
-
const
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
63
|
+
const currentLanguage = options.languages?.find((language) => language.current) ??
|
|
64
|
+
options.languages?.[0];
|
|
65
|
+
const languagesBlock = options.languages && options.languages.length > 1 && currentLanguage
|
|
66
|
+
? [
|
|
67
|
+
`<nav class="site-languages" aria-label="${escapeHtml(messages['languages.ariaLabel'])}">`,
|
|
68
|
+
'<details class="site-languages__details" data-site-languages>',
|
|
69
|
+
`<summary class="site-languages__toggle"><span class="site-languages__label">${escapeHtml(currentLanguage.label)}</span><svg class="site-languages__chevron" viewBox="0 0 16 16" width="11" height="11" aria-hidden="true" focusable="false"><path d="M4 6.5 8 10.5 12 6.5" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round"></path></svg></summary>`,
|
|
70
|
+
`<ul class="site-languages__panel">${options.languages
|
|
71
|
+
.map((language) => `<li><a href="${escapeHtml(language.href)}" hreflang="${escapeHtml(language.code)}"${language.current ? ' aria-current="page"' : ''}>${escapeHtml(language.label)}</a></li>`)
|
|
72
|
+
.join('')}</ul>`,
|
|
73
|
+
'</details>',
|
|
74
|
+
'</nav>',
|
|
75
|
+
].join('')
|
|
67
76
|
: '';
|
|
68
77
|
const footerNavBlock = options.footerNav && options.footerNav.length > 0
|
|
69
78
|
? `<nav class="site-footer__nav"><ul>${options.footerNav
|
|
@@ -110,6 +119,7 @@ export function renderDocument(options) {
|
|
|
110
119
|
const searchScript = options.searchEnabled
|
|
111
120
|
? renderSearchScript(messages, options.searchLocaleFilter)
|
|
112
121
|
: '';
|
|
122
|
+
const languagesScript = options.headerHtml || !languagesBlock ? '' : renderLanguagesScript();
|
|
113
123
|
const headerBlock = options.headerHtml ??
|
|
114
124
|
`<header class="site-header"><div class="site-header__inner"><div class="site-header__brand"><p class="site-header__title"><a href="${brandHref}">${logoBlock}<span>${siteTitle}</span></a></p>${siteDescriptionBlock}</div><div class="site-header__actions">${navBlock}${languagesBlock}${searchToggleBlock}</div></div></header>`;
|
|
115
125
|
const renderedFooterBlock = options.footerHtml ?? footerBlock;
|
|
@@ -136,6 +146,7 @@ export function renderDocument(options) {
|
|
|
136
146
|
'</main>',
|
|
137
147
|
renderedFooterBlock,
|
|
138
148
|
searchScript,
|
|
149
|
+
languagesScript,
|
|
139
150
|
'</body>',
|
|
140
151
|
'</html>',
|
|
141
152
|
].join('');
|
|
@@ -294,6 +305,35 @@ function renderListingLoadMoreScript(messages) {
|
|
|
294
305
|
})();
|
|
295
306
|
</script>`;
|
|
296
307
|
}
|
|
308
|
+
function renderLanguagesScript() {
|
|
309
|
+
return `<script>
|
|
310
|
+
(function () {
|
|
311
|
+
const menus = Array.from(document.querySelectorAll("details[data-site-languages]"));
|
|
312
|
+
if (menus.length === 0) return;
|
|
313
|
+
document.addEventListener("click", (event) => {
|
|
314
|
+
for (const menu of menus) {
|
|
315
|
+
if (menu.open && !menu.contains(event.target)) menu.open = false;
|
|
316
|
+
}
|
|
317
|
+
});
|
|
318
|
+
document.addEventListener("keydown", (event) => {
|
|
319
|
+
if (event.key !== "Escape") return;
|
|
320
|
+
for (const menu of menus) {
|
|
321
|
+
if (!menu.open) continue;
|
|
322
|
+
menu.open = false;
|
|
323
|
+
menu.querySelector("summary")?.focus();
|
|
324
|
+
}
|
|
325
|
+
});
|
|
326
|
+
for (const menu of menus) {
|
|
327
|
+
menu.addEventListener("toggle", () => {
|
|
328
|
+
if (!menu.open) return;
|
|
329
|
+
for (const other of menus) {
|
|
330
|
+
if (other !== menu) other.open = false;
|
|
331
|
+
}
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
})();
|
|
335
|
+
</script>`;
|
|
336
|
+
}
|
|
297
337
|
function renderSearchScript(messages, localeFilter) {
|
|
298
338
|
return [
|
|
299
339
|
'<script>',
|
package/dist/html/theme.js
CHANGED
|
@@ -112,6 +112,10 @@ function buildSharedSearchStyles() {
|
|
|
112
112
|
flex-direction: column;
|
|
113
113
|
align-items: stretch;
|
|
114
114
|
}
|
|
115
|
+
.site-languages__panel {
|
|
116
|
+
left: 0;
|
|
117
|
+
right: auto;
|
|
118
|
+
}
|
|
115
119
|
.site-search__panel {
|
|
116
120
|
left: 0;
|
|
117
121
|
right: auto;
|
|
@@ -219,39 +223,76 @@ a:hover { color: var(--link-hover); text-decoration: underline; }
|
|
|
219
223
|
white-space: nowrap;
|
|
220
224
|
}
|
|
221
225
|
.site-nav a:hover { color: var(--link-hover); }
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
gap: 0.6rem;
|
|
227
|
-
margin: 0;
|
|
228
|
-
padding: 0;
|
|
229
|
-
align-items: center;
|
|
226
|
+
main {
|
|
227
|
+
max-width: calc(var(--max) + 4rem);
|
|
228
|
+
margin: 0 auto;
|
|
229
|
+
padding: 2rem;
|
|
230
230
|
}
|
|
231
|
-
.site-languages
|
|
231
|
+
.site-languages {
|
|
232
|
+
position: relative;
|
|
232
233
|
display: flex;
|
|
233
|
-
align-items:
|
|
234
|
+
align-items: flex-end;
|
|
234
235
|
}
|
|
235
|
-
.site-
|
|
236
|
+
.site-languages__toggle {
|
|
237
|
+
list-style: none;
|
|
238
|
+
appearance: none;
|
|
239
|
+
display: inline-flex;
|
|
240
|
+
align-items: center;
|
|
241
|
+
gap: 0.35rem;
|
|
242
|
+
min-height: 2.25rem;
|
|
243
|
+
padding: 0.45rem 0.82rem;
|
|
244
|
+
border: 1px solid var(--border);
|
|
245
|
+
border-radius: 999px;
|
|
246
|
+
background: color-mix(in srgb, var(--surface) 92%, transparent);
|
|
236
247
|
color: var(--muted);
|
|
248
|
+
font: inherit;
|
|
237
249
|
font-size: 0.85rem;
|
|
238
|
-
|
|
250
|
+
cursor: pointer;
|
|
251
|
+
white-space: nowrap;
|
|
252
|
+
}
|
|
253
|
+
.site-languages__toggle::-webkit-details-marker { display: none; }
|
|
254
|
+
.site-languages__toggle:hover,
|
|
255
|
+
.site-languages__details[open] > .site-languages__toggle { color: var(--text); }
|
|
256
|
+
.site-languages__chevron {
|
|
257
|
+
flex: none;
|
|
258
|
+
transition: transform 0.15s ease;
|
|
259
|
+
}
|
|
260
|
+
.site-languages__details[open] > .site-languages__toggle .site-languages__chevron {
|
|
261
|
+
transform: rotate(180deg);
|
|
262
|
+
}
|
|
263
|
+
.site-languages__panel {
|
|
264
|
+
position: absolute;
|
|
265
|
+
top: calc(100% + 0.7rem);
|
|
266
|
+
right: 0;
|
|
267
|
+
list-style: none;
|
|
268
|
+
margin: 0;
|
|
269
|
+
padding: 0.4rem;
|
|
270
|
+
min-width: 9rem;
|
|
271
|
+
border: 1px solid var(--border);
|
|
272
|
+
border-radius: 14px;
|
|
273
|
+
background: color-mix(in srgb, var(--surface) 95%, white 5%);
|
|
274
|
+
box-shadow: 0 18px 40px rgba(24, 18, 11, 0.14);
|
|
275
|
+
z-index: 2;
|
|
276
|
+
}
|
|
277
|
+
.site-languages__panel li { display: flex; }
|
|
278
|
+
.site-languages__panel a {
|
|
279
|
+
flex: 1;
|
|
280
|
+
display: flex;
|
|
239
281
|
align-items: center;
|
|
240
282
|
min-height: 2.25rem;
|
|
241
|
-
padding: 0 0.
|
|
283
|
+
padding: 0 0.7rem;
|
|
284
|
+
border-radius: 9px;
|
|
285
|
+
color: var(--muted);
|
|
286
|
+
font-size: 0.88rem;
|
|
242
287
|
white-space: nowrap;
|
|
243
288
|
}
|
|
244
|
-
.site-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
font-weight: 700;
|
|
248
|
-
text-decoration: underline;
|
|
249
|
-
text-underline-offset: 0.25em;
|
|
289
|
+
.site-languages__panel a:hover {
|
|
290
|
+
color: var(--text);
|
|
291
|
+
background: color-mix(in srgb, var(--border) 26%, transparent);
|
|
250
292
|
}
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
padding: 2rem;
|
|
293
|
+
.site-languages__panel a[aria-current='page'] {
|
|
294
|
+
color: var(--text);
|
|
295
|
+
font-weight: 700;
|
|
255
296
|
}
|
|
256
297
|
.site-footer {
|
|
257
298
|
max-width: calc(var(--max) + 4rem);
|