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