@cloudhal/astro-renderer 0.0.0-stage → 0.1.1
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,533 @@
|
|
|
1
|
+
|
|
2
|
+
import { createBlockRegistry } from "@cloudhal/core-ts/blocks";
|
|
3
|
+
import { directiveExtension } from "@cloudhal/core-ts/body-blocks";
|
|
4
|
+
import { checkDirective, directiveProps } from "@cloudhal/core-ts/directives";
|
|
5
|
+
import { ASSET_HREF, assetUrl, mediaPath } from "@cloudhal/core-ts/media";
|
|
6
|
+
import { parseSurfaceFence } from "@cloudhal/core-ts/surfaces";
|
|
7
|
+
import { Marked } from "marked";
|
|
8
|
+
import { builtInBlockHtml, isBuiltInBlock, missingBlockHtml } from "./block-html.js";
|
|
9
|
+
import {
|
|
10
|
+
assetAlt,
|
|
11
|
+
assetCaption,
|
|
12
|
+
downloadMeta,
|
|
13
|
+
imageAttributes,
|
|
14
|
+
imgHtml,
|
|
15
|
+
isAttachment,
|
|
16
|
+
|
|
17
|
+
} from "./media.js";
|
|
18
|
+
import { safeHref, safeSrc } from "./safe-urls.js";
|
|
19
|
+
import { sanitizeHtml } from "./sanitize.js";
|
|
20
|
+
import { renderSurface, surfaceProblems } from "./surfaces.js";
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
export const escapeHtml = (s ) =>
|
|
25
|
+
s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
|
26
|
+
|
|
27
|
+
export { safeHref, safeSrc };
|
|
28
|
+
|
|
29
|
+
/** The context of the markdown being rendered right now (`renderMarkdown` is synchronous). */
|
|
30
|
+
let current ;
|
|
31
|
+
/** Heading ids already given out in the document being rendered (nested renders share them). */
|
|
32
|
+
let headingIds ;
|
|
33
|
+
/** While true (`renderInlineMarkdown`), images render as their text. */
|
|
34
|
+
let inlineOnly = false;
|
|
35
|
+
/** The footnotes of the document being rendered: definitions by id (rendered), refs in order. */
|
|
36
|
+
let footnotes ;
|
|
37
|
+
const FN_REF = (id ) => `\uE000fn:${id}\uE000`;
|
|
38
|
+
|
|
39
|
+
const TURKISH = { ı: "i", İ: "i", ş: "s", Ş: "s", ğ: "g", Ğ: "g" };
|
|
40
|
+
/**
|
|
41
|
+
* Ids our own markup uses (comments, forms) or may use: a heading that would take one gets `h-`
|
|
42
|
+
* in front, so content can never shadow an element Cloudhal's scripts look up by id.
|
|
43
|
+
*/
|
|
44
|
+
const RESERVED_ID = /^(?:comment|cloudhal|ch-)/;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* A heading's anchor (RFC-0018 §3): ASCII slug of its text, unique in the document. Text without
|
|
48
|
+
* Latin letters or digits falls back to `section`; repeats get `-2`, `-3`, ….
|
|
49
|
+
*/
|
|
50
|
+
export function headingSlug(text , taken ) {
|
|
51
|
+
const base =
|
|
52
|
+
text
|
|
53
|
+
.replace(/[ıİşŞğĞ]/g, (c) => TURKISH[c] ?? c)
|
|
54
|
+
.normalize("NFKD")
|
|
55
|
+
.replace(/[̀-ͯ]/g, "")
|
|
56
|
+
.toLowerCase()
|
|
57
|
+
.replace(/ß/g, "ss")
|
|
58
|
+
.replace(/[^a-z0-9]+/g, "-")
|
|
59
|
+
.replace(/^-+|-+$/g, "")
|
|
60
|
+
.slice(0, 64)
|
|
61
|
+
.replace(/-+$/, "") || "section";
|
|
62
|
+
const slug = RESERVED_ID.test(base) || /^\d/.test(base) ? `h-${base}` : base;
|
|
63
|
+
if (!taken) return slug;
|
|
64
|
+
const seen = taken.get(slug) ?? 0;
|
|
65
|
+
taken.set(slug, seen + 1);
|
|
66
|
+
return seen === 0 ? slug : `${slug}-${seen + 1}`;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* `asset:<id>` (exactly; RFC-0004 §7.6 rule 1) resolved against the render's assets. Anything else
|
|
71
|
+
* under the scheme, an unknown id or no context resolves to nothing and renders like an unsafe URL.
|
|
72
|
+
*/
|
|
73
|
+
function resolveAsset(href ) {
|
|
74
|
+
const m = ASSET_HREF.exec(href.trim());
|
|
75
|
+
if (!m) return undefined;
|
|
76
|
+
const asset = current?.assets?.get(m[1] );
|
|
77
|
+
if (!asset) current?.onMiss?.(m[1] );
|
|
78
|
+
return asset;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function localeOf() {
|
|
82
|
+
const defaultLocale = current?.defaultLocale ?? current?.locale ?? "en";
|
|
83
|
+
return { locale: current?.locale ?? defaultLocale, defaultLocale };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** A download link: the URL comes from validated parts only, the label is the markdown's (§7.6). */
|
|
87
|
+
function assetLink(asset , labelHtml , title ) {
|
|
88
|
+
const href = assetUrl(asset);
|
|
89
|
+
const download = isAttachment(asset) ? " download" : "";
|
|
90
|
+
const link = `<a href="${escapeHtml(href)}" type="${escapeHtml(asset.mediaType)}"${download}${title ? ` title="${escapeHtml(title)}"` : ""}>${labelHtml}</a>`;
|
|
91
|
+
return asset.kind === "document" || asset.kind === "data" || asset.kind === "other"
|
|
92
|
+
? `${link} ${downloadMeta(asset)}`
|
|
93
|
+
: link;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function assetEmbed(asset , text , title ) {
|
|
97
|
+
const { locale, defaultLocale } = localeOf();
|
|
98
|
+
const alt = assetAlt(asset, text, locale, defaultLocale);
|
|
99
|
+
const titleAttr = title ? ` title="${escapeHtml(title)}"` : "";
|
|
100
|
+
switch (asset.kind) {
|
|
101
|
+
case "image":
|
|
102
|
+
return imgHtml(
|
|
103
|
+
imageAttributes(
|
|
104
|
+
asset,
|
|
105
|
+
{ alt: text },
|
|
106
|
+
{
|
|
107
|
+
locale,
|
|
108
|
+
defaultLocale,
|
|
109
|
+
transform: current?.transform ?? "none",
|
|
110
|
+
...(current?.budget ? { budget: current.budget } : {}),
|
|
111
|
+
sizes: "(min-width: 48rem) 48rem, 100vw",
|
|
112
|
+
...(current?.preview ? { preview: true } : {}),
|
|
113
|
+
},
|
|
114
|
+
),
|
|
115
|
+
titleAttr,
|
|
116
|
+
);
|
|
117
|
+
case "video":
|
|
118
|
+
case "audio": {
|
|
119
|
+
const src = escapeHtml(assetUrl(asset));
|
|
120
|
+
const fallback = assetLink(asset, escapeHtml(alt || asset.slug));
|
|
121
|
+
return `<${asset.kind} controls preload="metadata" src="${src}"${alt ? ` aria-label="${escapeHtml(alt)}"` : ""}${titleAttr}>${fallback}</${asset.kind}>`;
|
|
122
|
+
}
|
|
123
|
+
default:
|
|
124
|
+
return assetLink(asset, escapeHtml(text || asset.slug), title);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Markdown is written by agents as well as people, so it is rendered as data, not markup: raw
|
|
130
|
+
* HTML is escaped (no script/iframe/event handlers can reach the published site) and links and
|
|
131
|
+
* images only keep safe URL schemes. `asset:<id>` links and images resolve to the site's media.
|
|
132
|
+
*/
|
|
133
|
+
const md = new Marked({ gfm: true, async: false });
|
|
134
|
+
const builtInTypes = createBlockRegistry();
|
|
135
|
+
|
|
136
|
+
/** A theme's blocks in the body being rendered right now (`renderBodyParts`). */
|
|
137
|
+
let themed ;
|
|
138
|
+
|
|
139
|
+
/** A body slot holding a ```surface fence (RFC-0020 §5); not a block type name (those are kebab). */
|
|
140
|
+
export const SURFACE_SLOT = "@surface";
|
|
141
|
+
|
|
142
|
+
/** Where a theme component goes in a body's HTML (`renderBodyParts`); escaped text never matches. */
|
|
143
|
+
const SLOT = (i ) => `<!--ch-block-slot:${i}-->`;
|
|
144
|
+
const SLOT_RE = /<!--ch-block-slot:(\d+)-->/;
|
|
145
|
+
|
|
146
|
+
md.use({
|
|
147
|
+
// Body directives (RFC-0015 §5): a built-in block renders like its component section; a
|
|
148
|
+
// container's inner markdown goes through this same renderer. A type the theme renders with its
|
|
149
|
+
// own component becomes a slot (`renderBodyParts`). Unknown types and props that do not fit
|
|
150
|
+
// render nothing on the site and a labelled placeholder in previews.
|
|
151
|
+
extensions: [
|
|
152
|
+
{
|
|
153
|
+
// Footnotes (GFM): `[^id]` refers, `[^id]: text` defines (continued by indented lines).
|
|
154
|
+
name: "footnoteDef",
|
|
155
|
+
level: "block",
|
|
156
|
+
start: (src ) => src.match(/^\[\^[\w-]{1,40}\]:/m)?.index,
|
|
157
|
+
tokenizer(src ) {
|
|
158
|
+
const m = /^\[\^([\w-]{1,40})\]:[ \t]+([^\n]*(?:\n(?: {2,}|\t)[^\n]*)*)(?:\n+|$)/.exec(src);
|
|
159
|
+
if (!m) return undefined;
|
|
160
|
+
const text = (m[2] ?? "").replace(/\n(?: {2,}|\t)/g, " ");
|
|
161
|
+
return { type: "footnoteDef", raw: m[0], id: m[1], tokens: this.lexer.inlineTokens(text) };
|
|
162
|
+
},
|
|
163
|
+
renderer(token) {
|
|
164
|
+
const t = token ;
|
|
165
|
+
if (footnotes && !footnotes.defs.has(t.id))
|
|
166
|
+
footnotes.defs.set(t.id, this.parser.parseInline(t.tokens));
|
|
167
|
+
return "";
|
|
168
|
+
},
|
|
169
|
+
},
|
|
170
|
+
{
|
|
171
|
+
name: "footnoteRef",
|
|
172
|
+
level: "inline",
|
|
173
|
+
start: (src ) => src.indexOf("[^"),
|
|
174
|
+
tokenizer(src ) {
|
|
175
|
+
const m = /^\[\^([\w-]{1,40})\](?!:)/.exec(src);
|
|
176
|
+
return m ? { type: "footnoteRef", raw: m[0], id: m[1] } : undefined;
|
|
177
|
+
},
|
|
178
|
+
renderer(token) {
|
|
179
|
+
const id = (token ).id;
|
|
180
|
+
if (!footnotes || inlineOnly) return escapeHtml(`[^${id}]`);
|
|
181
|
+
if (!footnotes.order.includes(id)) footnotes.order.push(id);
|
|
182
|
+
return FN_REF(id);
|
|
183
|
+
},
|
|
184
|
+
},
|
|
185
|
+
{
|
|
186
|
+
...directiveExtension,
|
|
187
|
+
renderer(token) {
|
|
188
|
+
const { directive, tokens } = token ;
|
|
189
|
+
const registry = themed?.registry ?? current?.blocks ?? builtInTypes;
|
|
190
|
+
const issues = checkDirective(registry, directive);
|
|
191
|
+
const ctx = current;
|
|
192
|
+
const valid = issues !== undefined && issues.length === 0;
|
|
193
|
+
const props = directiveProps(registry.get(directive.name), directive.attrs);
|
|
194
|
+
const contentHtml = directive.content !== undefined ? this.parser.parse(tokens) : undefined;
|
|
195
|
+
if (valid && themed?.names.has(directive.name)) {
|
|
196
|
+
themed.slots.push({
|
|
197
|
+
type: directive.name,
|
|
198
|
+
props,
|
|
199
|
+
...(directive.content !== undefined ? { content: directive.content } : {}),
|
|
200
|
+
...(contentHtml !== undefined ? { contentHtml } : {}),
|
|
201
|
+
});
|
|
202
|
+
return SLOT(themed.slots.length - 1);
|
|
203
|
+
}
|
|
204
|
+
const html =
|
|
205
|
+
valid && isBuiltInBlock(directive.name)
|
|
206
|
+
? builtInBlockHtml(directive.name, props, {
|
|
207
|
+
markdown: (source) => renderMarkdown(source, ctx),
|
|
208
|
+
...(contentHtml !== undefined ? { contentHtml } : {}),
|
|
209
|
+
})
|
|
210
|
+
: "";
|
|
211
|
+
const attrs = `data-block-type="${escapeHtml(directive.name)}"`;
|
|
212
|
+
if (html) return `<div class="ch-block" ${attrs}>${html}</div>`;
|
|
213
|
+
if (!ctx?.preview) return "";
|
|
214
|
+
return missingBlockHtml(
|
|
215
|
+
"div",
|
|
216
|
+
attrs,
|
|
217
|
+
directive.name,
|
|
218
|
+
issues === undefined
|
|
219
|
+
? "unknown block type"
|
|
220
|
+
: valid
|
|
221
|
+
? "shown by the theme on the site"
|
|
222
|
+
: "props do not fit the block type",
|
|
223
|
+
);
|
|
224
|
+
},
|
|
225
|
+
},
|
|
226
|
+
],
|
|
227
|
+
renderer: {
|
|
228
|
+
// Bodies carry no HTML (it is never trusted). A block of it (`<div>…</div>`, `<details>`)
|
|
229
|
+
// loses its tags and keeps its words, so a reader never sees markup as text; inline `<…>` is
|
|
230
|
+
// shown as typed (it is as often words in brackets as a tag).
|
|
231
|
+
html({ text, block }) {
|
|
232
|
+
if (inlineOnly || !block) return escapeHtml(text);
|
|
233
|
+
const words = text
|
|
234
|
+
.replace(/<!--[\s\S]*?(?:-->|$)/g, "")
|
|
235
|
+
.replace(/<(script|style)\b[\s\S]*?(?:<\/\1\s*>|$)/gi, "")
|
|
236
|
+
.replace(/<\/?[a-zA-Z][^>]*>/g, " ")
|
|
237
|
+
.replace(/[ \t]+/g, " ");
|
|
238
|
+
return words.trim() ? escapeHtml(words) : "";
|
|
239
|
+
},
|
|
240
|
+
// RFC-0020 §5: a ```surface fence is generative UI. With the theme's components it becomes a
|
|
241
|
+
// slot `Body.astro` fills with `Surface.astro`; otherwise Cloudhal's own markup. Any other
|
|
242
|
+
// fence is ordinary code.
|
|
243
|
+
code({ text, lang }) {
|
|
244
|
+
const info = /^surface(?:\s+(.*))?$/s.exec(lang ?? "");
|
|
245
|
+
if (!info) return false;
|
|
246
|
+
const fence = parseSurfaceFence(info[1] ?? "", text.split("\n"));
|
|
247
|
+
// A preview says why a block draws nothing; the published page stays silent about it.
|
|
248
|
+
if (current?.preview) {
|
|
249
|
+
const problems = surfaceProblems(fence.section, fence.issues, current.blocks);
|
|
250
|
+
if (problems.length)
|
|
251
|
+
return `<div class="ch-surface-problem" role="note"><p><strong>${
|
|
252
|
+
current.locale?.startsWith("tr")
|
|
253
|
+
? "Bu etkileşimli blok gösterilemiyor.</strong> Markdown modunda düzeltin:"
|
|
254
|
+
: "This interactive block cannot be shown.</strong> Fix it in Markdown mode:"
|
|
255
|
+
}</p><ul>${problems
|
|
256
|
+
.slice(0, 5)
|
|
257
|
+
.map((p) => `<li><code>${escapeHtml(p)}</code></li>`)
|
|
258
|
+
.join("")}</ul></div>`;
|
|
259
|
+
}
|
|
260
|
+
if (themed) {
|
|
261
|
+
themed.slots.push({ type: SURFACE_SLOT, props: { surface: fence.section } });
|
|
262
|
+
return SLOT(themed.slots.length - 1);
|
|
263
|
+
}
|
|
264
|
+
return renderSurface(fence.section, {
|
|
265
|
+
...(current ? { media: current } : {}),
|
|
266
|
+
...(current?.preview ? { preview: true } : {}),
|
|
267
|
+
});
|
|
268
|
+
},
|
|
269
|
+
// h2–h6 get ids for a table of contents and deep links; the h1 is the article's own title.
|
|
270
|
+
heading({ tokens, depth }) {
|
|
271
|
+
const inner = this.parser.parseInline(tokens);
|
|
272
|
+
if (depth === 1) return `<h1>${inner}</h1>\n`;
|
|
273
|
+
const id = headingSlug(htmlText(inner), headingIds);
|
|
274
|
+
return `<h${depth} id="${id}">${inner}</h${depth}>\n`;
|
|
275
|
+
},
|
|
276
|
+
link({ href, title, tokens }) {
|
|
277
|
+
const label = this.parser.parseInline(tokens);
|
|
278
|
+
if (/^\s*asset:/i.test(href)) {
|
|
279
|
+
const asset = resolveAsset(href);
|
|
280
|
+
return asset ? assetLink(asset, label, title) : label;
|
|
281
|
+
}
|
|
282
|
+
const safe = safeHref(href);
|
|
283
|
+
if (!safe) return label;
|
|
284
|
+
return `<a href="${escapeHtml(safe)}"${title ? ` title="${escapeHtml(title)}"` : ""}>${label}</a>`;
|
|
285
|
+
},
|
|
286
|
+
image({ href, title, text }) {
|
|
287
|
+
if (inlineOnly) return escapeHtml(text);
|
|
288
|
+
if (/^\s*asset:/i.test(href)) {
|
|
289
|
+
const asset = resolveAsset(href);
|
|
290
|
+
return asset ? assetEmbed(asset, text, title) : escapeHtml(text);
|
|
291
|
+
}
|
|
292
|
+
const safe = safeSrc(href);
|
|
293
|
+
if (!safe) return escapeHtml(text);
|
|
294
|
+
return `<img src="${escapeHtml(safe)}" alt="${escapeHtml(text)}"${title ? ` title="${escapeHtml(title)}"` : ""} loading="lazy">`;
|
|
295
|
+
},
|
|
296
|
+
},
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Renders markdown safely. With `ctx.assets`, `asset:<id>` references become images, players and
|
|
301
|
+
* download links (RFC-0004 §7.6); without it they degrade to their text, like any unsafe URL.
|
|
302
|
+
*/
|
|
303
|
+
export function renderMarkdown(source , ctx ) {
|
|
304
|
+
const previous = current;
|
|
305
|
+
const outer = headingIds === undefined;
|
|
306
|
+
current = ctx;
|
|
307
|
+
if (outer) {
|
|
308
|
+
headingIds = new Map();
|
|
309
|
+
footnotes = { defs: new Map(), order: [] };
|
|
310
|
+
}
|
|
311
|
+
try {
|
|
312
|
+
const html = md.parse(source) ;
|
|
313
|
+
return outer ? withFootnotes(html, ctx?.locale) : html;
|
|
314
|
+
} finally {
|
|
315
|
+
current = previous;
|
|
316
|
+
if (outer) {
|
|
317
|
+
headingIds = undefined;
|
|
318
|
+
footnotes = undefined;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* Numbers the footnote references in order of first use and lists their definitions at the end,
|
|
325
|
+
* each with a link back; a reference without a definition stays as written.
|
|
326
|
+
*/
|
|
327
|
+
function withFootnotes(html , locale = "en") {
|
|
328
|
+
const fn = footnotes;
|
|
329
|
+
if (!fn || !html.includes("\uE000fn:")) return html;
|
|
330
|
+
const used = fn.order.filter((id) => fn.defs.has(id));
|
|
331
|
+
const tr = locale.startsWith("tr");
|
|
332
|
+
const out = html.replace(/\uE000fn:([\w-]{1,40})\uE000/g, (_, id ) => {
|
|
333
|
+
const n = used.indexOf(id) + 1;
|
|
334
|
+
return n === 0
|
|
335
|
+
? escapeHtml(`[^${id}]`)
|
|
336
|
+
: `<sup class="ch-fnref"><a href="#ch-fn-${id}" id="ch-fnref-${id}" aria-describedby="ch-footnotes-label">${n}</a></sup>`;
|
|
337
|
+
});
|
|
338
|
+
if (used.length === 0) return out;
|
|
339
|
+
const items = used
|
|
340
|
+
.map(
|
|
341
|
+
(id) =>
|
|
342
|
+
`<li id="ch-fn-${id}">${fn.defs.get(id)} <a href="#ch-fnref-${id}" class="ch-fnback" aria-label="${tr ? "Metne dön" : "Back to the text"}">↩</a></li>`,
|
|
343
|
+
)
|
|
344
|
+
.join("");
|
|
345
|
+
return `${out}<section class="ch-footnotes"><h2 class="sr-only" id="ch-footnotes-label">${tr ? "Dipnotlar" : "Footnotes"}</h2><ol>${items}</ol></section>`;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* Inline markdown, for short notes such as corrections (RFC-0019 §2): emphasis, code and safe
|
|
350
|
+
* links; images become their text, and nothing block-level is produced.
|
|
351
|
+
*/
|
|
352
|
+
export function renderInlineMarkdown(source ) {
|
|
353
|
+
const previous = inlineOnly;
|
|
354
|
+
inlineOnly = true;
|
|
355
|
+
try {
|
|
356
|
+
return md.parseInline(source) ;
|
|
357
|
+
} finally {
|
|
358
|
+
inlineOnly = previous;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
const ENTITIES = {
|
|
363
|
+
"&": "&",
|
|
364
|
+
"<": "<",
|
|
365
|
+
">": ">",
|
|
366
|
+
""": '"',
|
|
367
|
+
"'": "'",
|
|
368
|
+
};
|
|
369
|
+
|
|
370
|
+
/** Markdown as plain text (meta descriptions): rendered safely, then tags dropped and entities decoded. */
|
|
371
|
+
export function markdownText(source ) {
|
|
372
|
+
return htmlText(renderMarkdown(source));
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/** Inline markdown as plain text: inline tags go without leaving gaps ("**537**," → "537,"). */
|
|
376
|
+
export function inlineMarkdownText(source ) {
|
|
377
|
+
return htmlText(renderInlineMarkdown(source).replace(/<[^>]*>/g, ""));
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/** Text of HTML this module produced: tags dropped, entities decoded, whitespace collapsed. */
|
|
381
|
+
export function htmlText(html ) {
|
|
382
|
+
return html
|
|
383
|
+
.replace(/<[^>]*>/g, " ")
|
|
384
|
+
.replace(/&(?:amp|lt|gt|quot|#39);/g, (m) => ENTITIES[m] ?? m)
|
|
385
|
+
.replace(/&#(\d{1,7});/g, (m, n ) =>
|
|
386
|
+
Number(n) <= 0x10ffff ? String.fromCodePoint(Number(n)) : m,
|
|
387
|
+
)
|
|
388
|
+
.replace(/\s+/g, " ")
|
|
389
|
+
.trim();
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/**
|
|
393
|
+
* `markdown` bodies are rendered as data; `html` bodies go through the allowlist sanitizer
|
|
394
|
+
* (RFC-0012): any writer can store them — agent keys over REST, `cloudhal push`, every role — so
|
|
395
|
+
* pages, previews, feeds and static builds never publish them as stored. Neither resolves
|
|
396
|
+
* `asset:` inside HTML (RFC-0004 §2.3).
|
|
397
|
+
*/
|
|
398
|
+
export function renderBody(
|
|
399
|
+
body ,
|
|
400
|
+
ctx ,
|
|
401
|
+
) {
|
|
402
|
+
return body.format === "markdown" ? renderMarkdown(body.value, ctx) : sanitizeHtml(body.value);
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
/** A theme block in a body (RFC-0015 §5): its type, props and, for a container, its text. */
|
|
406
|
+
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
|
|
412
|
+
|
|
413
|
+
|
|
414
|
+
|
|
415
|
+
/** A body as HTML runs and the theme blocks between them, in order. */
|
|
416
|
+
|
|
417
|
+
|
|
418
|
+
/**
|
|
419
|
+
* A body for a theme that renders some block types with its own components (`Body.astro`): the
|
|
420
|
+
* HTML is cut where each such block goes. `registry` holds the theme's types (validation);
|
|
421
|
+
* `components` names the types the theme renders. Without any, it is `renderBody` in one part.
|
|
422
|
+
*/
|
|
423
|
+
export function renderBodyParts(
|
|
424
|
+
body ,
|
|
425
|
+
ctx ,
|
|
426
|
+
blocks ,
|
|
427
|
+
) {
|
|
428
|
+
if (
|
|
429
|
+
body.format !== "markdown" ||
|
|
430
|
+
!blocks ||
|
|
431
|
+
(!body.value.includes("::") && !body.value.includes("surface"))
|
|
432
|
+
)
|
|
433
|
+
return [{ html: renderBody(body, ctx) }];
|
|
434
|
+
const previous = themed;
|
|
435
|
+
const slots = [];
|
|
436
|
+
themed = { registry: blocks.registry, names: blocks.components, slots };
|
|
437
|
+
let html ;
|
|
438
|
+
try {
|
|
439
|
+
html = renderMarkdown(body.value, ctx);
|
|
440
|
+
} finally {
|
|
441
|
+
themed = previous;
|
|
442
|
+
}
|
|
443
|
+
const parts = [];
|
|
444
|
+
const pieces = html.split(SLOT_RE);
|
|
445
|
+
pieces.forEach((piece, i) => {
|
|
446
|
+
if (i % 2 === 0) {
|
|
447
|
+
if (piece) parts.push({ html: piece });
|
|
448
|
+
} else {
|
|
449
|
+
const slot = slots[Number(piece)];
|
|
450
|
+
if (slot) parts.push({ slot });
|
|
451
|
+
}
|
|
452
|
+
});
|
|
453
|
+
return parts;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
/**
|
|
457
|
+
* The cover image above the body: eager (it is the largest image above the fold), safe src only.
|
|
458
|
+
* With its asset known, it gets the asset's size, alt fallback, placeholder, `srcset` when
|
|
459
|
+
* transforming, and a caption with the credit (RFC-0004 §7.5, §7.7).
|
|
460
|
+
*/
|
|
461
|
+
function coverFigure(image , ctx ) {
|
|
462
|
+
if (!image) return "";
|
|
463
|
+
const asset = image.assetId ? ctx?.assets?.get(image.assetId) : undefined;
|
|
464
|
+
if (asset) {
|
|
465
|
+
const defaultLocale = ctx?.defaultLocale ?? ctx?.locale ?? "en";
|
|
466
|
+
const locale = ctx?.locale ?? defaultLocale;
|
|
467
|
+
const attrs = imageAttributes(asset, image, {
|
|
468
|
+
locale,
|
|
469
|
+
defaultLocale,
|
|
470
|
+
transform: ctx?.transform ?? "none",
|
|
471
|
+
...(ctx?.budget ? { budget: ctx.budget } : {}),
|
|
472
|
+
sizes: "(min-width: 48rem) 48rem, 100vw",
|
|
473
|
+
priority: true,
|
|
474
|
+
...(ctx?.preview ? { preview: true } : {}),
|
|
475
|
+
});
|
|
476
|
+
const caption = assetCaption(asset, locale, defaultLocale);
|
|
477
|
+
return `<figure class="ch-cover">${imgHtml(attrs)}${caption ? `<figcaption>${escapeHtml(caption)}</figcaption>` : ""}</figure>`;
|
|
478
|
+
}
|
|
479
|
+
const src = safeSrc(image.src);
|
|
480
|
+
if (!src) return "";
|
|
481
|
+
const size =
|
|
482
|
+
image.width && image.height ? ` width="${image.width}" height="${image.height}"` : "";
|
|
483
|
+
return `<figure class="ch-cover"><img src="${escapeHtml(src)}" alt="${escapeHtml(image.alt)}"${size} fetchpriority="high" decoding="async"></figure>`;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
/** An article's pieces: the body to render (a leading H1 repeating the title dropped) and the HTML around it. */
|
|
487
|
+
export function entryArticleParts(
|
|
488
|
+
entry
|
|
489
|
+
|
|
490
|
+
|
|
491
|
+
,
|
|
492
|
+
locale = "en",
|
|
493
|
+
ctx ,
|
|
494
|
+
) {
|
|
495
|
+
let body = entry.body;
|
|
496
|
+
if (body.format === "markdown") {
|
|
497
|
+
const m = body.value.match(/^\s*#\s+(.+?)\s*#*\s*(?:\r?\n|$)/);
|
|
498
|
+
if (m && m[1]?.trim().toLowerCase() === entry.title.trim().toLowerCase())
|
|
499
|
+
body = { ...body, value: body.value.slice(m[0].length) };
|
|
500
|
+
}
|
|
501
|
+
const when = entry.publishedAt ?? undefined;
|
|
502
|
+
const date = when
|
|
503
|
+
? `<time datetime="${escapeHtml(when)}">${escapeHtml(new Date(when).toLocaleDateString(locale, { year: "numeric", month: "long", day: "numeric" }))}</time>`
|
|
504
|
+
: "";
|
|
505
|
+
const summary = entry.summary ? `<p class="ch-summary">${escapeHtml(entry.summary)}</p>` : "";
|
|
506
|
+
const media = { locale, ...ctx };
|
|
507
|
+
return {
|
|
508
|
+
body,
|
|
509
|
+
header: `<header class="ch-article-header"><h1>${escapeHtml(entry.title)}</h1>${summary}${date}</header>`,
|
|
510
|
+
cover: coverFigure(entry.coverImage, media),
|
|
511
|
+
media,
|
|
512
|
+
};
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
/**
|
|
516
|
+
* An article: a header (title, summary, publication date) followed by the body. A leading `# H1`
|
|
517
|
+
* that repeats the title is dropped so authors may write it either way. Themes with their own
|
|
518
|
+
* block components use `EntryArticle.astro`, which has the same markup.
|
|
519
|
+
*/
|
|
520
|
+
export function renderEntryArticle(
|
|
521
|
+
entry
|
|
522
|
+
|
|
523
|
+
|
|
524
|
+
,
|
|
525
|
+
locale = "en",
|
|
526
|
+
ctx ,
|
|
527
|
+
) {
|
|
528
|
+
const { body, header, cover, media } = entryArticleParts(entry, locale, ctx);
|
|
529
|
+
return `<article class="ch-article" data-entry-id="${escapeHtml(entry.id)}">${header}${cover}<div class="ch-richtext">${renderBody(body, media)}</div></article>`;
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
/** The media path of an asset (re-exported for themes). */
|
|
533
|
+
export { mediaPath };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dev middleware (added by the `cloudhal()` integration for Node targets): answers the media route
|
|
3
|
+
* from the local blob store before page routing, with `no-store`, drafts included (RFC-0004 §7.8).
|
|
4
|
+
* The Cloudflare Site Worker answers media in its entry instead (`cloudflare-entry.ts`).
|
|
5
|
+
*/
|
|
6
|
+
import config from "virtual:cloudhal/config";
|
|
7
|
+
import { serveMedia } from "@cloudhal/astro-renderer/runtime";
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
export async function onRequest(
|
|
14
|
+
context ,
|
|
15
|
+
next ,
|
|
16
|
+
) {
|
|
17
|
+
return (await serveMedia(context.request, config)) ?? next();
|
|
18
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The media route (RFC-0004 §7.1–§7.3): `GET`/`HEAD /media/<sha16>/<slug>.<ext>[.sha256]`, answered
|
|
3
|
+
* before page routing by the site Worker (and by the dev middleware). Pure over its dependencies, so
|
|
4
|
+
* Workers, Node and tests share it. Worker-safe.
|
|
5
|
+
*
|
|
6
|
+
* 1. The asset is found by its URL prefix in the source being served: the published snapshot, or
|
|
7
|
+
* the preview target behind a valid token (invariant 15). Unknown → 404.
|
|
8
|
+
* 2. Taken-down content answers 410 (451 when legal) from every snapshot (invariant 11).
|
|
9
|
+
* 3. A slug or extension that is not the asset's current one → 301 to the canonical URL.
|
|
10
|
+
* 4. External files → 302 to their https URL.
|
|
11
|
+
* 5. Blob content streams with Range (206), conditional requests (`If-None-Match` on the SHA-256),
|
|
12
|
+
* `nosniff`, a type-specific CSP, `Repr-Digest`, and immutable caching when published.
|
|
13
|
+
* 6. `.sha256` → `<sha256> <slug>.<ext>` (GNU `sha256sum -c` format).
|
|
14
|
+
*/
|
|
15
|
+
import type { Asset } from "@cloudhal/core-contracts";
|
|
16
|
+
import { type BlobRange, type BlobStore } from "@cloudhal/storage";
|
|
17
|
+
export interface MediaRouteDeps {
|
|
18
|
+
readonly siteId: string;
|
|
19
|
+
/** `published`: immutable, edge-cached, tagged. `preview` and `dev`: `private, no-store`. */
|
|
20
|
+
readonly mode: "published" | "preview" | "dev";
|
|
21
|
+
/** The asset behind a URL prefix in the source being served. */
|
|
22
|
+
assetByPrefix(prefix16: string): Promise<Asset | undefined>;
|
|
23
|
+
/** The content's ledger row (`purged` → 410/451). */
|
|
24
|
+
blobState(sha256: string): Promise<{
|
|
25
|
+
state: string;
|
|
26
|
+
purge?: {
|
|
27
|
+
legal: boolean;
|
|
28
|
+
} | undefined;
|
|
29
|
+
} | undefined>;
|
|
30
|
+
readonly blobs: BlobStore;
|
|
31
|
+
/** Query string kept on redirects (`?cloudhal-preview=…`). */
|
|
32
|
+
readonly keepQuery?: string | undefined;
|
|
33
|
+
}
|
|
34
|
+
export interface MediaRouteMatch {
|
|
35
|
+
readonly prefix: string;
|
|
36
|
+
readonly slug: string;
|
|
37
|
+
readonly ext: string;
|
|
38
|
+
readonly checksum: boolean;
|
|
39
|
+
}
|
|
40
|
+
export declare function matchMediaRoute(pathname: string): MediaRouteMatch | undefined;
|
|
41
|
+
/** One `bytes=` range (RFC 9110 §14.1.2); several ranges, or garbage, are ignored (full body). */
|
|
42
|
+
export declare function parseRangeHeader(header: string | null): BlobRange | undefined;
|
|
43
|
+
/**
|
|
44
|
+
* Answers a media request, or returns `undefined` when the path is not a media URL (it falls
|
|
45
|
+
* through to the site's own routes: `/media` and other paths under it stay usable, invariant 12).
|
|
46
|
+
*/
|
|
47
|
+
export declare function serveMediaRequest(request: Request, deps: MediaRouteDeps): Promise<Response | undefined>;
|