@sevenfold/setto-client 0.48.0 → 0.50.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/LanguageSwitcher.d.ts +11 -0
- package/dist/blog/SettoBlog.d.ts +18 -1
- package/dist/pages/head.d.ts +36 -0
- package/dist/setto-client.js +64 -17
- package/dist/setto-client.js.map +1 -1
- package/dist/vite/pages.d.ts +68 -0
- package/dist/vite/plugin.d.ts +10 -0
- package/dist/vite/static.d.ts +16 -3
- package/dist/vite.js +272 -71
- package/dist/vite.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { type PagesManifest } from '../pages/manifest.js';
|
|
2
|
+
import { type HeadMeta } from './static.js';
|
|
3
|
+
/** Where the sites keep their language bundles: `src/i18n/locales/<lang>.json`. */
|
|
4
|
+
export declare const LOCALES_DIR = "src/i18n/locales";
|
|
5
|
+
export interface SiteRoute {
|
|
6
|
+
/** Normalised, `/priser`; never `/`. */
|
|
7
|
+
path: string;
|
|
8
|
+
/** From the manifest; a hand route has none and takes the site's title. */
|
|
9
|
+
titleKey?: string;
|
|
10
|
+
/** Listed in the sitemap: the manifest's pages, not hand routes. */
|
|
11
|
+
listed: boolean;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* The literal paths of `<Route path="/…">` in a piece of source: absolute,
|
|
15
|
+
* without parameters or wildcards, not the editor's `/setto`, and not a
|
|
16
|
+
* route whose element is a `<Navigate>` (a redirect is not a page).
|
|
17
|
+
*/
|
|
18
|
+
export declare function routesInSource(source: string): string[];
|
|
19
|
+
/** Every hand-written route in the site's `src/`. */
|
|
20
|
+
export declare function codeRoutes(root: string): string[];
|
|
21
|
+
/**
|
|
22
|
+
* The routes that get a file: the manifest's visible pages, then hand routes
|
|
23
|
+
* it does not list. A page the manifest hides is not served to a visitor
|
|
24
|
+
* (they get the site's «not found»), so it gets nothing — even if code
|
|
25
|
+
* routes it too. `skip` holds paths something else writes (the blog).
|
|
26
|
+
*/
|
|
27
|
+
export declare function siteRoutes(manifest: PagesManifest, handRoutes: readonly string[], skip?: readonly string[]): SiteRoute[];
|
|
28
|
+
export type LocaleBundles = Record<string, Record<string, unknown> | null>;
|
|
29
|
+
/** Each language's bundle as the site's i18n registers it: missing, unreadable or empty is `null`. */
|
|
30
|
+
export declare function readLocales(root: string, languages: readonly string[], dir?: string): LocaleBundles;
|
|
31
|
+
/** A string at `pages.priser` (nested, as i18next reads it), or the flat key. */
|
|
32
|
+
export declare function lookup(bundle: Record<string, unknown> | null | undefined, key: string): string | undefined;
|
|
33
|
+
export interface PageHeadsInput {
|
|
34
|
+
routes: readonly SiteRoute[];
|
|
35
|
+
/** The site's languages, the primary first. */
|
|
36
|
+
languages: readonly string[];
|
|
37
|
+
locales: LocaleBundles;
|
|
38
|
+
siteUrl: string | null;
|
|
39
|
+
/** The front page's head (`frontPageHead`): the defaults every page starts from. */
|
|
40
|
+
front: HeadMeta;
|
|
41
|
+
/** The front page's `titleKey`, for its versions in other languages. */
|
|
42
|
+
frontTitleKey?: string;
|
|
43
|
+
}
|
|
44
|
+
export interface PageHead {
|
|
45
|
+
/** Under `dist/`: `priser/index.html`, `en/priser/index.html`, `en/index.html`. */
|
|
46
|
+
file: string;
|
|
47
|
+
meta: HeadMeta;
|
|
48
|
+
/** Absolute, for the sitemap; only a manifest page that is its own (not another language's duplicate). */
|
|
49
|
+
sitemap?: string;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* The head of every page the site has, in every language it speaks.
|
|
53
|
+
*
|
|
54
|
+
* A language counts when its bundle has something in it: the sites' i18n
|
|
55
|
+
* skips an empty one (`en.json` has shipped as `{}`), so `/en/priser` would
|
|
56
|
+
* show the Norwegian page and is its duplicate — its canonical is then the
|
|
57
|
+
* Norwegian address and it has no `hreflang` of its own. A language that
|
|
58
|
+
* counts gets its own canonical and `hreflang` naming every version and
|
|
59
|
+
* `x-default` (the primary), as the provider names them in the browser.
|
|
60
|
+
*
|
|
61
|
+
* Title: `pageTitle(<titleKey in this language, else the primary's>,
|
|
62
|
+
* <front page's title>)`, as `SettoPage` sets it. Description: the key
|
|
63
|
+
* beside the title (`pages.prisar.description`), else the front page's.
|
|
64
|
+
*
|
|
65
|
+
* The primary front page is `index.html`, written by the front-page step;
|
|
66
|
+
* this adds its versions in other languages (`/en`).
|
|
67
|
+
*/
|
|
68
|
+
export declare function pageHeads({ routes, languages, locales, siteUrl, front, frontTitleKey }: PageHeadsInput): PageHead[];
|
package/dist/vite/plugin.d.ts
CHANGED
|
@@ -18,6 +18,16 @@ export interface SettoBlogPluginOptions {
|
|
|
18
18
|
* in the manifest wins.
|
|
19
19
|
*/
|
|
20
20
|
blogPath?: string;
|
|
21
|
+
/**
|
|
22
|
+
* Give every route its own `<route>/index.html` (0.50.0): its canonical
|
|
23
|
+
* address, `og:url`, title, description and `hreflang`, from
|
|
24
|
+
* `src/content/pages.json` and the site's language bundles, plus a
|
|
25
|
+
* `sitemap.xml` when the site has none. On by default for a site with a
|
|
26
|
+
* page manifest; a site without one is left alone. `false` turns it off.
|
|
27
|
+
*/
|
|
28
|
+
pages?: boolean;
|
|
29
|
+
/** Where the language bundles are, `<lang>.json`. `src/i18n/locales` by default. */
|
|
30
|
+
localesDir?: string;
|
|
21
31
|
}
|
|
22
32
|
interface LoadedPost {
|
|
23
33
|
key: string;
|
package/dist/vite/static.d.ts
CHANGED
|
@@ -14,6 +14,8 @@
|
|
|
14
14
|
* Pure functions over strings, so they can be tested without a build.
|
|
15
15
|
*/
|
|
16
16
|
import type { BlogFrontmatter } from '../blog/contract.js';
|
|
17
|
+
import { nameFromTitle } from '../pages/head.js';
|
|
18
|
+
export { nameFromTitle };
|
|
17
19
|
export interface HeadMeta {
|
|
18
20
|
title: string;
|
|
19
21
|
description: string;
|
|
@@ -36,6 +38,12 @@ export interface HeadMeta {
|
|
|
36
38
|
hreflang: string;
|
|
37
39
|
href: string;
|
|
38
40
|
}[];
|
|
41
|
+
/**
|
|
42
|
+
* The front page's title, which this page's title is built on: written as
|
|
43
|
+
* `<meta name="setto:title-base">` so `SettoPage` builds the same title in
|
|
44
|
+
* the browser rather than one on top of this one.
|
|
45
|
+
*/
|
|
46
|
+
titleBase?: string;
|
|
39
47
|
}
|
|
40
48
|
export declare const escapeXml: (value: string) => string;
|
|
41
49
|
/**
|
|
@@ -74,8 +82,6 @@ export declare function imageSize(bytes: Uint8Array): {
|
|
|
74
82
|
} | null;
|
|
75
83
|
/** Where the site's share picture lives: `public/og.jpg`, served at `/og.jpg`. */
|
|
76
84
|
export declare const SHARE_IMAGE_PATH = "/og.jpg";
|
|
77
|
-
/** «Nordvik Rør» from «Nordvik Rør – rørlegger i Ålesund». */
|
|
78
|
-
export declare function nameFromTitle(title: string | undefined): string | undefined;
|
|
79
85
|
/**
|
|
80
86
|
* The name a share card shows beside the page title (`og:site_name`).
|
|
81
87
|
*
|
|
@@ -124,7 +130,7 @@ export interface PostHeadInput {
|
|
|
124
130
|
fallbackImage?: string;
|
|
125
131
|
}
|
|
126
132
|
export declare function postHead({ post, blogPath, siteUrl, siteName, fallbackImage }: PostHeadInput): HeadMeta;
|
|
127
|
-
export declare function indexHead({ title, description, blogPath, siteUrl, siteName, locale, image, }: {
|
|
133
|
+
export declare function indexHead({ title, description, blogPath, siteUrl, siteName, locale, image, alternates, }: {
|
|
128
134
|
title: string;
|
|
129
135
|
description: string;
|
|
130
136
|
blogPath: string;
|
|
@@ -133,6 +139,8 @@ export declare function indexHead({ title, description, blogPath, siteUrl, siteN
|
|
|
133
139
|
locale: string;
|
|
134
140
|
/** Absolute; the site's own share picture. */
|
|
135
141
|
image?: string;
|
|
142
|
+
/** The same page in the site's other languages, for `hreflang`. */
|
|
143
|
+
alternates?: HeadMeta['alternates'];
|
|
136
144
|
}): HeadMeta;
|
|
137
145
|
export declare function rssFeed({ posts, blogPath, siteUrl, title, description, locale, }: {
|
|
138
146
|
posts: readonly BlogFrontmatter[];
|
|
@@ -152,3 +160,8 @@ export declare function withSitemapEntries(sitemap: string, entries: readonly {
|
|
|
152
160
|
}[]): string;
|
|
153
161
|
/** The site's own address, from the first `<loc>` of its sitemap. */
|
|
154
162
|
export declare function originFromSitemap(sitemap: string): string | null;
|
|
163
|
+
/** A sitemap of these addresses, for a site that ships none. */
|
|
164
|
+
export declare function newSitemap(entries: readonly {
|
|
165
|
+
loc: string;
|
|
166
|
+
lastmod?: string;
|
|
167
|
+
}[]): string;
|
package/dist/vite.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createHash } from "node:crypto";
|
|
2
|
-
import { existsSync,
|
|
2
|
+
import { readFileSync, existsSync, readdirSync, statSync, mkdirSync, writeFileSync } from "node:fs";
|
|
3
3
|
import path from "node:path";
|
|
4
4
|
import { SETTO_MEDIA_HOST, BLOG_DIR, parseBlogPostPath, validateBlogPost } from "./blog.js";
|
|
5
5
|
function byDateDesc(a, b) {
|
|
@@ -61,6 +61,7 @@ const PAGES_MANIFEST_VERSION = 1;
|
|
|
61
61
|
const PAGES_PATH = "src/content/pages.json";
|
|
62
62
|
const BUILTIN_SECTION_TYPES = ["text", "blog", "blog-latest"];
|
|
63
63
|
const ID = /^[a-z0-9][a-z0-9-]{0,62}$/;
|
|
64
|
+
const POST_SLUG = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
|
64
65
|
const PATH = /^\/(?:[a-z0-9][a-z0-9-]*(?:\/[a-z0-9][a-z0-9-]*)*)?$/;
|
|
65
66
|
function isRecord(value) {
|
|
66
67
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
@@ -177,6 +178,10 @@ function normalisePath(path2) {
|
|
|
177
178
|
const bare = (path2.split(/[?#]/)[0] ?? "").replace(/\/+$/, "");
|
|
178
179
|
return bare === "" ? "/" : bare.toLowerCase();
|
|
179
180
|
}
|
|
181
|
+
function pageForPath(manifest, path2) {
|
|
182
|
+
const wanted = normalisePath(path2);
|
|
183
|
+
return manifest.pages.find((p) => p.path === wanted) ?? (blogSlugForPath(manifest, path2) ? blogPage(manifest).page : null);
|
|
184
|
+
}
|
|
180
185
|
function blogPage(manifest) {
|
|
181
186
|
for (const page of manifest.pages) {
|
|
182
187
|
const section = page.sections.find((s) => s.type === "blog");
|
|
@@ -184,6 +189,35 @@ function blogPage(manifest) {
|
|
|
184
189
|
}
|
|
185
190
|
return null;
|
|
186
191
|
}
|
|
192
|
+
function blogSlugForPath(manifest, path2) {
|
|
193
|
+
const blog = blogPage(manifest);
|
|
194
|
+
if (!blog || blog.page.path === "/") return null;
|
|
195
|
+
const wanted = normalisePath(path2);
|
|
196
|
+
const prefix = `${blog.page.path}/`;
|
|
197
|
+
if (!wanted.startsWith(prefix)) return null;
|
|
198
|
+
const slug = wanted.slice(prefix.length);
|
|
199
|
+
return slug.length <= 80 && POST_SLUG.test(slug) ? slug : null;
|
|
200
|
+
}
|
|
201
|
+
const TITLE_BASE_META = "setto:title-base";
|
|
202
|
+
const isAllCaps = (value) => !/\p{Ll}/u.test(value) && /\p{Lu}{4,}/u.test(value);
|
|
203
|
+
function nameFromTitle(title) {
|
|
204
|
+
if (!title) return void 0;
|
|
205
|
+
const separator = /\s(?:–|—|\||-)\s/.exec(title);
|
|
206
|
+
const name = (separator ? title.slice(0, separator.index) : title).replace(/\s+/g, " ").trim();
|
|
207
|
+
return name && !isAllCaps(name) ? name : void 0;
|
|
208
|
+
}
|
|
209
|
+
function pageTitle(title, base) {
|
|
210
|
+
const own = title?.replace(/\s+/g, " ").trim() ?? "";
|
|
211
|
+
const front = base?.replace(/\s+/g, " ").trim() ?? "";
|
|
212
|
+
if (!own) return front;
|
|
213
|
+
if (!front || own === front) return own;
|
|
214
|
+
const name = nameFromTitle(front);
|
|
215
|
+
if (name && own.toLowerCase().includes(name.toLowerCase())) return own;
|
|
216
|
+
return `${own} · ${front}`;
|
|
217
|
+
}
|
|
218
|
+
function descriptionKeyFor(titleKey) {
|
|
219
|
+
return titleKey.endsWith(".title") ? `${titleKey.slice(0, -".title".length)}.description` : null;
|
|
220
|
+
}
|
|
187
221
|
const escapeAttr = (value) => value.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
|
188
222
|
const escapeXml = escapeAttr;
|
|
189
223
|
const safeJson = (value) => JSON.stringify(value).replace(/</g, "\\u003c").replace(/>/g, "\\u003e").replace(/&/g, "\\u0026");
|
|
@@ -193,15 +227,19 @@ function htmlLang(locale) {
|
|
|
193
227
|
const OG_LOCALE = { no: "nb_NO", nb: "nb_NO", nn: "nn_NO", en: "en_GB", sv: "sv_SE", da: "da_DK", de: "de_DE" };
|
|
194
228
|
function withHead(indexHtml, meta, scope = "page") {
|
|
195
229
|
const comments = [];
|
|
196
|
-
let html = indexHtml.replace(/<!--[\s\S]*?-->/g, (comment) => `<!--setto-comment:${comments.push(comment) - 1}-->`).replace(/<title>[\s\S]*?<\/title>\s*/gi, "").replace(/<meta\s+name=["']description["'][^>]*>\s*/gi, "").replace(/<meta\s+(?:property|name)=["'](?:og|twitter|article):[^"']*["'][^>]*>\s*/gi, "").replace(/<link\s+rel=["']canonical["'][^>]*>\s*/gi, "");
|
|
230
|
+
let html = indexHtml.replace(/<!--[\s\S]*?-->/g, (comment) => `<!--setto-comment:${comments.push(comment) - 1}-->`).replace(/<title>[\s\S]*?<\/title>\s*/gi, "").replace(/<meta\s+name=["']description["'][^>]*>\s*/gi, "").replace(/<meta\s+(?:property|name)=["'](?:og|twitter|article):[^"']*["'][^>]*>\s*/gi, "").replace(/<link\s+rel=["']canonical["'][^>]*>\s*/gi, "").replace(/<meta\s+name=["']setto:title-base["'][^>]*>\s*/gi, "");
|
|
231
|
+
if (scope === "page" || meta.alternates?.length) {
|
|
232
|
+
html = html.replace(/<link\b(?=[^>]*\bhreflang=)[^>]*>\s*/gi, "");
|
|
233
|
+
}
|
|
197
234
|
if (scope === "page") {
|
|
198
|
-
html = html.replace(/<link\s+rel=["']alternate["']\s+type=["']application\/rss\+xml["'][^>]*>\s*/gi, "").replace(/<
|
|
235
|
+
html = html.replace(/<link\s+rel=["']alternate["']\s+type=["']application\/rss\+xml["'][^>]*>\s*/gi, "").replace(/<script\s+type=["']application\/ld\+json["'][^>]*>[\s\S]*?<\/script>\s*/gi, "");
|
|
199
236
|
}
|
|
200
237
|
html = html.replace(/<!--setto-comment:(\d+)-->/g, (_, i) => comments[Number(i)] ?? "");
|
|
201
238
|
const lang = escapeAttr(htmlLang(meta.locale));
|
|
202
239
|
html = /<html\b[^>]*\blang=/i.test(html) ? html.replace(/(<html\b[^>]*\blang=)(["'])[^"']*\2/i, `$1"${lang}"`) : html.replace(/<html\b/i, `<html lang="${lang}"`);
|
|
203
240
|
const tags = [
|
|
204
241
|
`<title>${escapeAttr(meta.title)}</title>`,
|
|
242
|
+
meta.titleBase ? `<meta name="${TITLE_BASE_META}" content="${escapeAttr(meta.titleBase)}" />` : "",
|
|
205
243
|
meta.description ? `<meta name="description" content="${escapeAttr(meta.description)}" />` : "",
|
|
206
244
|
meta.url ? `<link rel="canonical" href="${escapeAttr(meta.url)}" />` : "",
|
|
207
245
|
...(meta.alternates ?? []).map(
|
|
@@ -288,14 +326,7 @@ function imageSize(bytes) {
|
|
|
288
326
|
return null;
|
|
289
327
|
}
|
|
290
328
|
const SHARE_IMAGE_PATH = "/og.jpg";
|
|
291
|
-
const isAllCaps = (value) => !/\p{Ll}/u.test(value) && /\p{Lu}{4,}/u.test(value);
|
|
292
329
|
const hasVariantSuffix = (value) => /\s[-–—]\s/.test(value);
|
|
293
|
-
function nameFromTitle(title) {
|
|
294
|
-
if (!title) return void 0;
|
|
295
|
-
const separator = /\s(?:–|—|\||-)\s/.exec(title);
|
|
296
|
-
const name = (separator ? title.slice(0, separator.index) : title).replace(/\s+/g, " ").trim();
|
|
297
|
-
return name && !isAllCaps(name) ? name : void 0;
|
|
298
|
-
}
|
|
299
330
|
function shareSiteName({
|
|
300
331
|
ogSiteName,
|
|
301
332
|
title,
|
|
@@ -373,10 +404,12 @@ function indexHead({
|
|
|
373
404
|
siteUrl,
|
|
374
405
|
siteName,
|
|
375
406
|
locale,
|
|
376
|
-
image
|
|
407
|
+
image,
|
|
408
|
+
alternates
|
|
377
409
|
}) {
|
|
378
410
|
const url = siteUrl ? absolute(siteUrl, blogPath) : void 0;
|
|
379
411
|
return {
|
|
412
|
+
alternates,
|
|
380
413
|
title: siteName ? `${title} · ${siteName}` : title,
|
|
381
414
|
description,
|
|
382
415
|
url,
|
|
@@ -457,6 +490,138 @@ function originFromSitemap(sitemap) {
|
|
|
457
490
|
return null;
|
|
458
491
|
}
|
|
459
492
|
}
|
|
493
|
+
function newSitemap(entries) {
|
|
494
|
+
return withSitemapEntries(
|
|
495
|
+
'<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n</urlset>\n',
|
|
496
|
+
entries
|
|
497
|
+
);
|
|
498
|
+
}
|
|
499
|
+
const LOCALES_DIR = "src/i18n/locales";
|
|
500
|
+
const SOURCE_FILE = /\.(?:tsx|jsx|ts|js)$/;
|
|
501
|
+
function sourceFiles(dir, out = []) {
|
|
502
|
+
if (!existsSync(dir)) return out;
|
|
503
|
+
for (const name of readdirSync(dir)) {
|
|
504
|
+
if (name === "node_modules" || name.startsWith(".")) continue;
|
|
505
|
+
const file = path.join(dir, name);
|
|
506
|
+
if (statSync(file).isDirectory()) sourceFiles(file, out);
|
|
507
|
+
else if (SOURCE_FILE.test(name) && !/\.(?:test|spec)\.[jt]sx?$/.test(name)) out.push(file);
|
|
508
|
+
}
|
|
509
|
+
return out;
|
|
510
|
+
}
|
|
511
|
+
function routesInSource(source) {
|
|
512
|
+
const found = [];
|
|
513
|
+
const pattern = /<Route\b[^>]*?\bpath\s*=\s*(?:\{\s*)?(["'`])(\/[^"'`]*)\1/g;
|
|
514
|
+
for (let match = pattern.exec(source); match; match = pattern.exec(source)) {
|
|
515
|
+
const raw = match[2];
|
|
516
|
+
if (/[:*?()]/.test(raw)) continue;
|
|
517
|
+
const route = normalisePath(raw);
|
|
518
|
+
if (route === "/" || route === "/setto" || route.startsWith("/setto/")) continue;
|
|
519
|
+
const rest = source.slice(match.index + match[0].length);
|
|
520
|
+
const next = rest.search(/<Route\b/);
|
|
521
|
+
if (/<Navigate\b/.test(next === -1 ? rest.slice(0, 400) : rest.slice(0, next))) continue;
|
|
522
|
+
found.push(route);
|
|
523
|
+
}
|
|
524
|
+
return found;
|
|
525
|
+
}
|
|
526
|
+
function codeRoutes(root) {
|
|
527
|
+
const routes = /* @__PURE__ */ new Set();
|
|
528
|
+
for (const file of sourceFiles(path.join(root, "src"))) {
|
|
529
|
+
const source = readFileSync(file, "utf8");
|
|
530
|
+
if (!source.includes("<Route")) continue;
|
|
531
|
+
for (const route of routesInSource(source)) routes.add(route);
|
|
532
|
+
}
|
|
533
|
+
return [...routes].sort();
|
|
534
|
+
}
|
|
535
|
+
function siteRoutes(manifest, handRoutes, skip = []) {
|
|
536
|
+
const skipped = new Set(skip.map(normalisePath));
|
|
537
|
+
const known = /* @__PURE__ */ new Set();
|
|
538
|
+
const routes = [];
|
|
539
|
+
for (const page of manifest.pages) {
|
|
540
|
+
const route = normalisePath(page.path);
|
|
541
|
+
known.add(route);
|
|
542
|
+
if (page.hidden || route === "/" || skipped.has(route)) continue;
|
|
543
|
+
routes.push({ path: route, titleKey: page.titleKey, listed: true });
|
|
544
|
+
}
|
|
545
|
+
for (const route of handRoutes) {
|
|
546
|
+
if (known.has(route) || skipped.has(route)) continue;
|
|
547
|
+
known.add(route);
|
|
548
|
+
routes.push({ path: route, listed: false });
|
|
549
|
+
}
|
|
550
|
+
return routes;
|
|
551
|
+
}
|
|
552
|
+
function readLocales(root, languages, dir = LOCALES_DIR) {
|
|
553
|
+
const bundles = {};
|
|
554
|
+
for (const lang of languages) {
|
|
555
|
+
try {
|
|
556
|
+
const parsed = JSON.parse(readFileSync(path.join(root, dir, `${lang}.json`), "utf8"));
|
|
557
|
+
bundles[lang] = parsed && typeof parsed === "object" && !Array.isArray(parsed) && Object.keys(parsed).length > 0 ? parsed : null;
|
|
558
|
+
} catch {
|
|
559
|
+
bundles[lang] = null;
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
return bundles;
|
|
563
|
+
}
|
|
564
|
+
function lookup(bundle, key) {
|
|
565
|
+
if (!bundle) return void 0;
|
|
566
|
+
const flat = bundle[key];
|
|
567
|
+
if (typeof flat === "string") return flat.trim() || void 0;
|
|
568
|
+
let node = bundle;
|
|
569
|
+
for (const part of key.split(".")) {
|
|
570
|
+
if (!node || typeof node !== "object") return void 0;
|
|
571
|
+
node = node[part];
|
|
572
|
+
}
|
|
573
|
+
return typeof node === "string" ? node.trim() || void 0 : void 0;
|
|
574
|
+
}
|
|
575
|
+
function pageHeads({ routes, languages, locales, siteUrl, front, frontTitleKey }) {
|
|
576
|
+
const primary = languages[0] ?? "no";
|
|
577
|
+
const counts = (lang) => lang === primary || Boolean(locales[lang]) || !locales[primary];
|
|
578
|
+
const spoken = languages.filter(counts);
|
|
579
|
+
const prefix = (lang) => lang === primary ? "" : `/${lang}`;
|
|
580
|
+
const addressIn = (route, lang) => route === "/" ? prefix(lang) || "/" : `${prefix(lang)}${route}`;
|
|
581
|
+
const text = (key, lang) => key ? lookup(locales[lang], key) ?? lookup(locales[primary], key) : void 0;
|
|
582
|
+
const heads = [];
|
|
583
|
+
const pages = [
|
|
584
|
+
{ route: "/", titleKey: frontTitleKey, listed: true },
|
|
585
|
+
...routes.map((r) => ({ route: r.path, titleKey: r.titleKey, listed: r.listed }))
|
|
586
|
+
];
|
|
587
|
+
for (const lang of languages) {
|
|
588
|
+
const own = counts(lang);
|
|
589
|
+
const shown = own ? lang : primary;
|
|
590
|
+
for (const page of pages) {
|
|
591
|
+
if (page.route === "/" && lang === primary) continue;
|
|
592
|
+
const titleText = text(page.titleKey, shown);
|
|
593
|
+
const descriptionText = text(page.titleKey ? descriptionKeyFor(page.titleKey) : null, shown);
|
|
594
|
+
let title;
|
|
595
|
+
if (page.route === "/") {
|
|
596
|
+
title = titleText && pageTitle(titleText, front.title) === titleText ? titleText : front.title;
|
|
597
|
+
} else {
|
|
598
|
+
title = pageTitle(titleText, front.title);
|
|
599
|
+
}
|
|
600
|
+
const canonicalPath = addressIn(page.route, own ? lang : primary);
|
|
601
|
+
const url = siteUrl ? absolute(siteUrl, canonicalPath) : void 0;
|
|
602
|
+
const alternates = siteUrl && own && spoken.length > 1 ? [
|
|
603
|
+
...spoken.map((l) => ({ hreflang: l, href: absolute(siteUrl, addressIn(page.route, l)) })),
|
|
604
|
+
{ hreflang: "x-default", href: absolute(siteUrl, addressIn(page.route, primary)) }
|
|
605
|
+
] : void 0;
|
|
606
|
+
heads.push({
|
|
607
|
+
file: `${addressIn(page.route, lang).slice(1)}/index.html`,
|
|
608
|
+
meta: {
|
|
609
|
+
...front,
|
|
610
|
+
title,
|
|
611
|
+
description: descriptionText ?? front.description,
|
|
612
|
+
url,
|
|
613
|
+
imageAlt: front.image ? front.imageAlt === front.title ? title : front.imageAlt : void 0,
|
|
614
|
+
locale: shown,
|
|
615
|
+
alternates,
|
|
616
|
+
titleBase: front.title,
|
|
617
|
+
type: "website"
|
|
618
|
+
},
|
|
619
|
+
sitemap: page.listed && own && url ? url : void 0
|
|
620
|
+
});
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
return heads;
|
|
624
|
+
}
|
|
460
625
|
const INDEX_ID = "virtual:setto-blog";
|
|
461
626
|
const POST_PREFIX = "virtual:setto-blog/post/";
|
|
462
627
|
const RESOLVED = "\0";
|
|
@@ -618,77 +783,113 @@ function writeStaticFiles(root, outDir, loaded, options = {}) {
|
|
|
618
783
|
}
|
|
619
784
|
const manifestRaw = readJson(path.join(root, PAGES_PATH));
|
|
620
785
|
const parsed = manifestRaw ? parseManifest(manifestRaw) : null;
|
|
621
|
-
const
|
|
622
|
-
|
|
623
|
-
const
|
|
624
|
-
|
|
625
|
-
const
|
|
626
|
-
loaded.map((p) => p.frontmatter),
|
|
627
|
-
language,
|
|
628
|
-
language
|
|
629
|
-
);
|
|
630
|
-
const all = loaded.map((p) => p.frontmatter);
|
|
786
|
+
const manifest = parsed?.ok ? parsed.manifest : null;
|
|
787
|
+
const blog = manifest ? blogPage(manifest) : null;
|
|
788
|
+
const blogShown = blog ? !(blog.page.hidden || blog.section.hidden) : true;
|
|
789
|
+
const blogPathRaw = blog?.page.path ?? (options.blogPath ? normalisePath(options.blogPath) : null);
|
|
790
|
+
const blogPath = blogShown && blogPathRaw && blogPathRaw !== "/" ? blogPathRaw : null;
|
|
631
791
|
const prefixOf = (lang) => lang === language ? "" : `/${lang}`;
|
|
632
|
-
const
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
const
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
blogPath: langBlogPath,
|
|
645
|
-
siteUrl,
|
|
646
|
-
siteName,
|
|
647
|
-
locale: lang,
|
|
648
|
-
image: shareImageUrl
|
|
649
|
-
})
|
|
650
|
-
)
|
|
651
|
-
);
|
|
652
|
-
for (const post of postsForLanguage(all, lang, language)) {
|
|
653
|
-
const meta = postHead({ post, blogPath: langBlogPath, siteUrl, siteName, fallbackImage: shareImageUrl });
|
|
654
|
-
const versions = versionsOf(post.slug);
|
|
655
|
-
if (post.locale !== lang && siteUrl) meta.url = absolute(siteUrl, `${prefixOf(post.locale)}${blogPath}/${post.slug}`);
|
|
656
|
-
if (siteUrl && versions.length > 1) {
|
|
657
|
-
meta.alternates = [
|
|
658
|
-
...versions.map((v) => ({ hreflang: v.locale, href: absolute(siteUrl, `${prefixOf(v.locale)}${blogPath}/${v.slug}`) })),
|
|
659
|
-
{ hreflang: "x-default", href: absolute(siteUrl, `${blogPath}/${post.slug}`) }
|
|
660
|
-
];
|
|
661
|
-
}
|
|
662
|
-
write(path.join(dir, post.slug, "index.html"), withHead(indexHtml, meta));
|
|
663
|
-
}
|
|
792
|
+
const sitemapEntries = [];
|
|
793
|
+
const pagesOn = Boolean(manifest && front && options.pages !== false);
|
|
794
|
+
if (manifest && front && pagesOn) {
|
|
795
|
+
const heads = pageHeads({
|
|
796
|
+
routes: siteRoutes(manifest, codeRoutes(root), blogPath ? [blogPath] : []),
|
|
797
|
+
languages,
|
|
798
|
+
locales: readLocales(root, languages, options.localesDir ?? LOCALES_DIR),
|
|
799
|
+
siteUrl,
|
|
800
|
+
front,
|
|
801
|
+
frontTitleKey: pageForPath(manifest, "/")?.titleKey
|
|
802
|
+
});
|
|
803
|
+
for (const head of heads) write(head.file, withHead(indexHtml, head.meta, "social"));
|
|
664
804
|
if (siteUrl) {
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
path.join(dir, "rss.xml"),
|
|
668
|
-
rssFeed({
|
|
669
|
-
posts: postsForLanguage(all, lang, language).slice(0, 50),
|
|
670
|
-
blogPath: langBlogPath,
|
|
671
|
-
siteUrl,
|
|
672
|
-
title: feedTitle,
|
|
673
|
-
description: siteName ? `${langStrings.latest} · ${siteName}` : langStrings.latest,
|
|
674
|
-
locale: lang
|
|
675
|
-
})
|
|
676
|
-
);
|
|
805
|
+
sitemapEntries.push({ loc: `${siteUrl}/` });
|
|
806
|
+
for (const head of heads) if (head.sitemap) sitemapEntries.push({ loc: head.sitemap });
|
|
677
807
|
}
|
|
678
808
|
}
|
|
809
|
+
if (blogPath) writeBlog();
|
|
679
810
|
if (siteUrl) {
|
|
680
811
|
if (sitemap) {
|
|
681
|
-
const
|
|
812
|
+
const next = withSitemapEntries(sitemap, sitemapEntries);
|
|
813
|
+
if (next !== sitemap) write("sitemap.xml", next);
|
|
814
|
+
} else if (pagesOn && sitemapEntries.length) {
|
|
815
|
+
write("sitemap.xml", newSitemap(sitemapEntries));
|
|
816
|
+
const robotsFile = path.join(outDir, "robots.txt");
|
|
817
|
+
if (existsSync(robotsFile)) {
|
|
818
|
+
const robots = readFileSync(robotsFile, "utf8");
|
|
819
|
+
if (!/^\s*sitemap\s*:/im.test(robots)) {
|
|
820
|
+
write("robots.txt", `${robots.replace(/\s*$/, "")}
|
|
821
|
+
Sitemap: ${siteUrl}/sitemap.xml
|
|
822
|
+
`);
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
}
|
|
826
|
+
}
|
|
827
|
+
return { written, warnings, siteUrl };
|
|
828
|
+
function writeBlog() {
|
|
829
|
+
if (!blogPath) return;
|
|
830
|
+
const posts = postsForLanguage(
|
|
831
|
+
loaded.map((p) => p.frontmatter),
|
|
832
|
+
language,
|
|
833
|
+
language
|
|
834
|
+
);
|
|
835
|
+
const all = loaded.map((p) => p.frontmatter);
|
|
836
|
+
const versionsOf = (slug) => all.filter((p) => p.slug === slug);
|
|
837
|
+
for (const lang of languages) {
|
|
838
|
+
const langBlogPath = `${prefixOf(lang)}${blogPath}`;
|
|
839
|
+
const dir = langBlogPath.replace(/^\//, "");
|
|
840
|
+
const langStrings = blogStrings(lang);
|
|
841
|
+
const index = indexHead({
|
|
842
|
+
title: langStrings.blog,
|
|
843
|
+
description: siteName ? `${langStrings.latest} · ${siteName}` : langStrings.latest,
|
|
844
|
+
blogPath: langBlogPath,
|
|
845
|
+
siteUrl,
|
|
846
|
+
siteName,
|
|
847
|
+
locale: lang,
|
|
848
|
+
image: shareImageUrl,
|
|
849
|
+
alternates: siteUrl && languages.length > 1 ? [
|
|
850
|
+
...languages.map((l) => ({ hreflang: l, href: absolute(siteUrl, `${prefixOf(l)}${blogPath}`) })),
|
|
851
|
+
{ hreflang: "x-default", href: absolute(siteUrl, blogPath) }
|
|
852
|
+
] : void 0
|
|
853
|
+
});
|
|
854
|
+
index.titleBase = front?.title;
|
|
855
|
+
write(path.join(dir, "index.html"), withHead(indexHtml, index));
|
|
856
|
+
for (const post of postsForLanguage(all, lang, language)) {
|
|
857
|
+
const meta = postHead({ post, blogPath: langBlogPath, siteUrl, siteName, fallbackImage: shareImageUrl });
|
|
858
|
+
const versions = versionsOf(post.slug);
|
|
859
|
+
if (post.locale !== lang && siteUrl) meta.url = absolute(siteUrl, `${prefixOf(post.locale)}${blogPath}/${post.slug}`);
|
|
860
|
+
if (siteUrl && (versions.length > 1 || languages.length > 1)) {
|
|
861
|
+
const primaryVersion = versions.find((v) => v.locale === language) ?? versions[0] ?? post;
|
|
862
|
+
meta.alternates = [
|
|
863
|
+
...versions.map((v) => ({ hreflang: v.locale, href: absolute(siteUrl, `${prefixOf(v.locale)}${blogPath}/${v.slug}`) })),
|
|
864
|
+
{ hreflang: "x-default", href: absolute(siteUrl, `${prefixOf(primaryVersion.locale)}${blogPath}/${post.slug}`) }
|
|
865
|
+
];
|
|
866
|
+
}
|
|
867
|
+
meta.titleBase = front?.title;
|
|
868
|
+
write(path.join(dir, post.slug, "index.html"), withHead(indexHtml, meta));
|
|
869
|
+
}
|
|
870
|
+
if (siteUrl) {
|
|
871
|
+
const feedTitle = siteName ? `${langStrings.blog} · ${siteName}` : langStrings.blog;
|
|
872
|
+
write(
|
|
873
|
+
path.join(dir, "rss.xml"),
|
|
874
|
+
rssFeed({
|
|
875
|
+
posts: postsForLanguage(all, lang, language).slice(0, 50),
|
|
876
|
+
blogPath: langBlogPath,
|
|
877
|
+
siteUrl,
|
|
878
|
+
title: feedTitle,
|
|
879
|
+
description: siteName ? `${langStrings.latest} · ${siteName}` : langStrings.latest,
|
|
880
|
+
locale: lang
|
|
881
|
+
})
|
|
882
|
+
);
|
|
883
|
+
}
|
|
884
|
+
}
|
|
885
|
+
if (siteUrl) {
|
|
886
|
+
sitemapEntries.push(
|
|
682
887
|
{ loc: absolute(siteUrl, blogPath), lastmod: posts[0]?.date },
|
|
683
888
|
...posts.map((post) => ({ loc: absolute(siteUrl, `${blogPath}/${post.slug}`), lastmod: post.date })),
|
|
684
|
-
// A version in another language is a page of its own.
|
|
685
889
|
...all.filter((post) => post.locale !== language && languages.includes(post.locale)).map((post) => ({ loc: absolute(siteUrl, `${prefixOf(post.locale)}${blogPath}/${post.slug}`), lastmod: post.date }))
|
|
686
|
-
|
|
687
|
-
const next = withSitemapEntries(sitemap, entries);
|
|
688
|
-
if (next !== sitemap) write("sitemap.xml", next);
|
|
890
|
+
);
|
|
689
891
|
}
|
|
690
892
|
}
|
|
691
|
-
return { written, warnings, siteUrl };
|
|
692
893
|
}
|
|
693
894
|
function resolveSiteUrl(options, configured, sitemap) {
|
|
694
895
|
const vercel = process.env.VERCEL_PROJECT_PRODUCTION_URL?.trim();
|