@fluixi/head 0.1.0-alpha.22 → 1.0.0-alpha.100

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.
@@ -0,0 +1,302 @@
1
+ // src/reconcile.ts
2
+ import { createRoot, createEffect, onCleanup as onCleanup2 } from "@fluixi/reactive/signal";
3
+
4
+ // src/core.ts
5
+ import { getOwner, onCleanup, createSignal } from "@fluixi/reactive/signal";
6
+ function val(v) {
7
+ return typeof v === "function" ? v() : v;
8
+ }
9
+ function robotsToString(r) {
10
+ if (typeof r === "string") return r;
11
+ const out = [];
12
+ out.push(r.index === false ? "noindex" : "index");
13
+ out.push(r.follow === false ? "nofollow" : "follow");
14
+ if (r.noarchive) out.push("noarchive");
15
+ if (r.nosnippet) out.push("nosnippet");
16
+ if (r.noimageindex) out.push("noimageindex");
17
+ if (r.maxSnippet != null) out.push(`max-snippet:${r.maxSnippet}`);
18
+ if (r.maxImagePreview) out.push(`max-image-preview:${r.maxImagePreview}`);
19
+ if (r.maxVideoPreview != null) out.push(`max-video-preview:${r.maxVideoPreview}`);
20
+ return out.join(", ");
21
+ }
22
+ var slot = globalThis[/* @__PURE__ */ Symbol.for("fluixi.head")] ??= {
23
+ active: null,
24
+ scope: null
25
+ };
26
+ function djb2(s) {
27
+ let h = 5381;
28
+ for (let i = 0; i < s.length; i++) h = h * 33 ^ s.charCodeAt(i);
29
+ return (h >>> 0).toString(36);
30
+ }
31
+ function metaKey(a) {
32
+ if (a.charset != null) return "meta:charset";
33
+ if (a.property) return "meta:property:" + a.property;
34
+ if (a.name) return "meta:name:" + a.name;
35
+ if (a["http-equiv"]) return "meta:http-equiv:" + a["http-equiv"];
36
+ return "meta:" + djb2(JSON.stringify(a));
37
+ }
38
+ function pushImage(out, img) {
39
+ const o = typeof img === "string" ? { url: img } : img;
40
+ out.push({ tag: "meta", key: "meta:property:og:image:" + o.url, attrs: { property: "og:image", content: o.url } });
41
+ if (o.secureUrl) out.push({ tag: "meta", key: "meta:property:og:image:secure:" + o.url, attrs: { property: "og:image:secure_url", content: o.secureUrl } });
42
+ if (o.type) out.push({ tag: "meta", key: "meta:property:og:image:type:" + o.url, attrs: { property: "og:image:type", content: o.type } });
43
+ if (o.width != null) out.push({ tag: "meta", key: "meta:property:og:image:width:" + o.url, attrs: { property: "og:image:width", content: String(o.width) } });
44
+ if (o.height != null) out.push({ tag: "meta", key: "meta:property:og:image:height:" + o.url, attrs: { property: "og:image:height", content: String(o.height) } });
45
+ if (o.alt) out.push({ tag: "meta", key: "meta:property:og:image:alt:" + o.url, attrs: { property: "og:image:alt", content: o.alt } });
46
+ }
47
+ function expandOG(og, out) {
48
+ const m = (p, c) => {
49
+ if (c != null) out.push({ tag: "meta", key: "meta:property:og:" + p, attrs: { property: "og:" + p, content: c } });
50
+ };
51
+ m("title", og.title);
52
+ m("description", og.description);
53
+ m("type", og.type);
54
+ m("url", og.url);
55
+ m("site_name", og.siteName);
56
+ m("locale", og.locale);
57
+ for (const l of og.localeAlternate ?? []) out.push({ tag: "meta", key: "meta:property:og:locale:alt:" + l, attrs: { property: "og:locale:alternate", content: l } });
58
+ if (og.image != null) {
59
+ const imgs = Array.isArray(og.image) ? og.image : [og.image];
60
+ for (const i of imgs) pushImage(out, i);
61
+ }
62
+ if (og.video) {
63
+ const v = typeof og.video === "string" ? { url: og.video } : og.video;
64
+ m("video", v.url);
65
+ if ("type" in v && v.type) m("video:type", v.type);
66
+ if ("width" in v && v.width != null) m("video:width", String(v.width));
67
+ if ("height" in v && v.height != null) m("video:height", String(v.height));
68
+ }
69
+ if (og.audio) {
70
+ const a = typeof og.audio === "string" ? { url: og.audio } : og.audio;
71
+ m("audio", a.url);
72
+ if ("type" in a && a.type) m("audio:type", a.type);
73
+ }
74
+ if (og.article) {
75
+ const ar = og.article;
76
+ m("article:published_time", ar.publishedTime);
77
+ m("article:modified_time", ar.modifiedTime);
78
+ m("article:expiration_time", ar.expirationTime);
79
+ m("article:section", ar.section);
80
+ for (const au of [].concat(ar.author ?? [])) out.push({ tag: "meta", key: "meta:property:og:article:author:" + au, attrs: { property: "article:author", content: au } });
81
+ for (const t of [].concat(ar.tag ?? [])) out.push({ tag: "meta", key: "meta:property:og:article:tag:" + t, attrs: { property: "article:tag", content: t } });
82
+ }
83
+ if (og.book) {
84
+ const b = og.book;
85
+ m("book:isbn", b.isbn);
86
+ m("book:release_date", b.releaseDate);
87
+ for (const au of [].concat(b.author ?? [])) out.push({ tag: "meta", key: "meta:property:og:book:author:" + au, attrs: { property: "book:author", content: au } });
88
+ for (const t of [].concat(b.tag ?? [])) out.push({ tag: "meta", key: "meta:property:og:book:tag:" + t, attrs: { property: "book:tag", content: t } });
89
+ }
90
+ if (og.profile) {
91
+ const p = og.profile;
92
+ m("profile:first_name", p.firstName);
93
+ m("profile:last_name", p.lastName);
94
+ m("profile:username", p.username);
95
+ m("profile:gender", p.gender);
96
+ }
97
+ }
98
+ function expandTwitter(t, out) {
99
+ const m = (n, c) => {
100
+ if (c != null) out.push({ tag: "meta", key: "meta:name:twitter:" + n, attrs: { name: "twitter:" + n, content: c } });
101
+ };
102
+ m("card", t.card);
103
+ m("site", t.site);
104
+ m("creator", t.creator);
105
+ m("title", t.title);
106
+ m("description", t.description);
107
+ m("image", t.image);
108
+ m("image:alt", t.imageAlt);
109
+ m("player", t.player);
110
+ if (t.playerWidth != null) m("player:width", String(t.playerWidth));
111
+ if (t.playerHeight != null) m("player:height", String(t.playerHeight));
112
+ if (t.app) {
113
+ m("app:name:iphone", t.app.name);
114
+ m("app:id:iphone", t.app.idIphone);
115
+ m("app:id:ipad", t.app.idIpad);
116
+ m("app:id:googleplay", t.app.idGooglePlay);
117
+ }
118
+ }
119
+ function expandIcons(icons, out) {
120
+ const link = (key, attrs) => out.push({ tag: "link", key, attrs });
121
+ if (icons.icon != null) {
122
+ const arr = typeof icons.icon === "string" ? [{ url: icons.icon }] : icons.icon;
123
+ for (const i of arr) link("link:icon:" + i.url, { rel: "icon", href: i.url, ...i.type ? { type: i.type } : {}, ...i.sizes ? { sizes: i.sizes } : {} });
124
+ }
125
+ if (icons.apple != null) {
126
+ const arr = typeof icons.apple === "string" ? [{ url: icons.apple }] : icons.apple;
127
+ for (const i of arr) link("link:apple-touch-icon:" + i.url, { rel: "apple-touch-icon", href: i.url, ...i.sizes ? { sizes: i.sizes } : {} });
128
+ }
129
+ if (icons.mask) link("link:mask-icon", { rel: "mask-icon", href: icons.mask.url, ...icons.mask.color ? { color: icons.mask.color } : {} });
130
+ if (icons.shortcut) link("link:shortcut", { rel: "shortcut icon", href: icons.shortcut });
131
+ if (icons.msTileImage) out.push({ tag: "meta", key: "meta:name:msapplication-TileImage", attrs: { name: "msapplication-TileImage", content: icons.msTileImage } });
132
+ if (icons.msTileColor) out.push({ tag: "meta", key: "meta:name:msapplication-TileColor", attrs: { name: "msapplication-TileColor", content: icons.msTileColor } });
133
+ }
134
+ function expand(input, out, ts) {
135
+ const meta = (key, attrs) => out.push({ tag: "meta", key, attrs });
136
+ const title = val(input.title);
137
+ if (title !== void 0) ts.title = title;
138
+ if (input.titleTemplate !== void 0) ts.template = input.titleTemplate;
139
+ const lang = val(input.lang);
140
+ if (lang !== void 0) out.push({ tag: "htmlAttr", key: "html:lang", name: "lang", value: lang });
141
+ for (const [name, raw] of Object.entries(input.htmlAttrs ?? {})) {
142
+ const value = val(raw);
143
+ if (value !== void 0) out.push({ tag: "htmlAttr", key: `html:${name}`, name, value });
144
+ }
145
+ if (input.charset !== void 0) meta("meta:charset", { charset: input.charset });
146
+ if (input.viewport !== void 0) meta("meta:name:viewport", { name: "viewport", content: input.viewport });
147
+ const desc = val(input.description);
148
+ if (desc !== void 0) meta("meta:name:description", { name: "description", content: desc });
149
+ const kw = val(input.keywords);
150
+ if (kw !== void 0) meta("meta:name:keywords", { name: "keywords", content: Array.isArray(kw) ? kw.join(", ") : kw });
151
+ if (input.author !== void 0) meta("meta:name:author", { name: "author", content: input.author });
152
+ if (input.robots !== void 0) meta("meta:name:robots", { name: "robots", content: robotsToString(input.robots) });
153
+ if (input.generator !== void 0) meta("meta:name:generator", { name: "generator", content: input.generator });
154
+ if (input.applicationName !== void 0) meta("meta:name:application-name", { name: "application-name", content: input.applicationName });
155
+ if (input.referrer !== void 0) meta("meta:name:referrer", { name: "referrer", content: input.referrer });
156
+ const scheme = val(input.colorScheme);
157
+ if (scheme !== void 0) meta("meta:name:color-scheme", { name: "color-scheme", content: scheme });
158
+ const theme = val(input.themeColor);
159
+ if (typeof theme === "string") meta("meta:name:theme-color", { name: "theme-color", content: theme });
160
+ else if (theme) {
161
+ for (const { color, media } of theme) {
162
+ meta(`meta:name:theme-color${media ? `:${media}` : ""}`, media ? { name: "theme-color", content: color, media } : { name: "theme-color", content: color });
163
+ }
164
+ }
165
+ const canonical = val(input.canonical);
166
+ if (canonical !== void 0) out.push({ tag: "link", key: "link:rel:canonical", attrs: { rel: "canonical", href: canonical } });
167
+ if (input.manifest !== void 0) out.push({ tag: "link", key: "link:rel:manifest", attrs: { rel: "manifest", href: input.manifest } });
168
+ if (input.base !== void 0) out.push({ tag: "base", key: "base", attrs: { href: input.base } });
169
+ if (input.verification) {
170
+ const map = { google: "google-site-verification", bing: "msvalidate.01", yandex: "yandex-verification", pinterest: "p:domain_verify" };
171
+ for (const [k, v] of Object.entries(input.verification)) {
172
+ if (v == null) continue;
173
+ const name = map[k] ?? k;
174
+ meta("meta:name:" + name, { name, content: v });
175
+ }
176
+ }
177
+ for (const a of input.alternates ?? []) out.push({ tag: "link", key: "link:alternate:hreflang:" + a.hreflang, attrs: { rel: "alternate", hreflang: a.hreflang, href: a.href } });
178
+ for (const f of input.feeds ?? []) out.push({ tag: "link", key: "link:alternate:feed:" + f.href, attrs: { rel: "alternate", type: f.type ?? "application/rss+xml", href: f.href, ...f.title ? { title: f.title } : {} } });
179
+ if (input.og) expandOG(input.og, out);
180
+ if (input.twitter) expandTwitter(input.twitter, out);
181
+ if (input.icons) expandIcons(input.icons, out);
182
+ const ld = val(input.jsonLd);
183
+ if (ld !== void 0) {
184
+ const arr = Array.isArray(ld) ? ld : [ld];
185
+ for (const obj of arr) {
186
+ const json = JSON.stringify(obj);
187
+ out.push({ tag: "script", key: "script:ldjson:" + djb2(json), attrs: { type: "application/ld+json" }, children: json });
188
+ }
189
+ }
190
+ for (const m of input.meta ?? []) {
191
+ const attrs = {};
192
+ for (const [k, v] of Object.entries(m)) if (v != null) attrs[k === "httpEquiv" ? "http-equiv" : k] = v;
193
+ out.push({ tag: "meta", key: metaKey(attrs), attrs });
194
+ }
195
+ for (const l of input.link ?? []) {
196
+ const attrs = {};
197
+ for (const [k, v] of Object.entries(l)) if (v != null) attrs[k] = v;
198
+ out.push({ tag: "link", key: "link:" + (attrs.rel ?? "") + ":" + (attrs.href ?? djb2(JSON.stringify(attrs))), attrs });
199
+ }
200
+ for (const s of input.script ?? []) {
201
+ const attrs = {};
202
+ let children;
203
+ for (const [k, v] of Object.entries(s)) {
204
+ if (v == null) continue;
205
+ if (k === "children") children = v;
206
+ else attrs[k] = v;
207
+ }
208
+ out.push({ tag: "script", key: "script:" + (attrs.src ?? djb2((children ?? "") + JSON.stringify(attrs))), attrs, children });
209
+ }
210
+ }
211
+ function applyTemplate(tpl, title) {
212
+ return typeof tpl === "function" ? tpl(title) : tpl.includes("%s") ? tpl.replace("%s", title) : tpl;
213
+ }
214
+ function resolveHead(head) {
215
+ const raw = [];
216
+ const ts = {};
217
+ for (const src of head.sources) {
218
+ const input = typeof src === "function" ? src() : src;
219
+ expand(input, raw, ts);
220
+ }
221
+ if (ts.title !== void 0) {
222
+ raw.push({ tag: "title", key: "title", children: ts.template ? applyTemplate(ts.template, ts.title) : ts.title });
223
+ }
224
+ const byKey = /* @__PURE__ */ new Map();
225
+ const htmlAttrs = {};
226
+ for (const t of raw) {
227
+ if (t.tag === "htmlAttr") {
228
+ htmlAttrs[t.name] = t.value;
229
+ continue;
230
+ }
231
+ byKey.set(t.key, t);
232
+ }
233
+ return { tags: [...byKey.values()], htmlAttrs };
234
+ }
235
+
236
+ // src/serialize.ts
237
+ var MANAGED_ATTR = "data-fh";
238
+ var KEY_ATTR = "data-fh-key";
239
+
240
+ // src/reconcile.ts
241
+ function setAttrs(el, attrs) {
242
+ for (const a of Array.from(el.attributes)) {
243
+ if (a.name === MANAGED_ATTR || a.name === KEY_ATTR) continue;
244
+ if (!(a.name in attrs)) el.removeAttribute(a.name);
245
+ }
246
+ for (const [k, v] of Object.entries(attrs)) if (el.getAttribute(k) !== v) el.setAttribute(k, v);
247
+ }
248
+ function createEl(t) {
249
+ const el = document.createElement(t.tag);
250
+ setAttrs(el, t.attrs);
251
+ el.setAttribute(MANAGED_ATTR, "");
252
+ el.setAttribute(KEY_ATTR, t.key);
253
+ if (t.tag === "script" && t.children != null) el.textContent = t.children;
254
+ return el;
255
+ }
256
+ function apply(tags, htmlAttrs) {
257
+ const head = document.head;
258
+ const existing = /* @__PURE__ */ new Map();
259
+ for (const el of Array.from(head.querySelectorAll(`[${MANAGED_ATTR}]`))) {
260
+ const key = el.getAttribute(KEY_ATTR);
261
+ if (el.tagName === "TITLE") continue;
262
+ if (key) existing.set(key, el);
263
+ }
264
+ let title = null;
265
+ const seen = /* @__PURE__ */ new Set();
266
+ for (const t of tags) {
267
+ if (t.tag === "title") {
268
+ title = t.children;
269
+ continue;
270
+ }
271
+ if (t.tag === "htmlAttr") continue;
272
+ seen.add(t.key);
273
+ const cur = existing.get(t.key);
274
+ if (cur && cur.tagName === t.tag.toUpperCase()) {
275
+ setAttrs(cur, t.attrs);
276
+ if (t.tag === "script" && (cur.textContent ?? "") !== (t.children ?? "")) cur.textContent = t.children ?? "";
277
+ } else {
278
+ if (cur) cur.remove();
279
+ head.appendChild(createEl(t));
280
+ }
281
+ }
282
+ for (const [key, el] of existing) if (!seen.has(key)) el.remove();
283
+ if (title != null && document.title !== title) document.title = title;
284
+ for (const [k, v] of Object.entries(htmlAttrs)) {
285
+ if (document.documentElement.getAttribute(k) !== v) document.documentElement.setAttribute(k, v);
286
+ }
287
+ }
288
+ function mountHead(head) {
289
+ return createRoot((dispose) => {
290
+ createEffect(() => {
291
+ head.version();
292
+ const { tags, htmlAttrs } = resolveHead(head);
293
+ apply(tags, htmlAttrs);
294
+ });
295
+ onCleanup2(() => {
296
+ });
297
+ return dispose;
298
+ });
299
+ }
300
+ export {
301
+ mountHead
302
+ };
@@ -0,0 +1,306 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/serialize.ts
21
+ var serialize_exports = {};
22
+ __export(serialize_exports, {
23
+ KEY_ATTR: () => KEY_ATTR,
24
+ MANAGED_ATTR: () => MANAGED_ATTR,
25
+ renderHead: () => renderHead,
26
+ renderHtmlAttrs: () => renderHtmlAttrs
27
+ });
28
+ module.exports = __toCommonJS(serialize_exports);
29
+
30
+ // src/core.ts
31
+ var import_signal = require("@fluixi/reactive/signal");
32
+ function val(v) {
33
+ return typeof v === "function" ? v() : v;
34
+ }
35
+ function robotsToString(r) {
36
+ if (typeof r === "string") return r;
37
+ const out = [];
38
+ out.push(r.index === false ? "noindex" : "index");
39
+ out.push(r.follow === false ? "nofollow" : "follow");
40
+ if (r.noarchive) out.push("noarchive");
41
+ if (r.nosnippet) out.push("nosnippet");
42
+ if (r.noimageindex) out.push("noimageindex");
43
+ if (r.maxSnippet != null) out.push(`max-snippet:${r.maxSnippet}`);
44
+ if (r.maxImagePreview) out.push(`max-image-preview:${r.maxImagePreview}`);
45
+ if (r.maxVideoPreview != null) out.push(`max-video-preview:${r.maxVideoPreview}`);
46
+ return out.join(", ");
47
+ }
48
+ var slot = globalThis[/* @__PURE__ */ Symbol.for("fluixi.head")] ??= {
49
+ active: null,
50
+ scope: null
51
+ };
52
+ function djb2(s) {
53
+ let h = 5381;
54
+ for (let i = 0; i < s.length; i++) h = h * 33 ^ s.charCodeAt(i);
55
+ return (h >>> 0).toString(36);
56
+ }
57
+ function metaKey(a) {
58
+ if (a.charset != null) return "meta:charset";
59
+ if (a.property) return "meta:property:" + a.property;
60
+ if (a.name) return "meta:name:" + a.name;
61
+ if (a["http-equiv"]) return "meta:http-equiv:" + a["http-equiv"];
62
+ return "meta:" + djb2(JSON.stringify(a));
63
+ }
64
+ function pushImage(out, img) {
65
+ const o = typeof img === "string" ? { url: img } : img;
66
+ out.push({ tag: "meta", key: "meta:property:og:image:" + o.url, attrs: { property: "og:image", content: o.url } });
67
+ if (o.secureUrl) out.push({ tag: "meta", key: "meta:property:og:image:secure:" + o.url, attrs: { property: "og:image:secure_url", content: o.secureUrl } });
68
+ if (o.type) out.push({ tag: "meta", key: "meta:property:og:image:type:" + o.url, attrs: { property: "og:image:type", content: o.type } });
69
+ if (o.width != null) out.push({ tag: "meta", key: "meta:property:og:image:width:" + o.url, attrs: { property: "og:image:width", content: String(o.width) } });
70
+ if (o.height != null) out.push({ tag: "meta", key: "meta:property:og:image:height:" + o.url, attrs: { property: "og:image:height", content: String(o.height) } });
71
+ if (o.alt) out.push({ tag: "meta", key: "meta:property:og:image:alt:" + o.url, attrs: { property: "og:image:alt", content: o.alt } });
72
+ }
73
+ function expandOG(og, out) {
74
+ const m = (p, c) => {
75
+ if (c != null) out.push({ tag: "meta", key: "meta:property:og:" + p, attrs: { property: "og:" + p, content: c } });
76
+ };
77
+ m("title", og.title);
78
+ m("description", og.description);
79
+ m("type", og.type);
80
+ m("url", og.url);
81
+ m("site_name", og.siteName);
82
+ m("locale", og.locale);
83
+ for (const l of og.localeAlternate ?? []) out.push({ tag: "meta", key: "meta:property:og:locale:alt:" + l, attrs: { property: "og:locale:alternate", content: l } });
84
+ if (og.image != null) {
85
+ const imgs = Array.isArray(og.image) ? og.image : [og.image];
86
+ for (const i of imgs) pushImage(out, i);
87
+ }
88
+ if (og.video) {
89
+ const v = typeof og.video === "string" ? { url: og.video } : og.video;
90
+ m("video", v.url);
91
+ if ("type" in v && v.type) m("video:type", v.type);
92
+ if ("width" in v && v.width != null) m("video:width", String(v.width));
93
+ if ("height" in v && v.height != null) m("video:height", String(v.height));
94
+ }
95
+ if (og.audio) {
96
+ const a = typeof og.audio === "string" ? { url: og.audio } : og.audio;
97
+ m("audio", a.url);
98
+ if ("type" in a && a.type) m("audio:type", a.type);
99
+ }
100
+ if (og.article) {
101
+ const ar = og.article;
102
+ m("article:published_time", ar.publishedTime);
103
+ m("article:modified_time", ar.modifiedTime);
104
+ m("article:expiration_time", ar.expirationTime);
105
+ m("article:section", ar.section);
106
+ for (const au of [].concat(ar.author ?? [])) out.push({ tag: "meta", key: "meta:property:og:article:author:" + au, attrs: { property: "article:author", content: au } });
107
+ for (const t of [].concat(ar.tag ?? [])) out.push({ tag: "meta", key: "meta:property:og:article:tag:" + t, attrs: { property: "article:tag", content: t } });
108
+ }
109
+ if (og.book) {
110
+ const b = og.book;
111
+ m("book:isbn", b.isbn);
112
+ m("book:release_date", b.releaseDate);
113
+ for (const au of [].concat(b.author ?? [])) out.push({ tag: "meta", key: "meta:property:og:book:author:" + au, attrs: { property: "book:author", content: au } });
114
+ for (const t of [].concat(b.tag ?? [])) out.push({ tag: "meta", key: "meta:property:og:book:tag:" + t, attrs: { property: "book:tag", content: t } });
115
+ }
116
+ if (og.profile) {
117
+ const p = og.profile;
118
+ m("profile:first_name", p.firstName);
119
+ m("profile:last_name", p.lastName);
120
+ m("profile:username", p.username);
121
+ m("profile:gender", p.gender);
122
+ }
123
+ }
124
+ function expandTwitter(t, out) {
125
+ const m = (n, c) => {
126
+ if (c != null) out.push({ tag: "meta", key: "meta:name:twitter:" + n, attrs: { name: "twitter:" + n, content: c } });
127
+ };
128
+ m("card", t.card);
129
+ m("site", t.site);
130
+ m("creator", t.creator);
131
+ m("title", t.title);
132
+ m("description", t.description);
133
+ m("image", t.image);
134
+ m("image:alt", t.imageAlt);
135
+ m("player", t.player);
136
+ if (t.playerWidth != null) m("player:width", String(t.playerWidth));
137
+ if (t.playerHeight != null) m("player:height", String(t.playerHeight));
138
+ if (t.app) {
139
+ m("app:name:iphone", t.app.name);
140
+ m("app:id:iphone", t.app.idIphone);
141
+ m("app:id:ipad", t.app.idIpad);
142
+ m("app:id:googleplay", t.app.idGooglePlay);
143
+ }
144
+ }
145
+ function expandIcons(icons, out) {
146
+ const link = (key, attrs) => out.push({ tag: "link", key, attrs });
147
+ if (icons.icon != null) {
148
+ const arr = typeof icons.icon === "string" ? [{ url: icons.icon }] : icons.icon;
149
+ for (const i of arr) link("link:icon:" + i.url, { rel: "icon", href: i.url, ...i.type ? { type: i.type } : {}, ...i.sizes ? { sizes: i.sizes } : {} });
150
+ }
151
+ if (icons.apple != null) {
152
+ const arr = typeof icons.apple === "string" ? [{ url: icons.apple }] : icons.apple;
153
+ for (const i of arr) link("link:apple-touch-icon:" + i.url, { rel: "apple-touch-icon", href: i.url, ...i.sizes ? { sizes: i.sizes } : {} });
154
+ }
155
+ if (icons.mask) link("link:mask-icon", { rel: "mask-icon", href: icons.mask.url, ...icons.mask.color ? { color: icons.mask.color } : {} });
156
+ if (icons.shortcut) link("link:shortcut", { rel: "shortcut icon", href: icons.shortcut });
157
+ if (icons.msTileImage) out.push({ tag: "meta", key: "meta:name:msapplication-TileImage", attrs: { name: "msapplication-TileImage", content: icons.msTileImage } });
158
+ if (icons.msTileColor) out.push({ tag: "meta", key: "meta:name:msapplication-TileColor", attrs: { name: "msapplication-TileColor", content: icons.msTileColor } });
159
+ }
160
+ function expand(input, out, ts) {
161
+ const meta = (key, attrs) => out.push({ tag: "meta", key, attrs });
162
+ const title = val(input.title);
163
+ if (title !== void 0) ts.title = title;
164
+ if (input.titleTemplate !== void 0) ts.template = input.titleTemplate;
165
+ const lang = val(input.lang);
166
+ if (lang !== void 0) out.push({ tag: "htmlAttr", key: "html:lang", name: "lang", value: lang });
167
+ for (const [name, raw] of Object.entries(input.htmlAttrs ?? {})) {
168
+ const value = val(raw);
169
+ if (value !== void 0) out.push({ tag: "htmlAttr", key: `html:${name}`, name, value });
170
+ }
171
+ if (input.charset !== void 0) meta("meta:charset", { charset: input.charset });
172
+ if (input.viewport !== void 0) meta("meta:name:viewport", { name: "viewport", content: input.viewport });
173
+ const desc = val(input.description);
174
+ if (desc !== void 0) meta("meta:name:description", { name: "description", content: desc });
175
+ const kw = val(input.keywords);
176
+ if (kw !== void 0) meta("meta:name:keywords", { name: "keywords", content: Array.isArray(kw) ? kw.join(", ") : kw });
177
+ if (input.author !== void 0) meta("meta:name:author", { name: "author", content: input.author });
178
+ if (input.robots !== void 0) meta("meta:name:robots", { name: "robots", content: robotsToString(input.robots) });
179
+ if (input.generator !== void 0) meta("meta:name:generator", { name: "generator", content: input.generator });
180
+ if (input.applicationName !== void 0) meta("meta:name:application-name", { name: "application-name", content: input.applicationName });
181
+ if (input.referrer !== void 0) meta("meta:name:referrer", { name: "referrer", content: input.referrer });
182
+ const scheme = val(input.colorScheme);
183
+ if (scheme !== void 0) meta("meta:name:color-scheme", { name: "color-scheme", content: scheme });
184
+ const theme = val(input.themeColor);
185
+ if (typeof theme === "string") meta("meta:name:theme-color", { name: "theme-color", content: theme });
186
+ else if (theme) {
187
+ for (const { color, media } of theme) {
188
+ meta(`meta:name:theme-color${media ? `:${media}` : ""}`, media ? { name: "theme-color", content: color, media } : { name: "theme-color", content: color });
189
+ }
190
+ }
191
+ const canonical = val(input.canonical);
192
+ if (canonical !== void 0) out.push({ tag: "link", key: "link:rel:canonical", attrs: { rel: "canonical", href: canonical } });
193
+ if (input.manifest !== void 0) out.push({ tag: "link", key: "link:rel:manifest", attrs: { rel: "manifest", href: input.manifest } });
194
+ if (input.base !== void 0) out.push({ tag: "base", key: "base", attrs: { href: input.base } });
195
+ if (input.verification) {
196
+ const map = { google: "google-site-verification", bing: "msvalidate.01", yandex: "yandex-verification", pinterest: "p:domain_verify" };
197
+ for (const [k, v] of Object.entries(input.verification)) {
198
+ if (v == null) continue;
199
+ const name = map[k] ?? k;
200
+ meta("meta:name:" + name, { name, content: v });
201
+ }
202
+ }
203
+ for (const a of input.alternates ?? []) out.push({ tag: "link", key: "link:alternate:hreflang:" + a.hreflang, attrs: { rel: "alternate", hreflang: a.hreflang, href: a.href } });
204
+ for (const f of input.feeds ?? []) out.push({ tag: "link", key: "link:alternate:feed:" + f.href, attrs: { rel: "alternate", type: f.type ?? "application/rss+xml", href: f.href, ...f.title ? { title: f.title } : {} } });
205
+ if (input.og) expandOG(input.og, out);
206
+ if (input.twitter) expandTwitter(input.twitter, out);
207
+ if (input.icons) expandIcons(input.icons, out);
208
+ const ld = val(input.jsonLd);
209
+ if (ld !== void 0) {
210
+ const arr = Array.isArray(ld) ? ld : [ld];
211
+ for (const obj of arr) {
212
+ const json = JSON.stringify(obj);
213
+ out.push({ tag: "script", key: "script:ldjson:" + djb2(json), attrs: { type: "application/ld+json" }, children: json });
214
+ }
215
+ }
216
+ for (const m of input.meta ?? []) {
217
+ const attrs = {};
218
+ for (const [k, v] of Object.entries(m)) if (v != null) attrs[k === "httpEquiv" ? "http-equiv" : k] = v;
219
+ out.push({ tag: "meta", key: metaKey(attrs), attrs });
220
+ }
221
+ for (const l of input.link ?? []) {
222
+ const attrs = {};
223
+ for (const [k, v] of Object.entries(l)) if (v != null) attrs[k] = v;
224
+ out.push({ tag: "link", key: "link:" + (attrs.rel ?? "") + ":" + (attrs.href ?? djb2(JSON.stringify(attrs))), attrs });
225
+ }
226
+ for (const s of input.script ?? []) {
227
+ const attrs = {};
228
+ let children;
229
+ for (const [k, v] of Object.entries(s)) {
230
+ if (v == null) continue;
231
+ if (k === "children") children = v;
232
+ else attrs[k] = v;
233
+ }
234
+ out.push({ tag: "script", key: "script:" + (attrs.src ?? djb2((children ?? "") + JSON.stringify(attrs))), attrs, children });
235
+ }
236
+ }
237
+ function applyTemplate(tpl, title) {
238
+ return typeof tpl === "function" ? tpl(title) : tpl.includes("%s") ? tpl.replace("%s", title) : tpl;
239
+ }
240
+ function resolveHead(head) {
241
+ const raw = [];
242
+ const ts = {};
243
+ for (const src of head.sources) {
244
+ const input = typeof src === "function" ? src() : src;
245
+ expand(input, raw, ts);
246
+ }
247
+ if (ts.title !== void 0) {
248
+ raw.push({ tag: "title", key: "title", children: ts.template ? applyTemplate(ts.template, ts.title) : ts.title });
249
+ }
250
+ const byKey = /* @__PURE__ */ new Map();
251
+ const htmlAttrs = {};
252
+ for (const t of raw) {
253
+ if (t.tag === "htmlAttr") {
254
+ htmlAttrs[t.name] = t.value;
255
+ continue;
256
+ }
257
+ byKey.set(t.key, t);
258
+ }
259
+ return { tags: [...byKey.values()], htmlAttrs };
260
+ }
261
+
262
+ // src/serialize.ts
263
+ function escAttr(s) {
264
+ return s.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
265
+ }
266
+ function escText(s) {
267
+ return s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
268
+ }
269
+ function escScript(s) {
270
+ return s.replace(/<\//g, "<\\/").replace(/<!--/g, "<\\!--");
271
+ }
272
+ var MANAGED_ATTR = "data-fh";
273
+ var KEY_ATTR = "data-fh-key";
274
+ function renderAttrs(attrs) {
275
+ let s = "";
276
+ for (const [k, v] of Object.entries(attrs)) s += ` ${k}="${escAttr(v)}"`;
277
+ return s;
278
+ }
279
+ function renderTag(t) {
280
+ switch (t.tag) {
281
+ case "title":
282
+ return `<title ${MANAGED_ATTR}>${escText(t.children)}</title>`;
283
+ case "meta":
284
+ case "link":
285
+ case "base": {
286
+ const marker = ` ${MANAGED_ATTR} ${KEY_ATTR}="${escAttr(t.key)}"`;
287
+ return `<${t.tag}${renderAttrs(t.attrs)}${marker}>`;
288
+ }
289
+ case "script": {
290
+ const marker = ` ${MANAGED_ATTR} ${KEY_ATTR}="${escAttr(t.key)}"`;
291
+ const body = t.children ? escScript(t.children) : "";
292
+ return `<script${renderAttrs(t.attrs)}${marker}>${body}<\/script>`;
293
+ }
294
+ default:
295
+ return "";
296
+ }
297
+ }
298
+ function renderHead(head) {
299
+ const { tags, htmlAttrs } = resolveHead(head);
300
+ const order = { title: 0, base: 1, meta: 2, link: 3, script: 4 };
301
+ const sorted = [...tags].sort((a, b) => (order[a.tag] ?? 9) - (order[b.tag] ?? 9));
302
+ return { headHtml: sorted.map(renderTag).join(""), htmlAttrs };
303
+ }
304
+ function renderHtmlAttrs(htmlAttrs) {
305
+ return Object.entries(htmlAttrs).map(([k, v]) => ` ${k}="${escAttr(v)}"`).join("");
306
+ }