@cloudhal/astro-renderer 0.0.0-stage → 0.1.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/LICENSE +202 -0
- package/README.md +36 -2
- package/dist/src/ambient-client.d.ts +17 -0
- package/dist/src/ambient-client.js +55 -0
- package/dist/src/analytics-client.d.ts +17 -0
- package/dist/src/analytics-client.js +96 -0
- package/dist/src/analytics-middleware.d.ts +6 -0
- package/dist/src/analytics-middleware.js +43 -0
- package/dist/src/analytics-node.d.ts +46 -0
- package/dist/src/analytics-node.js +134 -0
- package/dist/src/analytics.d.ts +117 -0
- package/dist/src/analytics.js +372 -0
- package/dist/src/archive.d.ts +106 -0
- package/dist/src/archive.js +207 -0
- package/dist/src/astro-env.d.ts +21 -0
- package/dist/src/block-html.d.ts +14 -0
- package/dist/src/block-html.js +103 -0
- package/dist/src/blocks.d.ts +24 -0
- package/dist/src/blocks.js +66 -0
- package/dist/src/cache-tags.d.ts +25 -0
- package/dist/src/cache-tags.js +59 -0
- package/dist/src/carousel-client.d.ts +28 -0
- package/dist/src/carousel-client.js +67 -0
- package/dist/src/chart.d.ts +34 -0
- package/dist/src/chart.js +182 -0
- package/dist/src/chrome-client.d.ts +27 -0
- package/dist/src/chrome-client.js +64 -0
- package/dist/src/cloudflare-entry.d.ts +87 -0
- package/dist/src/cloudflare-entry.js +548 -0
- package/dist/src/comments-client.d.ts +11 -0
- package/dist/src/comments-client.js +58 -0
- package/dist/src/comments-html.d.ts +42 -0
- package/dist/src/comments-html.js +303 -0
- package/dist/src/comments-middleware.d.ts +6 -0
- package/dist/src/comments-middleware.js +38 -0
- package/dist/src/comments-node.d.ts +42 -0
- package/dist/src/comments-node.js +112 -0
- package/dist/src/comments-view.d.ts +68 -0
- package/dist/src/comments-view.js +174 -0
- package/dist/src/comments.d.ts +66 -0
- package/dist/src/comments.js +258 -0
- package/dist/src/components/Block.astro +39 -0
- package/dist/src/components/Body.astro +43 -0
- package/dist/src/components/Comments.astro +19 -0
- package/dist/src/components/ContentList.astro +31 -0
- package/dist/src/components/EntryArticle.astro +31 -0
- package/dist/src/components/Hero.astro +36 -0
- package/dist/src/components/Image.astro +36 -0
- package/dist/src/components/RichText.astro +19 -0
- package/dist/src/components/Sections.astro +66 -0
- package/dist/src/components/SeoHead.astro +22 -0
- package/dist/src/components/Surface.astro +36 -0
- package/dist/src/components/SurfaceNode.astro +82 -0
- package/dist/src/components/index.d.ts +12 -0
- package/dist/src/components/index.js +12 -0
- package/dist/src/csp.d.ts +20 -0
- package/dist/src/csp.js +28 -0
- package/dist/src/embed-client.d.ts +38 -0
- package/dist/src/embed-client.js +98 -0
- package/dist/src/embed-middleware.d.ts +9 -0
- package/dist/src/embed-middleware.js +49 -0
- package/dist/src/feeds.d.ts +72 -0
- package/dist/src/feeds.js +246 -0
- package/dist/src/gates.d.ts +25 -0
- package/dist/src/gates.js +63 -0
- package/dist/src/index.d.ts +17 -0
- package/dist/src/index.js +17 -0
- package/dist/src/integration.d.ts +55 -0
- package/dist/src/integration.js +244 -0
- package/dist/src/kit.d.ts +48 -0
- package/dist/src/kit.js +83 -0
- package/dist/src/locale-feeds.d.ts +29 -0
- package/dist/src/locale-feeds.js +152 -0
- package/dist/src/locale-middleware.d.ts +5 -0
- package/dist/src/locale-middleware.js +26 -0
- package/dist/src/locales.d.ts +87 -0
- package/dist/src/locales.js +336 -0
- package/dist/src/markdown.d.ts +83 -0
- package/dist/src/markdown.js +533 -0
- package/dist/src/media-middleware.d.ts +5 -0
- package/dist/src/media-middleware.js +18 -0
- package/dist/src/media-route.d.ts +47 -0
- package/dist/src/media-route.js +238 -0
- package/dist/src/media.d.ts +109 -0
- package/dist/src/media.js +298 -0
- package/dist/src/motion-client.d.ts +39 -0
- package/dist/src/motion-client.js +115 -0
- package/dist/src/navigation.d.ts +127 -0
- package/dist/src/navigation.js +382 -0
- package/dist/src/not-found.d.ts +16 -0
- package/dist/src/not-found.js +41 -0
- package/dist/src/overlay-client.d.ts +20 -0
- package/dist/src/overlay-client.js +49 -0
- package/dist/src/prefs-client.d.ts +14 -0
- package/dist/src/prefs-client.js +18 -0
- package/dist/src/preview.d.ts +70 -0
- package/dist/src/preview.js +551 -0
- package/dist/src/resolver.d.ts +71 -0
- package/dist/src/resolver.js +206 -0
- package/dist/src/routing.d.ts +108 -0
- package/dist/src/routing.js +285 -0
- package/dist/src/runtime-workerd.d.ts +87 -0
- package/dist/src/runtime-workerd.js +275 -0
- package/dist/src/runtime.d.ts +59 -0
- package/dist/src/runtime.js +153 -0
- package/dist/src/safe-urls.d.ts +13 -0
- package/dist/src/safe-urls.js +35 -0
- package/dist/src/sanitize.d.ts +7 -0
- package/dist/src/sanitize.js +217 -0
- package/dist/src/search-client.d.ts +29 -0
- package/dist/src/search-client.js +92 -0
- package/dist/src/search-limit.d.ts +36 -0
- package/dist/src/search-limit.js +66 -0
- package/dist/src/search.d.ts +64 -0
- package/dist/src/search.js +155 -0
- package/dist/src/seo.d.ts +85 -0
- package/dist/src/seo.js +468 -0
- package/dist/src/site-dispatch.d.ts +37 -0
- package/dist/src/site-dispatch.js +86 -0
- package/dist/src/site-hosts.d.ts +35 -0
- package/dist/src/site-hosts.js +110 -0
- package/dist/src/source.d.ts +227 -0
- package/dist/src/source.js +794 -0
- package/dist/src/static-gates.d.ts +3 -0
- package/dist/src/static-gates.js +28 -0
- package/dist/src/static-headers.d.ts +30 -0
- package/dist/src/static-headers.js +140 -0
- package/dist/src/static-media.d.ts +4 -0
- package/dist/src/static-media.js +65 -0
- package/dist/src/static-redirects.d.ts +4 -0
- package/dist/src/static-redirects.js +40 -0
- package/dist/src/surface-client.d.ts +34 -0
- package/dist/src/surface-client.js +146 -0
- package/dist/src/surfaces/html.d.ts +35 -0
- package/dist/src/surfaces/html.js +184 -0
- package/dist/src/surfaces/markup.d.ts +57 -0
- package/dist/src/surfaces/markup.js +135 -0
- package/dist/src/surfaces/motion.d.ts +33 -0
- package/dist/src/surfaces/motion.js +132 -0
- package/dist/src/surfaces/primitives/carousel.d.ts +2 -0
- package/dist/src/surfaces/primitives/carousel.js +95 -0
- package/dist/src/surfaces/primitives/content.d.ts +13 -0
- package/dist/src/surfaces/primitives/content.js +159 -0
- package/dist/src/surfaces/primitives/data.d.ts +5 -0
- package/dist/src/surfaces/primitives/data.js +80 -0
- package/dist/src/surfaces/primitives/index.d.ts +54 -0
- package/dist/src/surfaces/primitives/index.js +106 -0
- package/dist/src/surfaces/primitives/inputs.d.ts +8 -0
- package/dist/src/surfaces/primitives/inputs.js +68 -0
- package/dist/src/surfaces/primitives/layout.d.ts +14 -0
- package/dist/src/surfaces/primitives/layout.js +61 -0
- package/dist/src/surfaces/primitives/motion.d.ts +9 -0
- package/dist/src/surfaces/primitives/motion.js +45 -0
- package/dist/src/surfaces/primitives/overlays.d.ts +6 -0
- package/dist/src/surfaces/primitives/overlays.js +79 -0
- package/dist/src/surfaces/tree.d.ts +107 -0
- package/dist/src/surfaces/tree.js +321 -0
- package/dist/src/surfaces.d.ts +14 -0
- package/dist/src/surfaces.js +33 -0
- package/dist/src/taxonomy.d.ts +86 -0
- package/dist/src/taxonomy.js +268 -0
- package/dist/src/theme-settings.d.ts +24 -0
- package/dist/src/theme-settings.js +111 -0
- package/package.json +219 -3
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The theme kit (RFC-0018 §3): what every theme's templates read about an article, computed once
|
|
3
|
+
* by Cloudhal so themes only draw it: the outline (table of contents, reading time) and the
|
|
4
|
+
* corrections (RFC-0019 §2). Worker-safe.
|
|
5
|
+
*/
|
|
6
|
+
import type { ContentEntry, Correction } from "@cloudhal/core-contracts";
|
|
7
|
+
/** One heading of an article's table of contents; `id` is the anchor the rendered body carries. */
|
|
8
|
+
export interface TocItem {
|
|
9
|
+
id: string;
|
|
10
|
+
text: string;
|
|
11
|
+
depth: 2 | 3;
|
|
12
|
+
}
|
|
13
|
+
/** An article's outline: its `h2`/`h3` headings and an estimated reading time. */
|
|
14
|
+
export interface ArticleOutline {
|
|
15
|
+
toc: TocItem[];
|
|
16
|
+
readingMinutes: number;
|
|
17
|
+
}
|
|
18
|
+
/** Words per minute when a theme sets none (RFC-0018 §1 `readingSpeed`). */
|
|
19
|
+
export declare const READING_SPEED = 230;
|
|
20
|
+
/**
|
|
21
|
+
* Headings of HTML rendered by `renderMarkdown`/`renderBody` (their ids come from `headingSlug`).
|
|
22
|
+
* HTML bodies keep no ids through the sanitizer, so they have no table of contents.
|
|
23
|
+
*/
|
|
24
|
+
export declare function tableOfContents(html: string): TocItem[];
|
|
25
|
+
/**
|
|
26
|
+
* Minutes to read rendered HTML: words at `wordsPerMinute`, plus 12 seconds for the first image,
|
|
27
|
+
* one second less for each next one, at least 3 (Medium's rule). Never less than one minute.
|
|
28
|
+
*/
|
|
29
|
+
export declare function readingMinutes(html: string, wordsPerMinute?: number): number;
|
|
30
|
+
/**
|
|
31
|
+
* An entry's outline from its body. The body is rendered the same way the article renders it, so
|
|
32
|
+
* the ids match the page's headings; media is not needed for either.
|
|
33
|
+
*/
|
|
34
|
+
export declare function articleOutline(entry: Pick<ContentEntry, "body">, wordsPerMinute?: number): ArticleOutline;
|
|
35
|
+
/** One correction as an article shows it (RFC-0019 §2): the note rendered as inline markdown. */
|
|
36
|
+
export interface ArticleCorrection {
|
|
37
|
+
id: string;
|
|
38
|
+
kind: Correction["kind"];
|
|
39
|
+
at: string;
|
|
40
|
+
html: string;
|
|
41
|
+
}
|
|
42
|
+
/** An article's corrections: newest first, and the time of the latest (its "Updated" date). */
|
|
43
|
+
export interface ArticleCorrections {
|
|
44
|
+
items: ArticleCorrection[];
|
|
45
|
+
latest?: string;
|
|
46
|
+
}
|
|
47
|
+
/** The corrections an article shows, newest first; notes go through the safe inline renderer. */
|
|
48
|
+
export declare function articleCorrections(entry: Pick<ContentEntry, "corrections">): ArticleCorrections;
|
package/dist/src/kit.js
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The theme kit (RFC-0018 §3): what every theme's templates read about an article, computed once
|
|
3
|
+
* by Cloudhal so themes only draw it: the outline (table of contents, reading time) and the
|
|
4
|
+
* corrections (RFC-0019 §2). Worker-safe.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { htmlText, renderBody, renderInlineMarkdown } from "./markdown.js";
|
|
8
|
+
|
|
9
|
+
/** One heading of an article's table of contents; `id` is the anchor the rendered body carries. */
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
/** An article's outline: its `h2`/`h3` headings and an estimated reading time. */
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
/** Words per minute when a theme sets none (RFC-0018 §1 `readingSpeed`). */
|
|
23
|
+
export const READING_SPEED = 230;
|
|
24
|
+
|
|
25
|
+
const HEADING = /<h([23]) id="([^"]+)">([\s\S]*?)<\/h\1>/g;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Headings of HTML rendered by `renderMarkdown`/`renderBody` (their ids come from `headingSlug`).
|
|
29
|
+
* HTML bodies keep no ids through the sanitizer, so they have no table of contents.
|
|
30
|
+
*/
|
|
31
|
+
export function tableOfContents(html ) {
|
|
32
|
+
return [...html.matchAll(HEADING)].map((m) => ({
|
|
33
|
+
id: m[2] ,
|
|
34
|
+
text: htmlText(m[3] ),
|
|
35
|
+
depth: Number(m[1]) ,
|
|
36
|
+
}));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Minutes to read rendered HTML: words at `wordsPerMinute`, plus 12 seconds for the first image,
|
|
41
|
+
* one second less for each next one, at least 3 (Medium's rule). Never less than one minute.
|
|
42
|
+
*/
|
|
43
|
+
export function readingMinutes(html , wordsPerMinute = READING_SPEED) {
|
|
44
|
+
const words = htmlText(html).match(/\S+/g)?.length ?? 0;
|
|
45
|
+
const images = html.match(/<img\b/g)?.length ?? 0;
|
|
46
|
+
let seconds = (words / Math.max(1, wordsPerMinute)) * 60;
|
|
47
|
+
for (let i = 0; i < images; i++) seconds += Math.max(12 - i, 3);
|
|
48
|
+
return Math.max(1, Math.round(seconds / 60));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* An entry's outline from its body. The body is rendered the same way the article renders it, so
|
|
53
|
+
* the ids match the page's headings; media is not needed for either.
|
|
54
|
+
*/
|
|
55
|
+
export function articleOutline(
|
|
56
|
+
entry ,
|
|
57
|
+
wordsPerMinute = READING_SPEED,
|
|
58
|
+
) {
|
|
59
|
+
const html = renderBody(entry.body);
|
|
60
|
+
return { toc: tableOfContents(html), readingMinutes: readingMinutes(html, wordsPerMinute) };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** One correction as an article shows it (RFC-0019 §2): the note rendered as inline markdown. */
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
/** An article's corrections: newest first, and the time of the latest (its "Updated" date). */
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
/** The corrections an article shows, newest first; notes go through the safe inline renderer. */
|
|
78
|
+
export function articleCorrections(entry ) {
|
|
79
|
+
const items = (entry.corrections ?? [])
|
|
80
|
+
.map((c) => ({ id: c.id, kind: c.kind, at: c.at, html: renderInlineMarkdown(c.note) }))
|
|
81
|
+
.sort((a, b) => Date.parse(b.at) - Date.parse(a.at));
|
|
82
|
+
return items[0] ? { items, latest: items[0].at } : { items };
|
|
83
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RFC-0033 §4 (phase B2): the machine-readable views of every locale a host serves — the sitemap
|
|
3
|
+
* with `xhtml:link` alternates per URL, a feed per locale and `llms.txt` per locale. Built on the
|
|
4
|
+
* single-locale functions in `feeds.ts`; this module only loops over locales and localizes URLs.
|
|
5
|
+
*/
|
|
6
|
+
import type { Site } from "@cloudhal/core-contracts";
|
|
7
|
+
import { type SitemapUrl } from "./feeds.js";
|
|
8
|
+
import type { GraphSource } from "./source.js";
|
|
9
|
+
/** The locales a host serves: its own one, or every locale without a domain of its own. */
|
|
10
|
+
export declare function hostLocales(site: Site, host: string): string[];
|
|
11
|
+
/**
|
|
12
|
+
* The sitemap of a host: for each locale it serves, the canonical, indexable URLs `sitemapUrls`
|
|
13
|
+
* picks (pages, entries, archives with the locale's slugs) on the locale's URLs, each with its
|
|
14
|
+
* language versions as alternates.
|
|
15
|
+
*/
|
|
16
|
+
export declare function localizedSitemap(source: GraphSource, origin: string, host: string): Promise<SitemapUrl[]>;
|
|
17
|
+
/** A locale's site feed: its newest published entries, its links on the locale's URLs. */
|
|
18
|
+
export declare function localeFeedXml(source: GraphSource, input: {
|
|
19
|
+
origin: string;
|
|
20
|
+
locale: string;
|
|
21
|
+
}): Promise<string>;
|
|
22
|
+
/**
|
|
23
|
+
* `llms.txt` (llmstxt.org) of a locale: the site, then its pages and newest articles as Markdown
|
|
24
|
+
* links on the locale's URLs, and links to the other locales' files. Published, indexable only.
|
|
25
|
+
*/
|
|
26
|
+
export declare function llmsTxt(source: GraphSource, input: {
|
|
27
|
+
origin: string;
|
|
28
|
+
locale: string;
|
|
29
|
+
}): Promise<string>;
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RFC-0033 §4 (phase B2): the machine-readable views of every locale a host serves — the sitemap
|
|
3
|
+
* with `xhtml:link` alternates per URL, a feed per locale and `llms.txt` per locale. Built on the
|
|
4
|
+
* single-locale functions in `feeds.ts`; this module only loops over locales and localizes URLs.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { archiveSummaries } from "./archive.js";
|
|
8
|
+
import { FEED_LIMIT, rssXml, sitemapUrls } from "./feeds.js";
|
|
9
|
+
import { hostLocale, localeUrl } from "./locales.js";
|
|
10
|
+
import { loadAssetIndex } from "./media.js";
|
|
11
|
+
import { notFoundPageIds } from "./not-found.js";
|
|
12
|
+
import { entryPath } from "./routing.js";
|
|
13
|
+
|
|
14
|
+
import { loadTaxonomyIndex } from "./taxonomy.js";
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
const pathOf = (n ) => ("path" in n ? n.path : entryPath(n));
|
|
18
|
+
|
|
19
|
+
/** The locales a host serves: its own one, or every locale without a domain of its own. */
|
|
20
|
+
export function hostLocales(site , host ) {
|
|
21
|
+
const own = hostLocale(site, host);
|
|
22
|
+
if (own) return [own];
|
|
23
|
+
const domains = site.localization?.domains ?? {};
|
|
24
|
+
return site.locales.filter((l) => domains[l] === undefined);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Published pages and entries of every locale, for alternates. */
|
|
28
|
+
async function publishedNodes(source , site ) {
|
|
29
|
+
const entries = await Promise.all(site.locales.map((l) => source.allEntries(l)));
|
|
30
|
+
return [...(await source.pages()), ...entries.flat()].filter((n) => n.status === "published");
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Every language version of each grouped node, as `{locale, href}` with `x-default`. */
|
|
34
|
+
function alternateIndex(site , nodes , origin ) {
|
|
35
|
+
const groups = new Map ();
|
|
36
|
+
for (const n of nodes) {
|
|
37
|
+
const g = n.translation?.group;
|
|
38
|
+
if (!g) continue;
|
|
39
|
+
const key = `${"path" in n ? "p" : "e"}:${g}`;
|
|
40
|
+
groups.set(key, [...(groups.get(key) ?? []), n]);
|
|
41
|
+
}
|
|
42
|
+
return (node ) => {
|
|
43
|
+
const g = node.translation?.group;
|
|
44
|
+
const members = g ? (groups.get(`${"path" in node ? "p" : "e"}:${g}`) ?? []) : [];
|
|
45
|
+
if (members.length < 2) return undefined;
|
|
46
|
+
const out = members
|
|
47
|
+
.filter((m) => site.locales.includes(m.locale))
|
|
48
|
+
.sort((a, b) => site.locales.indexOf(a.locale) - site.locales.indexOf(b.locale))
|
|
49
|
+
.map((m) => ({ locale: m.locale, href: localeUrl(site, m.locale, pathOf(m), origin) }));
|
|
50
|
+
const fallback = out.find((a) => a.locale === site.defaultLocale);
|
|
51
|
+
return fallback ? [...out, { locale: "x-default", href: fallback.href }] : out;
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* The sitemap of a host: for each locale it serves, the canonical, indexable URLs `sitemapUrls`
|
|
57
|
+
* picks (pages, entries, archives with the locale's slugs) on the locale's URLs, each with its
|
|
58
|
+
* language versions as alternates.
|
|
59
|
+
*/
|
|
60
|
+
export async function localizedSitemap(
|
|
61
|
+
source ,
|
|
62
|
+
origin ,
|
|
63
|
+
host ,
|
|
64
|
+
) {
|
|
65
|
+
const site = await source.site();
|
|
66
|
+
const nodes = await publishedNodes(source, site);
|
|
67
|
+
const alternates = alternateIndex(site, nodes, origin);
|
|
68
|
+
const pages = await source.pages();
|
|
69
|
+
const out = [];
|
|
70
|
+
for (const locale of hostLocales(site, host)) {
|
|
71
|
+
const entries = (await source.allEntries(locale)).filter((e) => e.status === "published");
|
|
72
|
+
const archives = archiveSummaries(await loadTaxonomyIndex(source, locale), entries);
|
|
73
|
+
// `sitemapUrls` lists the site's default locale: view the site as if `locale` were it.
|
|
74
|
+
const urls = sitemapUrls({ ...site, defaultLocale: locale }, pages, entries, origin, archives);
|
|
75
|
+
const byPath = new Map(nodes.filter((n) => n.locale === locale).map((n) => [pathOf(n), n]));
|
|
76
|
+
for (const u of urls) {
|
|
77
|
+
const path = u.loc.slice(origin.length) || "/";
|
|
78
|
+
const node = byPath.get(path);
|
|
79
|
+
const alts = node ? alternates(node) : undefined;
|
|
80
|
+
out.push({
|
|
81
|
+
loc: localeUrl(site, locale, path, origin),
|
|
82
|
+
lastmod: u.lastmod,
|
|
83
|
+
...(alts ? { alternates: alts } : {}),
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return out;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** A locale's site feed: its newest published entries, its links on the locale's URLs. */
|
|
91
|
+
export async function localeFeedXml(
|
|
92
|
+
source ,
|
|
93
|
+
input ,
|
|
94
|
+
) {
|
|
95
|
+
const { origin, locale } = input;
|
|
96
|
+
const site = await source.site();
|
|
97
|
+
const home = await source.pageByPath("/", locale);
|
|
98
|
+
return rssXml({
|
|
99
|
+
site,
|
|
100
|
+
origin,
|
|
101
|
+
locale,
|
|
102
|
+
linkOrigin: localeUrl(site, locale, "", origin),
|
|
103
|
+
entries: await source.allEntries(locale),
|
|
104
|
+
taxonomy: await loadTaxonomyIndex(source, locale),
|
|
105
|
+
assets: await loadAssetIndex(source),
|
|
106
|
+
...(home?.seo?.description ? { description: home.seo.description } : {}),
|
|
107
|
+
...(home ? { title: home.seo?.title ?? site.name } : {}),
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* `llms.txt` (llmstxt.org) of a locale: the site, then its pages and newest articles as Markdown
|
|
113
|
+
* links on the locale's URLs, and links to the other locales' files. Published, indexable only.
|
|
114
|
+
*/
|
|
115
|
+
export async function llmsTxt(
|
|
116
|
+
source ,
|
|
117
|
+
input ,
|
|
118
|
+
) {
|
|
119
|
+
const { origin, locale } = input;
|
|
120
|
+
const site = await source.site();
|
|
121
|
+
const home = await source.pageByPath("/", locale);
|
|
122
|
+
const visible = (n ) =>
|
|
123
|
+
n.status === "published" && n.locale === locale && !("seo" in n && n.seo?.noindex);
|
|
124
|
+
const all = await source.pages();
|
|
125
|
+
const notFound = notFoundPageIds(all);
|
|
126
|
+
const pages = all.filter((p) => visible(p) && !notFound.has(p.id));
|
|
127
|
+
const entries = (await source.allEntries(locale)).filter(visible).slice(0, FEED_LIMIT * 4);
|
|
128
|
+
const line = (n ) => {
|
|
129
|
+
const summary = "summary" in n && n.summary ? `: ${n.summary.replace(/\s+/g, " ")}` : "";
|
|
130
|
+
return `- [${n.title}](${localeUrl(site, locale, pathOf(n), origin)})${summary}`;
|
|
131
|
+
};
|
|
132
|
+
const others = site.locales.filter((l) => l !== locale);
|
|
133
|
+
const description = home?.seo?.description ?? site.name;
|
|
134
|
+
return [
|
|
135
|
+
`# ${site.name}`,
|
|
136
|
+
"",
|
|
137
|
+
`> ${description.replace(/\s+/g, " ")}`,
|
|
138
|
+
"",
|
|
139
|
+
`Language: ${locale}`,
|
|
140
|
+
"",
|
|
141
|
+
...(pages.length ? ["## Pages", "", ...pages.map(line), ""] : []),
|
|
142
|
+
...(entries.length ? ["## Articles", "", ...entries.map(line), ""] : []),
|
|
143
|
+
...(others.length
|
|
144
|
+
? [
|
|
145
|
+
"## Other languages",
|
|
146
|
+
"",
|
|
147
|
+
...others.map((l) => `- [${l}](${localeUrl(site, l, "/llms.txt", origin)})`),
|
|
148
|
+
"",
|
|
149
|
+
]
|
|
150
|
+
: []),
|
|
151
|
+
].join("\n");
|
|
152
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RFC-0033 §4: a localized page's internal links lead to the same locale. The page route puts a
|
|
3
|
+
* localizer on `locals.cloudhalLinks` (`linkLocalizer`); this middleware rewrites the HTML it
|
|
4
|
+
* rendered (`localizeLinks`). Starters export it as their `src/middleware.ts`.
|
|
5
|
+
*/
|
|
6
|
+
import { localizeLinks } from "./locales.js";
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
export async function onRequest(
|
|
13
|
+
context ,
|
|
14
|
+
next ,
|
|
15
|
+
) {
|
|
16
|
+
const response = await next();
|
|
17
|
+
const localize = (context.locals ).cloudhalLinks;
|
|
18
|
+
if (!localize || !response.headers.get("content-type")?.includes("text/html")) return response;
|
|
19
|
+
const headers = new Headers(response.headers);
|
|
20
|
+
headers.delete("content-length");
|
|
21
|
+
return new Response(localizeLinks(await response.text(), localize), {
|
|
22
|
+
status: response.status,
|
|
23
|
+
statusText: response.statusText,
|
|
24
|
+
headers,
|
|
25
|
+
});
|
|
26
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RFC-0033 §4 / ADR-0035: a site's locales on the web. The locale is in the path
|
|
3
|
+
* (`prefix-except-default`: the default locale at the root, the others under `/<locale>`;
|
|
4
|
+
* `prefix-all`: every locale prefixed) or in the host (`localization.domains`), never in a cookie
|
|
5
|
+
* or `Accept-Language`. This module splits a request into locale and graph path, builds localized
|
|
6
|
+
* URLs, lists a node's language versions for `hreflang`, and rewrites a localized page's internal
|
|
7
|
+
* links to the same locale. Worker-safe.
|
|
8
|
+
*/
|
|
9
|
+
import type { ContentEntry, Page, Site } from "@cloudhal/core-contracts";
|
|
10
|
+
import type { HeadMeta } from "./seo.js";
|
|
11
|
+
import type { GraphSource } from "./source.js";
|
|
12
|
+
/** `<html dir>` for a locale. Kurdish written in Latin (`ku-Latn`) is the one exception handled. */
|
|
13
|
+
export declare function textDirection(locale: string): "ltr" | "rtl";
|
|
14
|
+
type Routing = "prefix-except-default" | "prefix-all";
|
|
15
|
+
export declare function localeRouting(site: Site): Routing;
|
|
16
|
+
/** The locale a host serves on its own (`localization.domains`), if any. */
|
|
17
|
+
export declare function hostLocale(site: Site, host: string): string | undefined;
|
|
18
|
+
export interface LocaleRequest {
|
|
19
|
+
/** The locale the request is for. */
|
|
20
|
+
readonly locale: string;
|
|
21
|
+
/** The graph path inside that locale (no prefix). */
|
|
22
|
+
readonly path: string;
|
|
23
|
+
/** Where to send the request instead (a 301), for a non-canonical locale URL. */
|
|
24
|
+
readonly redirect?: string;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Splits a request path (no trailing slash, `/` for the root) into locale and graph path. A
|
|
28
|
+
* default-locale prefix under `prefix-except-default` and an unprefixed path under `prefix-all`
|
|
29
|
+
* redirect to the canonical URL; a locale served by its own domain is never a prefix.
|
|
30
|
+
*/
|
|
31
|
+
export declare function localeRequest(site: Site, host: string, path: string): LocaleRequest;
|
|
32
|
+
/** A graph path as a URL path of `locale` on the site's own host (no domain mapping). */
|
|
33
|
+
export declare function localizedPath(site: Site, locale: string, path: string): string;
|
|
34
|
+
/** The absolute URL of a graph path in `locale`: its own domain when it has one. */
|
|
35
|
+
export declare function localeUrl(site: Site, locale: string, path: string, origin: string): string;
|
|
36
|
+
/** One language version of a page or entry for `hreflang` and the language switcher. */
|
|
37
|
+
export interface Alternate {
|
|
38
|
+
/** BCP 47 locale, or `x-default`. */
|
|
39
|
+
readonly locale: string;
|
|
40
|
+
readonly href: string;
|
|
41
|
+
/** This is the version being rendered. */
|
|
42
|
+
readonly current: boolean;
|
|
43
|
+
/** This is the group's original (the source of the translations). */
|
|
44
|
+
readonly original?: boolean;
|
|
45
|
+
}
|
|
46
|
+
type Localized = Page | ContentEntry;
|
|
47
|
+
/**
|
|
48
|
+
* RFC-0034 §6: another site of the project holding versions of the same work (a language
|
|
49
|
+
* edition): its published source and the origin it is served on.
|
|
50
|
+
*/
|
|
51
|
+
export interface PeerEdition {
|
|
52
|
+
readonly source: GraphSource;
|
|
53
|
+
readonly origin: string;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* RFC-0033 invariant 7: every visible version of the node (itself included), plus `x-default`
|
|
57
|
+
* (the default locale's version). Empty when the node has no other version — `hreflang` needs two.
|
|
58
|
+
* `peers` adds the versions published by other sites of the project (RFC-0034 §6), each on its
|
|
59
|
+
* own host; a locale this site serves itself is not taken from a peer.
|
|
60
|
+
*/
|
|
61
|
+
export declare function alternatesFor(source: GraphSource, node: Localized, origin: string, peers?: readonly PeerEdition[]): Promise<Alternate[]>;
|
|
62
|
+
/**
|
|
63
|
+
* Internal links of a page in `locale` (`origin`: the site's primary origin, for links back from a
|
|
64
|
+
* locale's own domain): a path that leads to a page, entry or archive with a
|
|
65
|
+
* version in `locale` goes there; one that leads to a default-locale node without such a version
|
|
66
|
+
* stays in the default locale (a reader may still follow it); anything else — search, feeds,
|
|
67
|
+
* unknown paths — stays in `locale`.
|
|
68
|
+
*/
|
|
69
|
+
export declare function linkLocalizer(source: GraphSource, locale: string, origin: string): Promise<((path: string) => string) | undefined>;
|
|
70
|
+
/**
|
|
71
|
+
* Rewrites root-relative `href`s of `<a>`, `action`s of `<form>` and the search dialog's
|
|
72
|
+
* `data-endpoint`/`data-page` in rendered HTML through `localize` (query and fragment kept).
|
|
73
|
+
* Platform paths are left alone.
|
|
74
|
+
*/
|
|
75
|
+
export declare function localizeLinks(html: string, localize: (path: string) => string): string;
|
|
76
|
+
/**
|
|
77
|
+
* The head of a page rendered in `locale`: its canonical, `og:url` and feed links on the locale's
|
|
78
|
+
* URLs (each version is self-canonical, never pointing at another language), `lang`/`dir`, the
|
|
79
|
+
* `hreflang` set and `og:locale:alternate`.
|
|
80
|
+
*/
|
|
81
|
+
export declare function localizeHead(meta: HeadMeta, site: Site, input: {
|
|
82
|
+
locale: string;
|
|
83
|
+
origin: string;
|
|
84
|
+
alternates: readonly Alternate[];
|
|
85
|
+
translation?: Localized["translation"];
|
|
86
|
+
}): HeadMeta;
|
|
87
|
+
export {};
|