@fluixi/head 1.0.0-alpha.49 → 1.0.0-alpha.51

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/core.mjs ADDED
@@ -0,0 +1,276 @@
1
+ // src/core.ts
2
+ import { getOwner, onCleanup, createSignal } from "@fluixi/reactive/signal";
3
+ function val(v) {
4
+ return typeof v === "function" ? v() : v;
5
+ }
6
+ function robotsToString(r) {
7
+ if (typeof r === "string") return r;
8
+ const out = [];
9
+ out.push(r.index === false ? "noindex" : "index");
10
+ out.push(r.follow === false ? "nofollow" : "follow");
11
+ if (r.noarchive) out.push("noarchive");
12
+ if (r.nosnippet) out.push("nosnippet");
13
+ if (r.noimageindex) out.push("noimageindex");
14
+ if (r.maxSnippet != null) out.push(`max-snippet:${r.maxSnippet}`);
15
+ if (r.maxImagePreview) out.push(`max-image-preview:${r.maxImagePreview}`);
16
+ if (r.maxVideoPreview != null) out.push(`max-video-preview:${r.maxVideoPreview}`);
17
+ return out.join(", ");
18
+ }
19
+ function createHead() {
20
+ const sources = [];
21
+ const [version, bump] = createSignal(0);
22
+ return {
23
+ sources,
24
+ version,
25
+ push(input) {
26
+ sources.push(input);
27
+ bump((n) => n + 1);
28
+ return () => {
29
+ const i = sources.indexOf(input);
30
+ if (i !== -1) {
31
+ sources.splice(i, 1);
32
+ bump((n) => n + 1);
33
+ }
34
+ };
35
+ }
36
+ };
37
+ }
38
+ var active = null;
39
+ function setActiveHead(h) {
40
+ active = h;
41
+ }
42
+ function getActiveHead() {
43
+ return active;
44
+ }
45
+ function runWithHead(h, fn) {
46
+ const prev = active;
47
+ active = h;
48
+ try {
49
+ return fn();
50
+ } finally {
51
+ active = prev;
52
+ }
53
+ }
54
+ function useHead(input) {
55
+ const h = getActiveHead();
56
+ if (!h) {
57
+ const proc = globalThis.process;
58
+ if (proc?.env?.NODE_ENV !== "production") {
59
+ console.warn("[fluixi/head] useHead() called with no active head registry — ignored.");
60
+ }
61
+ return () => {
62
+ };
63
+ }
64
+ const dispose = h.push(input);
65
+ if (typeof window !== "undefined" && getOwner()) onCleanup(dispose);
66
+ return dispose;
67
+ }
68
+ var seo = useHead;
69
+ function djb2(s) {
70
+ let h = 5381;
71
+ for (let i = 0; i < s.length; i++) h = h * 33 ^ s.charCodeAt(i);
72
+ return (h >>> 0).toString(36);
73
+ }
74
+ function metaKey(a) {
75
+ if (a.charset != null) return "meta:charset";
76
+ if (a.property) return "meta:property:" + a.property;
77
+ if (a.name) return "meta:name:" + a.name;
78
+ if (a["http-equiv"]) return "meta:http-equiv:" + a["http-equiv"];
79
+ return "meta:" + djb2(JSON.stringify(a));
80
+ }
81
+ function pushImage(out, img) {
82
+ const o = typeof img === "string" ? { url: img } : img;
83
+ out.push({ tag: "meta", key: "meta:property:og:image:" + o.url, attrs: { property: "og:image", content: o.url } });
84
+ if (o.secureUrl) out.push({ tag: "meta", key: "meta:property:og:image:secure:" + o.url, attrs: { property: "og:image:secure_url", content: o.secureUrl } });
85
+ if (o.type) out.push({ tag: "meta", key: "meta:property:og:image:type:" + o.url, attrs: { property: "og:image:type", content: o.type } });
86
+ 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) } });
87
+ 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) } });
88
+ if (o.alt) out.push({ tag: "meta", key: "meta:property:og:image:alt:" + o.url, attrs: { property: "og:image:alt", content: o.alt } });
89
+ }
90
+ function expandOG(og, out) {
91
+ const m = (p, c) => {
92
+ if (c != null) out.push({ tag: "meta", key: "meta:property:og:" + p, attrs: { property: "og:" + p, content: c } });
93
+ };
94
+ m("title", og.title);
95
+ m("description", og.description);
96
+ m("type", og.type);
97
+ m("url", og.url);
98
+ m("site_name", og.siteName);
99
+ m("locale", og.locale);
100
+ for (const l of og.localeAlternate ?? []) out.push({ tag: "meta", key: "meta:property:og:locale:alt:" + l, attrs: { property: "og:locale:alternate", content: l } });
101
+ if (og.image != null) {
102
+ const imgs = Array.isArray(og.image) ? og.image : [og.image];
103
+ for (const i of imgs) pushImage(out, i);
104
+ }
105
+ if (og.video) {
106
+ const v = typeof og.video === "string" ? { url: og.video } : og.video;
107
+ m("video", v.url);
108
+ if ("type" in v && v.type) m("video:type", v.type);
109
+ if ("width" in v && v.width != null) m("video:width", String(v.width));
110
+ if ("height" in v && v.height != null) m("video:height", String(v.height));
111
+ }
112
+ if (og.audio) {
113
+ const a = typeof og.audio === "string" ? { url: og.audio } : og.audio;
114
+ m("audio", a.url);
115
+ if ("type" in a && a.type) m("audio:type", a.type);
116
+ }
117
+ if (og.article) {
118
+ const ar = og.article;
119
+ m("article:published_time", ar.publishedTime);
120
+ m("article:modified_time", ar.modifiedTime);
121
+ m("article:expiration_time", ar.expirationTime);
122
+ m("article:section", ar.section);
123
+ for (const au of [].concat(ar.author ?? [])) out.push({ tag: "meta", key: "meta:property:og:article:author:" + au, attrs: { property: "article:author", content: au } });
124
+ for (const t of [].concat(ar.tag ?? [])) out.push({ tag: "meta", key: "meta:property:og:article:tag:" + t, attrs: { property: "article:tag", content: t } });
125
+ }
126
+ if (og.book) {
127
+ const b = og.book;
128
+ m("book:isbn", b.isbn);
129
+ m("book:release_date", b.releaseDate);
130
+ for (const au of [].concat(b.author ?? [])) out.push({ tag: "meta", key: "meta:property:og:book:author:" + au, attrs: { property: "book:author", content: au } });
131
+ for (const t of [].concat(b.tag ?? [])) out.push({ tag: "meta", key: "meta:property:og:book:tag:" + t, attrs: { property: "book:tag", content: t } });
132
+ }
133
+ if (og.profile) {
134
+ const p = og.profile;
135
+ m("profile:first_name", p.firstName);
136
+ m("profile:last_name", p.lastName);
137
+ m("profile:username", p.username);
138
+ m("profile:gender", p.gender);
139
+ }
140
+ }
141
+ function expandTwitter(t, out) {
142
+ const m = (n, c) => {
143
+ if (c != null) out.push({ tag: "meta", key: "meta:name:twitter:" + n, attrs: { name: "twitter:" + n, content: c } });
144
+ };
145
+ m("card", t.card);
146
+ m("site", t.site);
147
+ m("creator", t.creator);
148
+ m("title", t.title);
149
+ m("description", t.description);
150
+ m("image", t.image);
151
+ m("image:alt", t.imageAlt);
152
+ m("player", t.player);
153
+ if (t.playerWidth != null) m("player:width", String(t.playerWidth));
154
+ if (t.playerHeight != null) m("player:height", String(t.playerHeight));
155
+ if (t.app) {
156
+ m("app:name:iphone", t.app.name);
157
+ m("app:id:iphone", t.app.idIphone);
158
+ m("app:id:ipad", t.app.idIpad);
159
+ m("app:id:googleplay", t.app.idGooglePlay);
160
+ }
161
+ }
162
+ function expandIcons(icons, out) {
163
+ const link = (key, attrs) => out.push({ tag: "link", key, attrs });
164
+ if (icons.icon != null) {
165
+ const arr = typeof icons.icon === "string" ? [{ url: icons.icon }] : icons.icon;
166
+ 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 } : {} });
167
+ }
168
+ if (icons.apple != null) {
169
+ const arr = typeof icons.apple === "string" ? [{ url: icons.apple }] : icons.apple;
170
+ for (const i of arr) link("link:apple-touch-icon:" + i.url, { rel: "apple-touch-icon", href: i.url, ...i.sizes ? { sizes: i.sizes } : {} });
171
+ }
172
+ if (icons.mask) link("link:mask-icon", { rel: "mask-icon", href: icons.mask.url, ...icons.mask.color ? { color: icons.mask.color } : {} });
173
+ if (icons.shortcut) link("link:shortcut", { rel: "shortcut icon", href: icons.shortcut });
174
+ if (icons.msTileImage) out.push({ tag: "meta", key: "meta:name:msapplication-TileImage", attrs: { name: "msapplication-TileImage", content: icons.msTileImage } });
175
+ if (icons.msTileColor) out.push({ tag: "meta", key: "meta:name:msapplication-TileColor", attrs: { name: "msapplication-TileColor", content: icons.msTileColor } });
176
+ }
177
+ function expand(input, out, ts) {
178
+ const meta = (key, attrs) => out.push({ tag: "meta", key, attrs });
179
+ const title = val(input.title);
180
+ if (title !== void 0) ts.title = title;
181
+ if (input.titleTemplate !== void 0) ts.template = input.titleTemplate;
182
+ const lang = val(input.lang);
183
+ if (lang !== void 0) out.push({ tag: "htmlAttr", key: "html:lang", name: "lang", value: lang });
184
+ if (input.charset !== void 0) meta("meta:charset", { charset: input.charset });
185
+ if (input.viewport !== void 0) meta("meta:name:viewport", { name: "viewport", content: input.viewport });
186
+ const desc = val(input.description);
187
+ if (desc !== void 0) meta("meta:name:description", { name: "description", content: desc });
188
+ const kw = val(input.keywords);
189
+ if (kw !== void 0) meta("meta:name:keywords", { name: "keywords", content: Array.isArray(kw) ? kw.join(", ") : kw });
190
+ if (input.author !== void 0) meta("meta:name:author", { name: "author", content: input.author });
191
+ if (input.robots !== void 0) meta("meta:name:robots", { name: "robots", content: robotsToString(input.robots) });
192
+ if (input.generator !== void 0) meta("meta:name:generator", { name: "generator", content: input.generator });
193
+ if (input.applicationName !== void 0) meta("meta:name:application-name", { name: "application-name", content: input.applicationName });
194
+ if (input.referrer !== void 0) meta("meta:name:referrer", { name: "referrer", content: input.referrer });
195
+ if (input.colorScheme !== void 0) meta("meta:name:color-scheme", { name: "color-scheme", content: input.colorScheme });
196
+ const theme = val(input.themeColor);
197
+ if (theme !== void 0) meta("meta:name:theme-color", { name: "theme-color", content: theme });
198
+ const canonical = val(input.canonical);
199
+ if (canonical !== void 0) out.push({ tag: "link", key: "link:rel:canonical", attrs: { rel: "canonical", href: canonical } });
200
+ if (input.manifest !== void 0) out.push({ tag: "link", key: "link:rel:manifest", attrs: { rel: "manifest", href: input.manifest } });
201
+ if (input.base !== void 0) out.push({ tag: "base", key: "base", attrs: { href: input.base } });
202
+ if (input.verification) {
203
+ const map = { google: "google-site-verification", bing: "msvalidate.01", yandex: "yandex-verification", pinterest: "p:domain_verify" };
204
+ for (const [k, v] of Object.entries(input.verification)) {
205
+ if (v == null) continue;
206
+ const name = map[k] ?? k;
207
+ meta("meta:name:" + name, { name, content: v });
208
+ }
209
+ }
210
+ 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 } });
211
+ 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 } : {} } });
212
+ if (input.og) expandOG(input.og, out);
213
+ if (input.twitter) expandTwitter(input.twitter, out);
214
+ if (input.icons) expandIcons(input.icons, out);
215
+ const ld = val(input.jsonLd);
216
+ if (ld !== void 0) {
217
+ const arr = Array.isArray(ld) ? ld : [ld];
218
+ for (const obj of arr) {
219
+ const json = JSON.stringify(obj);
220
+ out.push({ tag: "script", key: "script:ldjson:" + djb2(json), attrs: { type: "application/ld+json" }, children: json });
221
+ }
222
+ }
223
+ for (const m of input.meta ?? []) {
224
+ const attrs = {};
225
+ for (const [k, v] of Object.entries(m)) if (v != null) attrs[k === "httpEquiv" ? "http-equiv" : k] = v;
226
+ out.push({ tag: "meta", key: metaKey(attrs), attrs });
227
+ }
228
+ for (const l of input.link ?? []) {
229
+ const attrs = {};
230
+ for (const [k, v] of Object.entries(l)) if (v != null) attrs[k] = v;
231
+ out.push({ tag: "link", key: "link:" + (attrs.rel ?? "") + ":" + (attrs.href ?? djb2(JSON.stringify(attrs))), attrs });
232
+ }
233
+ for (const s of input.script ?? []) {
234
+ const attrs = {};
235
+ let children;
236
+ for (const [k, v] of Object.entries(s)) {
237
+ if (v == null) continue;
238
+ if (k === "children") children = v;
239
+ else attrs[k] = v;
240
+ }
241
+ out.push({ tag: "script", key: "script:" + (attrs.src ?? djb2((children ?? "") + JSON.stringify(attrs))), attrs, children });
242
+ }
243
+ }
244
+ function applyTemplate(tpl, title) {
245
+ return typeof tpl === "function" ? tpl(title) : tpl.includes("%s") ? tpl.replace("%s", title) : tpl;
246
+ }
247
+ function resolveHead(head) {
248
+ const raw = [];
249
+ const ts = {};
250
+ for (const src of head.sources) {
251
+ const input = typeof src === "function" ? src() : src;
252
+ expand(input, raw, ts);
253
+ }
254
+ if (ts.title !== void 0) {
255
+ raw.push({ tag: "title", key: "title", children: ts.template ? applyTemplate(ts.template, ts.title) : ts.title });
256
+ }
257
+ const byKey = /* @__PURE__ */ new Map();
258
+ const htmlAttrs = {};
259
+ for (const t of raw) {
260
+ if (t.tag === "htmlAttr") {
261
+ htmlAttrs[t.name] = t.value;
262
+ continue;
263
+ }
264
+ byKey.set(t.key, t);
265
+ }
266
+ return { tags: [...byKey.values()], htmlAttrs };
267
+ }
268
+ export {
269
+ createHead,
270
+ getActiveHead,
271
+ resolveHead,
272
+ runWithHead,
273
+ seo,
274
+ setActiveHead,
275
+ useHead
276
+ };