@cube-drone/marquee-react-renderer 0.4.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 +373 -0
- package/README.md +163 -0
- package/dist/Marquee.d.ts +62 -0
- package/dist/Marquee.js +224 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +15 -0
- package/dist/locate.d.ts +9 -0
- package/dist/locate.js +71 -0
- package/dist/render.d.ts +28 -0
- package/dist/render.js +593 -0
- package/package.json +56 -0
- package/src/Marquee.ts +356 -0
- package/src/index.ts +26 -0
- package/src/locate.ts +76 -0
- package/src/render.ts +782 -0
package/dist/render.js
ADDED
|
@@ -0,0 +1,593 @@
|
|
|
1
|
+
// AST -> React elements. The interactive sibling of @cube-drone/marquee-html-renderer:
|
|
2
|
+
// same mq-* class contract, same Profile socket, same spec obligations - but
|
|
3
|
+
// the tree is built from React elements, never from an HTML string, so the
|
|
4
|
+
// "AST is the contract" rule holds by construction (there is no innerHTML
|
|
5
|
+
// path for author bytes, not even a tempting one).
|
|
6
|
+
//
|
|
7
|
+
// The one place raw HTML is injected is embedder-trusted hook output
|
|
8
|
+
// (Profile.turbolink / .directive / .span return HTML strings, as the static
|
|
9
|
+
// renderer's socket requires). That is embedder code, exactly like a plugin -
|
|
10
|
+
// author bytes only ever reach it as a target string, which the hook escapes.
|
|
11
|
+
// Pass React-returning `hooks` instead to avoid the string path entirely.
|
|
12
|
+
import { createElement as h, useState } from "react";
|
|
13
|
+
import { FONTS, } from "@cube-drone/marquee-html-renderer";
|
|
14
|
+
/** Effects whose elements the visibility observer watches, and which the
|
|
15
|
+
* skip gesture stills. Named, closed: the vocabulary decides, not the DOM. */
|
|
16
|
+
const ANIMATED = new Set([
|
|
17
|
+
"marquee",
|
|
18
|
+
"blink",
|
|
19
|
+
"rainbow",
|
|
20
|
+
"bounce",
|
|
21
|
+
"jitter",
|
|
22
|
+
"wave",
|
|
23
|
+
"rubber",
|
|
24
|
+
"typewriter",
|
|
25
|
+
"fadein",
|
|
26
|
+
]);
|
|
27
|
+
export const ANIM_CLASS = "mq-anim";
|
|
28
|
+
// -- validation gates (mirrors the static renderer: closed value grammars,
|
|
29
|
+
// failures degrade, never emit)
|
|
30
|
+
const HEX_OR_TOKEN = /^#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6})$|^[a-z][a-z0-9-]{0,31}$/;
|
|
31
|
+
const TOKEN = /^[a-z][a-z0-9-]{0,31}$/;
|
|
32
|
+
const COUNT = /^[0-9]{1,4}$/;
|
|
33
|
+
function isColorValue(v) {
|
|
34
|
+
return v !== undefined && HEX_OR_TOKEN.test(v);
|
|
35
|
+
}
|
|
36
|
+
function isToken(v) {
|
|
37
|
+
return v !== undefined && TOKEN.test(v);
|
|
38
|
+
}
|
|
39
|
+
const MEDIA_SIZE_TOKENS = {
|
|
40
|
+
small: "10rem",
|
|
41
|
+
medium: "20rem",
|
|
42
|
+
large: "32rem",
|
|
43
|
+
full: "100%",
|
|
44
|
+
};
|
|
45
|
+
function mediaSize(v) {
|
|
46
|
+
if (v === undefined) {
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
const token = MEDIA_SIZE_TOKENS[v];
|
|
50
|
+
if (token !== undefined) {
|
|
51
|
+
return token;
|
|
52
|
+
}
|
|
53
|
+
if (/^[0-9]{1,4}$/.test(v)) {
|
|
54
|
+
const n = Number(v);
|
|
55
|
+
return n >= 1 && n <= 4096 ? `${n}px` : null;
|
|
56
|
+
}
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
/** A URL safe inside a CSS url('...') token: author bytes must not write
|
|
60
|
+
* CSS, even in their own style attribute. */
|
|
61
|
+
function cssUrl(url) {
|
|
62
|
+
return encodeURI(url).replaceAll("'", "%27").replaceAll("(", "%28").replaceAll(")", "%29");
|
|
63
|
+
}
|
|
64
|
+
/** Style knobs -> React style object (custom properties are legal keys). */
|
|
65
|
+
function styleVars(attrs, profile) {
|
|
66
|
+
const style = {};
|
|
67
|
+
if (isColorValue(attrs["color"])) {
|
|
68
|
+
style["--mq-color"] = attrs["color"];
|
|
69
|
+
}
|
|
70
|
+
const bg = attrs["background"];
|
|
71
|
+
if (bg !== undefined && bg.startsWith("tile:")) {
|
|
72
|
+
const media = profile.media(bg.slice("tile:".length));
|
|
73
|
+
if (media !== null && media.kind === "image") {
|
|
74
|
+
style["--mq-bg-tile"] = `url('${cssUrl(media.url)}')`;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
else if (isColorValue(bg)) {
|
|
78
|
+
style["--mq-bg"] = bg;
|
|
79
|
+
}
|
|
80
|
+
return style;
|
|
81
|
+
}
|
|
82
|
+
function schemeClass(attrs) {
|
|
83
|
+
return isToken(attrs["scheme"]) ? ` mq-scheme-${attrs["scheme"]}` : "";
|
|
84
|
+
}
|
|
85
|
+
function fontClass(attrs) {
|
|
86
|
+
const value = attrs["font"];
|
|
87
|
+
return value !== undefined && FONTS[value] !== undefined ? ` mq-font-${value}` : "";
|
|
88
|
+
}
|
|
89
|
+
/** Element props shared by every node-backed element: the ref that registers
|
|
90
|
+
* it, and (when positions are known) its source extent - which is what makes
|
|
91
|
+
* click-in-preview -> jump-in-editor a one-liner for the host. */
|
|
92
|
+
function nodeProps(node, ctx, extra = {}) {
|
|
93
|
+
const props = { ...extra, ref: ctx.register(node) };
|
|
94
|
+
const span = ctx.spans?.get(node);
|
|
95
|
+
if (span !== undefined) {
|
|
96
|
+
props["data-mq-start"] = span.start;
|
|
97
|
+
props["data-mq-end"] = span.end;
|
|
98
|
+
}
|
|
99
|
+
return props;
|
|
100
|
+
}
|
|
101
|
+
/** Trusted embedder HTML (hook output), never author bytes. */
|
|
102
|
+
function trustedHtml(tag, html, key) {
|
|
103
|
+
return h(tag, { key, dangerouslySetInnerHTML: { __html: html } });
|
|
104
|
+
}
|
|
105
|
+
export function renderChildren(nodes, ctx) {
|
|
106
|
+
return nodes.map((node, i) => renderNode(node, ctx, String(i)));
|
|
107
|
+
}
|
|
108
|
+
/** Asides flush just below the block that triggered them - part of regular
|
|
109
|
+
* flow, no floats, no popups (the same treatment as the static renderer). */
|
|
110
|
+
function withNotes(ctx, element, key) {
|
|
111
|
+
if (ctx.note.pending.length === 0) {
|
|
112
|
+
return element;
|
|
113
|
+
}
|
|
114
|
+
const notes = ctx.note.pending;
|
|
115
|
+
ctx.note.pending = [];
|
|
116
|
+
return h("div", { key, style: { display: "contents" } }, element, h("aside", { key: "notes", className: "mq-notes" }, notes.map((n, i) => h("p", { key: i, className: "mq-note" }, n))));
|
|
117
|
+
}
|
|
118
|
+
export function renderNode(node, ctx, key) {
|
|
119
|
+
switch (node.type) {
|
|
120
|
+
case "document":
|
|
121
|
+
return h("div", nodeProps(node, ctx, { key, className: "mq-doc" }), ...renderChildren(node.children, ctx));
|
|
122
|
+
case "paragraph": {
|
|
123
|
+
const el = h("p", nodeProps(node, ctx, { key }), ...renderChildren(node.children, ctx));
|
|
124
|
+
return withNotes(ctx, el, key);
|
|
125
|
+
}
|
|
126
|
+
case "heading": {
|
|
127
|
+
const inner = renderChildren(node.children, ctx);
|
|
128
|
+
// HTML's ladder stops at h6; 7 and 8 keep heading semantics via ARIA.
|
|
129
|
+
const el = node.level <= 6
|
|
130
|
+
? h(`h${node.level}`, nodeProps(node, ctx, { key }), ...inner)
|
|
131
|
+
: h("p", nodeProps(node, ctx, {
|
|
132
|
+
key,
|
|
133
|
+
className: `mq-h${node.level}`,
|
|
134
|
+
role: "heading",
|
|
135
|
+
"aria-level": node.level,
|
|
136
|
+
}), ...inner);
|
|
137
|
+
return withNotes(ctx, el, key);
|
|
138
|
+
}
|
|
139
|
+
case "code_block": {
|
|
140
|
+
const lang = infoToken(node.info);
|
|
141
|
+
return h("pre", nodeProps(node, ctx, { key, className: "mq-code" }), h("code", { className: lang === null ? undefined : `language-${lang}` }, node.text === "" ? null : `${node.text}\n`));
|
|
142
|
+
}
|
|
143
|
+
case "blockquote":
|
|
144
|
+
return h("blockquote", nodeProps(node, ctx, { key }), ...renderChildren(node.children, ctx));
|
|
145
|
+
case "list":
|
|
146
|
+
return h(node.ordered ? "ol" : "ul", nodeProps(node, ctx, { key }), ...renderChildren(node.children, ctx));
|
|
147
|
+
case "list_item":
|
|
148
|
+
return h("li", nodeProps(node, ctx, { key }), ...renderChildren(node.children, ctx));
|
|
149
|
+
case "thematic_break":
|
|
150
|
+
return h("hr", nodeProps(node, ctx, { key }));
|
|
151
|
+
case "directive":
|
|
152
|
+
return directive(node, ctx, key);
|
|
153
|
+
case "invalid_directive":
|
|
154
|
+
return h("div", nodeProps(node, ctx, { key, className: "mq-invalid", "data-reason": node.reason }));
|
|
155
|
+
case "comment":
|
|
156
|
+
return null; // the anti-shrug: correct rendering is absence
|
|
157
|
+
case "text":
|
|
158
|
+
return node.value;
|
|
159
|
+
case "emphasis":
|
|
160
|
+
return h("em", nodeProps(node, ctx, { key }), ...renderChildren(node.children, ctx));
|
|
161
|
+
case "strong":
|
|
162
|
+
return h("strong", nodeProps(node, ctx, { key }), ...renderChildren(node.children, ctx));
|
|
163
|
+
case "strikethrough":
|
|
164
|
+
return h("del", nodeProps(node, ctx, { key }), ...renderChildren(node.children, ctx));
|
|
165
|
+
case "code_span":
|
|
166
|
+
return h("code", nodeProps(node, ctx, { key }), node.text);
|
|
167
|
+
case "link": {
|
|
168
|
+
const inner = renderChildren(node.children, ctx);
|
|
169
|
+
return ctx.profile.linkAllowed(node.target)
|
|
170
|
+
? h("a", nodeProps(node, ctx, { key, href: node.target }), ...inner)
|
|
171
|
+
: h("span", nodeProps(node, ctx, { key, className: "mq-blocked" }), ...inner);
|
|
172
|
+
}
|
|
173
|
+
case "embed":
|
|
174
|
+
return embed(node, ctx, key);
|
|
175
|
+
case "turbolink":
|
|
176
|
+
return turbolink(node, node.target, undefined, ctx, key);
|
|
177
|
+
case "span":
|
|
178
|
+
return span(node, ctx, key);
|
|
179
|
+
case "emoji": {
|
|
180
|
+
const resolved = ctx.profile.emoji(node.slug);
|
|
181
|
+
if (resolved !== null && typeof resolved === "object") {
|
|
182
|
+
return h("img", nodeProps(node, ctx, {
|
|
183
|
+
key,
|
|
184
|
+
className: "mq-emoji",
|
|
185
|
+
src: resolved.image,
|
|
186
|
+
alt: resolved.alt ?? `:${node.slug}:`,
|
|
187
|
+
loading: "lazy",
|
|
188
|
+
}));
|
|
189
|
+
}
|
|
190
|
+
return resolved ?? `:${node.slug}:`;
|
|
191
|
+
}
|
|
192
|
+
case "hard_break":
|
|
193
|
+
return h("br", nodeProps(node, ctx, { key }));
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
/** A spoiler that reveals on a deliberate click (or Enter/Space), and
|
|
197
|
+
* stays revealed. Keyboard- and screen-reader-accessible: it announces
|
|
198
|
+
* itself as an expandable button. The `rest` carries nodeProps (the
|
|
199
|
+
* register ref + source-position data attrs), so scroll sync still finds
|
|
200
|
+
* it. */
|
|
201
|
+
function Spoiler({ children, tag = "span", ...rest }) {
|
|
202
|
+
const [revealed, setRevealed] = useState(false);
|
|
203
|
+
const reveal = () => setRevealed(true);
|
|
204
|
+
return h(tag, {
|
|
205
|
+
...rest,
|
|
206
|
+
role: "button",
|
|
207
|
+
tabIndex: 0,
|
|
208
|
+
"aria-expanded": revealed,
|
|
209
|
+
"aria-label": revealed ? undefined : "spoiler, click to reveal",
|
|
210
|
+
"data-mq-revealed": revealed ? "" : undefined,
|
|
211
|
+
onClick: reveal,
|
|
212
|
+
onKeyDown: (e) => {
|
|
213
|
+
if (e.key === "Enter" || e.key === " ") {
|
|
214
|
+
e.preventDefault();
|
|
215
|
+
reveal();
|
|
216
|
+
}
|
|
217
|
+
},
|
|
218
|
+
}, children);
|
|
219
|
+
}
|
|
220
|
+
function infoToken(info) {
|
|
221
|
+
if (info === undefined) {
|
|
222
|
+
return null;
|
|
223
|
+
}
|
|
224
|
+
const first = info.split(/[ \t]/, 1)[0] ?? "";
|
|
225
|
+
return /^[A-Za-z0-9_+-]{1,32}$/.test(first) ? first : null;
|
|
226
|
+
}
|
|
227
|
+
function embed(node, ctx, key) {
|
|
228
|
+
const media = ctx.profile.media(node.target);
|
|
229
|
+
if (media === null) {
|
|
230
|
+
return h("span", nodeProps(node, ctx, { key, className: "mq-placeholder" }), node.alt === "" ? node.target : node.alt);
|
|
231
|
+
}
|
|
232
|
+
const common = { key, className: "mq-embed" };
|
|
233
|
+
if (media.kind === "image") {
|
|
234
|
+
return h("img", nodeProps(node, ctx, { ...common, src: media.url, alt: node.alt, loading: "lazy" }));
|
|
235
|
+
}
|
|
236
|
+
if (media.kind === "audio") {
|
|
237
|
+
return h("audio", nodeProps(node, ctx, { ...common, src: media.url, controls: true }));
|
|
238
|
+
}
|
|
239
|
+
return h("video", nodeProps(node, ctx, { ...common, src: media.url, controls: true }));
|
|
240
|
+
}
|
|
241
|
+
const TURBOLINK_LEVELS = new Set(["full", "title", "bare"]);
|
|
242
|
+
function turbolink(node, target, levelAttr, ctx, key) {
|
|
243
|
+
if (!ctx.profile.linkAllowed(target)) {
|
|
244
|
+
return h("p", nodeProps(node, ctx, { key, className: "mq-turbolink" }), target);
|
|
245
|
+
}
|
|
246
|
+
const level = levelAttr !== undefined && TURBOLINK_LEVELS.has(levelAttr)
|
|
247
|
+
? levelAttr
|
|
248
|
+
: ctx.profile.turbolinkLevel(target);
|
|
249
|
+
if (level !== "bare") {
|
|
250
|
+
const react = ctx.hooks.turbolink?.(target, level) ?? null;
|
|
251
|
+
const html = react === null ? ctx.profile.turbolink(target, level) : null;
|
|
252
|
+
if (react !== null || html !== null) {
|
|
253
|
+
// Enrichment augments, never replaces: the wrapper carries the
|
|
254
|
+
// original link, so no plugin can eat it - not even by accident.
|
|
255
|
+
return h("div", nodeProps(node, ctx, { key, className: "mq-turbolink mq-turbolink-rich" }), react ?? trustedHtml("span", html, "rich"), h("a", { key: "src", className: "mq-turbolink-source", href: target }, target));
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
return h("p", nodeProps(node, ctx, { key, className: "mq-turbolink" }), h("a", { href: target }, target));
|
|
259
|
+
}
|
|
260
|
+
function directive(node, ctx, key) {
|
|
261
|
+
const { name, attrs } = node;
|
|
262
|
+
const kids = renderChildren(node.children, ctx);
|
|
263
|
+
const custom = ctx.hooks.directive?.(name, attrs, kids);
|
|
264
|
+
if (custom !== undefined && custom !== null) {
|
|
265
|
+
return h("div", nodeProps(node, ctx, { key, style: { display: "contents" } }), custom);
|
|
266
|
+
}
|
|
267
|
+
const customHtml = ctx.hooks.directive === undefined ? ctx.profile.directive(name, attrs, "") : null;
|
|
268
|
+
if (customHtml !== null) {
|
|
269
|
+
return h("div", nodeProps(node, ctx, { key, style: { display: "contents" }, dangerouslySetInnerHTML: { __html: customHtml } }));
|
|
270
|
+
}
|
|
271
|
+
switch (name) {
|
|
272
|
+
case "meta":
|
|
273
|
+
return h("div", nodeProps(node, ctx, { key, style: { display: "contents" } }), ...kids);
|
|
274
|
+
case "page": {
|
|
275
|
+
const layout = isToken(attrs["layout"]) ? ` mq-layout-${attrs["layout"]}` : "";
|
|
276
|
+
return h("div", nodeProps(node, ctx, {
|
|
277
|
+
key,
|
|
278
|
+
className: `mq-page${layout}${schemeClass(attrs)}${fontClass(attrs)}`,
|
|
279
|
+
style: styleVars(attrs, ctx.profile),
|
|
280
|
+
}), ...kids);
|
|
281
|
+
}
|
|
282
|
+
case "section":
|
|
283
|
+
return h("section", nodeProps(node, ctx, {
|
|
284
|
+
key,
|
|
285
|
+
className: `mq-section${schemeClass(attrs)}${fontClass(attrs)}`,
|
|
286
|
+
"data-slot": isToken(attrs["slot"]) ? attrs["slot"] : undefined,
|
|
287
|
+
style: styleVars(attrs, ctx.profile),
|
|
288
|
+
}), ...kids);
|
|
289
|
+
case "turbolink":
|
|
290
|
+
if (attrs["target"] !== undefined) {
|
|
291
|
+
return turbolink(node, attrs["target"], attrs["level"], ctx, key);
|
|
292
|
+
}
|
|
293
|
+
break;
|
|
294
|
+
case "media": {
|
|
295
|
+
const style = {};
|
|
296
|
+
const w = mediaSize(attrs["width"]);
|
|
297
|
+
const hh = mediaSize(attrs["height"]);
|
|
298
|
+
if (w !== null) {
|
|
299
|
+
style["--mq-media-w"] = w;
|
|
300
|
+
}
|
|
301
|
+
if (hh !== null) {
|
|
302
|
+
style["--mq-media-h"] = hh;
|
|
303
|
+
}
|
|
304
|
+
return h("div", nodeProps(node, ctx, { key, className: "mq-media", style }), ...kids);
|
|
305
|
+
}
|
|
306
|
+
case "table":
|
|
307
|
+
return renderTable(node, ctx, key);
|
|
308
|
+
case "center":
|
|
309
|
+
case "right":
|
|
310
|
+
case "left":
|
|
311
|
+
return h("div", nodeProps(node, ctx, { key, className: `mq-${name}` }), ...kids);
|
|
312
|
+
case "spoiler":
|
|
313
|
+
// Block spoiler: same click-to-reveal as the inline one, on a div.
|
|
314
|
+
return h(Spoiler, nodeProps(node, ctx, { key, tag: "div", className: "mq-spoiler mq-spoiler-block" }), ...kids);
|
|
315
|
+
}
|
|
316
|
+
// Unknown vocabulary: a container renders its children with an affordance;
|
|
317
|
+
// a leaf renders the inert placeholder. Never eat authored content.
|
|
318
|
+
return node.children.length > 0
|
|
319
|
+
? h("div", nodeProps(node, ctx, { key, className: "mq-unknown", "data-directive": name }), ...kids)
|
|
320
|
+
: h("div", nodeProps(node, ctx, { key, className: "mq-placeholder", "data-directive": name }));
|
|
321
|
+
}
|
|
322
|
+
/** :::table - paragraph-rows, [c] cell spans (SPEC.md, "Tables"). */
|
|
323
|
+
function renderTable(node, ctx, key) {
|
|
324
|
+
const header = node.attrs["header"];
|
|
325
|
+
const headRow = header === "row" || header === "both";
|
|
326
|
+
const headCol = header === "column" || header === "both";
|
|
327
|
+
const rows = [];
|
|
328
|
+
node.children.forEach((child, ri) => {
|
|
329
|
+
const cells = [];
|
|
330
|
+
if (child.type === "paragraph") {
|
|
331
|
+
let loose = [];
|
|
332
|
+
const flush = () => {
|
|
333
|
+
if (loose.some((n) => n.type !== "text" || n.value.trim() !== "")) {
|
|
334
|
+
cells.push(renderChildren(loose, ctx));
|
|
335
|
+
}
|
|
336
|
+
loose = [];
|
|
337
|
+
};
|
|
338
|
+
for (const inline of child.children) {
|
|
339
|
+
if (inline.type === "span" && inline.name === "c") {
|
|
340
|
+
flush();
|
|
341
|
+
cells.push(renderChildren(inline.children, ctx));
|
|
342
|
+
}
|
|
343
|
+
else {
|
|
344
|
+
loose.push(inline);
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
flush();
|
|
348
|
+
}
|
|
349
|
+
else {
|
|
350
|
+
cells.push([renderNode(child, ctx, "0")]);
|
|
351
|
+
}
|
|
352
|
+
const isHeadRow = headRow && rows.length === 0;
|
|
353
|
+
rows.push(h("tr", { key: ri }, cells.map((cell, ci) => isHeadRow || (headCol && ci === 0)
|
|
354
|
+
? h("th", { key: ci, scope: isHeadRow ? "col" : "row" }, ...cell)
|
|
355
|
+
: h("td", { key: ci }, ...cell))));
|
|
356
|
+
});
|
|
357
|
+
const table = h("table", nodeProps(node, ctx, { key, className: "mq-table" }), h("tbody", null, rows));
|
|
358
|
+
return withNotes(ctx, table, key);
|
|
359
|
+
}
|
|
360
|
+
function sizeRung(value, inner, node, ctx, key) {
|
|
361
|
+
// <font> is deliberate: presentational attributes are outside CSP's
|
|
362
|
+
// jurisdiction, so size survives with no stylesheet at all.
|
|
363
|
+
return h("font", nodeProps(node, ctx, { key, className: `mq-size-${value}`, size: value }), ...inner);
|
|
364
|
+
}
|
|
365
|
+
function span(node, ctx, key) {
|
|
366
|
+
const { name, attrs } = node;
|
|
367
|
+
const kids = renderChildren(node.children, ctx);
|
|
368
|
+
const custom = ctx.hooks.span?.(name, attrs, kids);
|
|
369
|
+
if (custom !== undefined && custom !== null) {
|
|
370
|
+
return h("span", nodeProps(node, ctx, { key, style: { display: "contents" } }), custom);
|
|
371
|
+
}
|
|
372
|
+
const customHtml = ctx.hooks.span === undefined ? ctx.profile.span(name, attrs, "") : null;
|
|
373
|
+
if (customHtml !== null) {
|
|
374
|
+
return h("span", nodeProps(node, ctx, { key, dangerouslySetInnerHTML: { __html: customHtml } }));
|
|
375
|
+
}
|
|
376
|
+
switch (name) {
|
|
377
|
+
case "sup":
|
|
378
|
+
return h("sup", nodeProps(node, ctx, { key }), ...kids);
|
|
379
|
+
case "sub":
|
|
380
|
+
return h("sub", nodeProps(node, ctx, { key }), ...kids);
|
|
381
|
+
case "small":
|
|
382
|
+
return h("small", nodeProps(node, ctx, { key }), ...kids);
|
|
383
|
+
case "big":
|
|
384
|
+
return h("big", nodeProps(node, ctx, { key }), ...kids);
|
|
385
|
+
case "spoiler":
|
|
386
|
+
// Click-to-reveal (the static renderer can only blur-until-hover).
|
|
387
|
+
// Same mq-spoiler class; .mq-root in the shared CSS gates hover off
|
|
388
|
+
// and honors the data-mq-revealed this component sets on click.
|
|
389
|
+
return h(Spoiler, nodeProps(node, ctx, { key, className: "mq-spoiler" }), ...kids);
|
|
390
|
+
case "size": {
|
|
391
|
+
const value = attrs["size"];
|
|
392
|
+
return value !== undefined && /^[1-7]$/.test(value)
|
|
393
|
+
? sizeRung(value, kids, node, ctx, key)
|
|
394
|
+
: h("span", nodeProps(node, ctx, { key, style: { display: "contents" } }), ...kids);
|
|
395
|
+
}
|
|
396
|
+
case "teeny":
|
|
397
|
+
return sizeRung("1", kids, node, ctx, key);
|
|
398
|
+
case "tiny":
|
|
399
|
+
return sizeRung("2", kids, node, ctx, key);
|
|
400
|
+
case "huge":
|
|
401
|
+
return sizeRung("6", kids, node, ctx, key);
|
|
402
|
+
case "enormous":
|
|
403
|
+
return sizeRung("7", kids, node, ctx, key);
|
|
404
|
+
case "font": {
|
|
405
|
+
const value = attrs["font"];
|
|
406
|
+
const face = value !== undefined ? FONTS[value] : undefined;
|
|
407
|
+
return face !== undefined
|
|
408
|
+
? h("font", nodeProps(node, ctx, { key, className: `mq-font-${value}`, face }), ...kids)
|
|
409
|
+
: h("span", nodeProps(node, ctx, { key, style: { display: "contents" } }), ...kids);
|
|
410
|
+
}
|
|
411
|
+
case "color": {
|
|
412
|
+
const value = attrs["color"];
|
|
413
|
+
return isColorValue(value)
|
|
414
|
+
? h("font", nodeProps(node, ctx, {
|
|
415
|
+
key,
|
|
416
|
+
className: "mq-color",
|
|
417
|
+
color: value,
|
|
418
|
+
style: { "--mq-color": value },
|
|
419
|
+
}), ...kids)
|
|
420
|
+
: h("span", nodeProps(node, ctx, { key, style: { display: "contents" } }), ...kids);
|
|
421
|
+
}
|
|
422
|
+
case "sidenote":
|
|
423
|
+
case "aside":
|
|
424
|
+
case "footnote": {
|
|
425
|
+
// A numbered mark in the flow; the note flushes below the block.
|
|
426
|
+
ctx.note.n += 1;
|
|
427
|
+
const n = ctx.note.n;
|
|
428
|
+
ctx.note.pending.push(h("span", { key: `note-${n}` }, h("span", { className: "mq-note-num" }, n), ...kids));
|
|
429
|
+
return h("sup", nodeProps(node, ctx, { key, className: "mq-noteref" }), n);
|
|
430
|
+
}
|
|
431
|
+
case "marquee": {
|
|
432
|
+
const dir = attrs["direction"] === "right" ? "right" : undefined;
|
|
433
|
+
const style = {};
|
|
434
|
+
if (attrs["speed"] !== undefined && COUNT.test(attrs["speed"])) {
|
|
435
|
+
style["--mq-speed"] = attrs["speed"];
|
|
436
|
+
}
|
|
437
|
+
return h("span", nodeProps(node, ctx, {
|
|
438
|
+
key,
|
|
439
|
+
className: `mq-marquee ${ANIM_CLASS}`,
|
|
440
|
+
"data-direction": dir,
|
|
441
|
+
style,
|
|
442
|
+
}), h("span", { className: "mq-marquee-inner" }, ...kids));
|
|
443
|
+
}
|
|
444
|
+
case "blink":
|
|
445
|
+
case "rainbow":
|
|
446
|
+
case "bounce":
|
|
447
|
+
case "jitter":
|
|
448
|
+
case "wave":
|
|
449
|
+
case "rubber":
|
|
450
|
+
case "typewriter":
|
|
451
|
+
case "fadein":
|
|
452
|
+
return effect(node, ctx, key);
|
|
453
|
+
}
|
|
454
|
+
// Unknown span: pure shrug, children as plain content.
|
|
455
|
+
return h("span", nodeProps(node, ctx, { key, style: { display: "contents" } }), ...kids);
|
|
456
|
+
}
|
|
457
|
+
// -- effects, including the per-unit (by=letter / by=word) machinery. The
|
|
458
|
+
// offsets mirror the static renderer's exactly, so a document animates the
|
|
459
|
+
// same in both - deterministic, never random.
|
|
460
|
+
const SEGMENTERS = {
|
|
461
|
+
letter: new Intl.Segmenter("en", { granularity: "grapheme" }),
|
|
462
|
+
word: new Intl.Segmenter("en", { granularity: "word" }),
|
|
463
|
+
};
|
|
464
|
+
const MAX_SPLIT_UNITS = 400;
|
|
465
|
+
const MAX_REVEAL_UNITS = 2000;
|
|
466
|
+
function isReveal(effectName) {
|
|
467
|
+
return effectName === "typewriter" || effectName === "fadein";
|
|
468
|
+
}
|
|
469
|
+
function revealStep(speedAttr, dflt) {
|
|
470
|
+
const speed = speedAttr !== undefined && COUNT.test(speedAttr) && Number(speedAttr) > 0
|
|
471
|
+
? Number(speedAttr)
|
|
472
|
+
: dflt;
|
|
473
|
+
return Math.round(1000 / speed) / 1000;
|
|
474
|
+
}
|
|
475
|
+
function gcd(a, b) {
|
|
476
|
+
return b === 0 ? a : gcd(b, a % b);
|
|
477
|
+
}
|
|
478
|
+
function scatterStride(total) {
|
|
479
|
+
let stride = Math.max(1, Math.round(total * 0.618));
|
|
480
|
+
while (stride < total && gcd(stride, total) !== 1) {
|
|
481
|
+
stride += 1;
|
|
482
|
+
}
|
|
483
|
+
return gcd(stride, total) === 1 ? stride : 1;
|
|
484
|
+
}
|
|
485
|
+
function unitOffset(state) {
|
|
486
|
+
if (isReveal(state.effect)) {
|
|
487
|
+
const o = state.phase === "scatter" ? (state.i * scatterStride(state.total)) % state.total : state.i;
|
|
488
|
+
return String(o);
|
|
489
|
+
}
|
|
490
|
+
let o;
|
|
491
|
+
if (state.phase === "scatter") {
|
|
492
|
+
o = ((state.i * 7919) % 101) / 101;
|
|
493
|
+
}
|
|
494
|
+
else {
|
|
495
|
+
switch (state.effect) {
|
|
496
|
+
case "rainbow":
|
|
497
|
+
o = state.i / state.total;
|
|
498
|
+
break;
|
|
499
|
+
case "bounce":
|
|
500
|
+
o = (state.i % 6) / 6;
|
|
501
|
+
break;
|
|
502
|
+
default:
|
|
503
|
+
o = (state.i % 8) / 8;
|
|
504
|
+
break;
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
return String(Math.round(o * 1000) / 1000);
|
|
508
|
+
}
|
|
509
|
+
function isUnit(seg, by) {
|
|
510
|
+
return by === "word" ? seg.isWordLike === true : !/^\s+$/.test(seg.segment);
|
|
511
|
+
}
|
|
512
|
+
function countUnits(nodes, by) {
|
|
513
|
+
let n = 0;
|
|
514
|
+
for (const node of nodes) {
|
|
515
|
+
if (node.type === "text") {
|
|
516
|
+
for (const seg of SEGMENTERS[by].segment(node.value)) {
|
|
517
|
+
if (isUnit(seg, by)) {
|
|
518
|
+
n += 1;
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
else if (node.type === "emphasis" || node.type === "strong" || node.type === "strikethrough") {
|
|
523
|
+
n += countUnits(node.children, by);
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
return n;
|
|
527
|
+
}
|
|
528
|
+
function splitRender(nodes, ctx, state) {
|
|
529
|
+
const out = [];
|
|
530
|
+
nodes.forEach((node, ni) => {
|
|
531
|
+
if (node.type === "text") {
|
|
532
|
+
for (const seg of SEGMENTERS[state.by].segment(node.value)) {
|
|
533
|
+
if (!isUnit(seg, state.by)) {
|
|
534
|
+
out.push(seg.segment); // spaces and punctuation ride along
|
|
535
|
+
continue;
|
|
536
|
+
}
|
|
537
|
+
const o = unitOffset(state);
|
|
538
|
+
state.i += 1;
|
|
539
|
+
out.push(h("span", { key: `${ni}-${state.i}`, className: "mq-l", style: { "--mq-o": o } }, seg.segment));
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
else if (node.type === "emphasis") {
|
|
543
|
+
out.push(h("em", { key: ni }, ...splitRender(node.children, ctx, state)));
|
|
544
|
+
}
|
|
545
|
+
else if (node.type === "strong") {
|
|
546
|
+
out.push(h("strong", { key: ni }, ...splitRender(node.children, ctx, state)));
|
|
547
|
+
}
|
|
548
|
+
else if (node.type === "strikethrough") {
|
|
549
|
+
out.push(h("del", { key: ni }, ...splitRender(node.children, ctx, state)));
|
|
550
|
+
}
|
|
551
|
+
else {
|
|
552
|
+
out.push(renderNode(node, ctx, String(ni))); // anything else renders whole
|
|
553
|
+
}
|
|
554
|
+
});
|
|
555
|
+
return out;
|
|
556
|
+
}
|
|
557
|
+
function effect(node, ctx, key) {
|
|
558
|
+
const { name, attrs } = node;
|
|
559
|
+
const byAttr = attrs["by"];
|
|
560
|
+
const splitBy = byAttr === "letter" || byAttr === "word"
|
|
561
|
+
? byAttr
|
|
562
|
+
: name === "typewriter"
|
|
563
|
+
? "letter" // typewriter is per-unit by nature
|
|
564
|
+
: null;
|
|
565
|
+
const style = {};
|
|
566
|
+
if (name === "blink" && attrs["rate"] !== undefined && COUNT.test(attrs["rate"])) {
|
|
567
|
+
style["--mq-rate"] = attrs["rate"];
|
|
568
|
+
}
|
|
569
|
+
if (name === "typewriter") {
|
|
570
|
+
style["--mq-tw-step"] = `${revealStep(attrs["speed"], 14)}s`;
|
|
571
|
+
}
|
|
572
|
+
if (name === "fadein" && splitBy !== null) {
|
|
573
|
+
style["--mq-fi-step"] = `${revealStep(attrs["speed"], 16)}s`;
|
|
574
|
+
}
|
|
575
|
+
const className = `mq-${name} ${ANIM_CLASS}`;
|
|
576
|
+
if (splitBy === null) {
|
|
577
|
+
return h("span", nodeProps(node, ctx, { key, className, style }), ...renderChildren(node.children, ctx));
|
|
578
|
+
}
|
|
579
|
+
const phase = attrs["phase"] === "scatter" || attrs["phase"] === "ramp"
|
|
580
|
+
? attrs["phase"]
|
|
581
|
+
: name === "jitter"
|
|
582
|
+
? "scatter"
|
|
583
|
+
: "ramp";
|
|
584
|
+
const total = countUnits(node.children, splitBy);
|
|
585
|
+
const cap = isReveal(name) ? MAX_REVEAL_UNITS : MAX_SPLIT_UNITS;
|
|
586
|
+
if (total === 0 || total > cap) {
|
|
587
|
+
// Past the cap: the run animates whole. DOM weight discipline.
|
|
588
|
+
return h("span", nodeProps(node, ctx, { key, className, style }), ...renderChildren(node.children, ctx));
|
|
589
|
+
}
|
|
590
|
+
const state = { effect: name, by: splitBy, phase, i: 0, total };
|
|
591
|
+
return h("span", nodeProps(node, ctx, { key, className: `${className} mq-split`, style }), ...splitRender(node.children, ctx, state));
|
|
592
|
+
}
|
|
593
|
+
export { ANIMATED };
|
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cube-drone/marquee-react-renderer",
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"description": "Interactive React renderer for the Marquee markup language: the JS halves of the animation contract (start-on-visible, tap-to-skip) plus source-position scroll sync for side-by-side editors",
|
|
5
|
+
"license": "MPL-2.0",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/cube-drone/marqueemarkup.git",
|
|
9
|
+
"directory": "ts/marquee-react-renderer"
|
|
10
|
+
},
|
|
11
|
+
"type": "module",
|
|
12
|
+
"engines": {
|
|
13
|
+
"node": ">=22.6.0"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"src"
|
|
18
|
+
],
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"access": "public"
|
|
21
|
+
},
|
|
22
|
+
"exports": {
|
|
23
|
+
".": {
|
|
24
|
+
"marquee-src": "./src/index.ts",
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"default": "./dist/index.js"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"scripts": {
|
|
30
|
+
"test": "node --conditions=marquee-src --test test/*.test.ts",
|
|
31
|
+
"check": "tsc --noEmit",
|
|
32
|
+
"build": "tsc -p tsconfig.build.json",
|
|
33
|
+
"prepack": "npm run build",
|
|
34
|
+
"demo": "node --conditions=marquee-src scripts/demo.ts"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"@cube-drone/marquee-html-renderer": "^0.4.0",
|
|
38
|
+
"@cube-drone/marquee-parser": "^0.4.0"
|
|
39
|
+
},
|
|
40
|
+
"peerDependencies": {
|
|
41
|
+
"react": ">=18"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@types/node": "^24.0.0",
|
|
45
|
+
"@types/react": "^19.0.0",
|
|
46
|
+
"@types/react-dom": "^19.0.0",
|
|
47
|
+
"esbuild": "^0.28.1",
|
|
48
|
+
"react": "^19.0.0",
|
|
49
|
+
"react-dom": "^19.0.0",
|
|
50
|
+
"typescript": "^5.9.0",
|
|
51
|
+
"@cube-drone/marquee-css": "^0.4.0",
|
|
52
|
+
"@cube-drone/marquee-fonts": "^0.4.0",
|
|
53
|
+
"@cube-drone/marquee-emoji": "^0.4.0",
|
|
54
|
+
"@cube-drone/marquee-turbolink": "^0.4.0"
|
|
55
|
+
}
|
|
56
|
+
}
|