@unhead/schema-org 0.0.2
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/README.md +9 -0
- package/dist/index.cjs +1387 -0
- package/dist/index.d.ts +1499 -0
- package/dist/index.mjs +1301 -0
- package/package.json +54 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,1301 @@
|
|
|
1
|
+
import { hash } from 'ohash';
|
|
2
|
+
import { defu } from 'defu';
|
|
3
|
+
import { hasProtocol, joinURL, withBase, withoutTrailingSlash } from 'ufo';
|
|
4
|
+
import { useHead } from 'unhead';
|
|
5
|
+
|
|
6
|
+
function defineSchemaOrgResolver(schema) {
|
|
7
|
+
return schema;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const idReference = (node) => ({
|
|
11
|
+
"@id": typeof node !== "string" ? node["@id"] : node
|
|
12
|
+
});
|
|
13
|
+
const resolvableDateToDate = (val) => {
|
|
14
|
+
try {
|
|
15
|
+
const date = val instanceof Date ? val : new Date(Date.parse(val));
|
|
16
|
+
return `${date.getFullYear()}-${date.getMonth()}-${date.getDate()}`;
|
|
17
|
+
} catch (e) {
|
|
18
|
+
}
|
|
19
|
+
return typeof val === "string" ? val : val.toString();
|
|
20
|
+
};
|
|
21
|
+
const resolvableDateToIso = (val) => {
|
|
22
|
+
if (!val)
|
|
23
|
+
return val;
|
|
24
|
+
try {
|
|
25
|
+
if (val instanceof Date)
|
|
26
|
+
return val.toISOString();
|
|
27
|
+
else
|
|
28
|
+
return new Date(Date.parse(val)).toISOString();
|
|
29
|
+
} catch (e) {
|
|
30
|
+
}
|
|
31
|
+
return typeof val === "string" ? val : val.toString();
|
|
32
|
+
};
|
|
33
|
+
const IdentityId = "#identity";
|
|
34
|
+
const setIfEmpty = (node, field, value) => {
|
|
35
|
+
if (!node?.[field] && value)
|
|
36
|
+
node[field] = value;
|
|
37
|
+
};
|
|
38
|
+
const asArray = (input) => Array.isArray(input) ? input : [input];
|
|
39
|
+
const dedupeMerge = (node, field, value) => {
|
|
40
|
+
const dedupeMerge2 = [];
|
|
41
|
+
const input = asArray(node[field]);
|
|
42
|
+
dedupeMerge2.push(...input);
|
|
43
|
+
const data = new Set(dedupeMerge2);
|
|
44
|
+
data.add(value);
|
|
45
|
+
node[field] = [...data.values()].filter(Boolean);
|
|
46
|
+
};
|
|
47
|
+
const prefixId = (url, id) => {
|
|
48
|
+
if (hasProtocol(id))
|
|
49
|
+
return url;
|
|
50
|
+
if (!id.startsWith("#"))
|
|
51
|
+
id = `#${id}`;
|
|
52
|
+
return joinURL(url, id);
|
|
53
|
+
};
|
|
54
|
+
const trimLength = (val, length) => {
|
|
55
|
+
if (!val)
|
|
56
|
+
return val;
|
|
57
|
+
if (val.length > length) {
|
|
58
|
+
const trimmedString = val.substring(0, length);
|
|
59
|
+
return trimmedString.substring(0, Math.min(trimmedString.length, trimmedString.lastIndexOf(" ")));
|
|
60
|
+
}
|
|
61
|
+
return val;
|
|
62
|
+
};
|
|
63
|
+
const resolveDefaultType = (node, defaultType) => {
|
|
64
|
+
const val = node["@type"];
|
|
65
|
+
if (val === defaultType)
|
|
66
|
+
return;
|
|
67
|
+
const types = /* @__PURE__ */ new Set([
|
|
68
|
+
...asArray(defaultType),
|
|
69
|
+
...asArray(val)
|
|
70
|
+
]);
|
|
71
|
+
node["@type"] = types.size === 1 ? val : [...types.values()];
|
|
72
|
+
};
|
|
73
|
+
const resolveWithBase = (base, urlOrPath) => {
|
|
74
|
+
if (!urlOrPath || hasProtocol(urlOrPath) || !urlOrPath.startsWith("/") && !urlOrPath.startsWith("#"))
|
|
75
|
+
return urlOrPath;
|
|
76
|
+
return withBase(urlOrPath, base);
|
|
77
|
+
};
|
|
78
|
+
const resolveAsGraphKey = (key) => {
|
|
79
|
+
if (!key)
|
|
80
|
+
return key;
|
|
81
|
+
return key.substring(key.lastIndexOf("#"));
|
|
82
|
+
};
|
|
83
|
+
const stripEmptyProperties = (obj) => {
|
|
84
|
+
Object.keys(obj).forEach((k) => {
|
|
85
|
+
if (obj[k] && typeof obj[k] === "object") {
|
|
86
|
+
if (obj[k].__v_isReadonly || obj[k].__v_isRef)
|
|
87
|
+
return;
|
|
88
|
+
stripEmptyProperties(obj[k]);
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
if (obj[k] === "" || obj[k] === null || typeof obj[k] === "undefined")
|
|
92
|
+
delete obj[k];
|
|
93
|
+
});
|
|
94
|
+
return obj;
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
const offerResolver = defineSchemaOrgResolver({
|
|
98
|
+
cast(node) {
|
|
99
|
+
if (typeof node === "number" || typeof node === "string") {
|
|
100
|
+
return {
|
|
101
|
+
price: node
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
return node;
|
|
105
|
+
},
|
|
106
|
+
defaults: {
|
|
107
|
+
"@type": "Offer",
|
|
108
|
+
"availability": "InStock"
|
|
109
|
+
},
|
|
110
|
+
resolve(node, ctx) {
|
|
111
|
+
setIfEmpty(node, "priceCurrency", ctx.meta.currency);
|
|
112
|
+
setIfEmpty(node, "priceValidUntil", new Date(Date.UTC(new Date().getFullYear() + 1, 12, -1, 0, 0, 0)));
|
|
113
|
+
if (node.url)
|
|
114
|
+
resolveWithBase(ctx.meta.host, node.url);
|
|
115
|
+
if (node.availability)
|
|
116
|
+
node.availability = withBase(node.availability, "https://schema.org/");
|
|
117
|
+
if (node.priceValidUntil)
|
|
118
|
+
node.priceValidUntil = resolvableDateToIso(node.priceValidUntil);
|
|
119
|
+
return node;
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
const aggregateOfferResolver = defineSchemaOrgResolver({
|
|
124
|
+
defaults: {
|
|
125
|
+
"@type": "AggregateOffer"
|
|
126
|
+
},
|
|
127
|
+
inheritMeta: [
|
|
128
|
+
{ meta: "currency", key: "priceCurrency" }
|
|
129
|
+
],
|
|
130
|
+
resolve(node, ctx) {
|
|
131
|
+
node.offers = resolveRelation(node.offers, ctx, offerResolver);
|
|
132
|
+
if (node.offers)
|
|
133
|
+
setIfEmpty(node, "offerCount", asArray(node.offers).length);
|
|
134
|
+
return node;
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
const aggregateRatingResolver = defineSchemaOrgResolver({
|
|
139
|
+
defaults: {
|
|
140
|
+
"@type": "AggregateRating"
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
const searchActionResolver = defineSchemaOrgResolver({
|
|
145
|
+
defaults: {
|
|
146
|
+
"@type": "SearchAction",
|
|
147
|
+
"target": {
|
|
148
|
+
"@type": "EntryPoint"
|
|
149
|
+
},
|
|
150
|
+
"query-input": {
|
|
151
|
+
"@type": "PropertyValueSpecification",
|
|
152
|
+
"valueRequired": true,
|
|
153
|
+
"valueName": "search_term_string"
|
|
154
|
+
}
|
|
155
|
+
},
|
|
156
|
+
resolve(node, ctx) {
|
|
157
|
+
if (typeof node.target === "string") {
|
|
158
|
+
node.target = {
|
|
159
|
+
"@type": "EntryPoint",
|
|
160
|
+
"urlTemplate": resolveWithBase(ctx.meta.host, node.target)
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
return node;
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
const PrimaryWebSiteId = "#website";
|
|
168
|
+
const webSiteResolver = defineSchemaOrgResolver({
|
|
169
|
+
defaults: {
|
|
170
|
+
"@type": "WebSite"
|
|
171
|
+
},
|
|
172
|
+
inheritMeta: [
|
|
173
|
+
"inLanguage",
|
|
174
|
+
{ meta: "host", key: "url" }
|
|
175
|
+
],
|
|
176
|
+
idPrefix: ["host", PrimaryWebSiteId],
|
|
177
|
+
resolve(node, ctx) {
|
|
178
|
+
node.potentialAction = resolveRelation(node.potentialAction, ctx, searchActionResolver, {
|
|
179
|
+
array: true
|
|
180
|
+
});
|
|
181
|
+
node.publisher = resolveRelation(node.publisher, ctx);
|
|
182
|
+
return node;
|
|
183
|
+
},
|
|
184
|
+
resolveRootNode(node, { find }) {
|
|
185
|
+
if (resolveAsGraphKey(node["@id"]) === PrimaryWebSiteId) {
|
|
186
|
+
const identity = find(IdentityId);
|
|
187
|
+
if (identity)
|
|
188
|
+
setIfEmpty(node, "publisher", idReference(identity));
|
|
189
|
+
const webPage = find(PrimaryWebPageId);
|
|
190
|
+
if (webPage)
|
|
191
|
+
setIfEmpty(webPage, "isPartOf", idReference(node));
|
|
192
|
+
}
|
|
193
|
+
return node;
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
const resolveListItem = defineSchemaOrgResolver({
|
|
198
|
+
cast(node) {
|
|
199
|
+
if (typeof node === "string") {
|
|
200
|
+
node = {
|
|
201
|
+
name: node
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
return node;
|
|
205
|
+
},
|
|
206
|
+
defaults: {
|
|
207
|
+
"@type": "ListItem"
|
|
208
|
+
},
|
|
209
|
+
resolve(node, ctx) {
|
|
210
|
+
if (typeof node.item === "string")
|
|
211
|
+
node.item = resolveWithBase(ctx.meta.host, node.item);
|
|
212
|
+
return node;
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
const PrimaryBreadcrumbId = "#breadcrumb";
|
|
217
|
+
const breadcrumbResolver = defineSchemaOrgResolver({
|
|
218
|
+
defaults: {
|
|
219
|
+
"@type": "BreadcrumbList"
|
|
220
|
+
},
|
|
221
|
+
idPrefix: ["url", PrimaryBreadcrumbId],
|
|
222
|
+
resolve(breadcrumb, ctx) {
|
|
223
|
+
if (breadcrumb.itemListElement) {
|
|
224
|
+
let index = 1;
|
|
225
|
+
breadcrumb.itemListElement = resolveRelation(breadcrumb.itemListElement, ctx, resolveListItem, {
|
|
226
|
+
array: true,
|
|
227
|
+
afterResolve(node) {
|
|
228
|
+
setIfEmpty(node, "position", index++);
|
|
229
|
+
}
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
return breadcrumb;
|
|
233
|
+
},
|
|
234
|
+
resolveRootNode(node, { find }) {
|
|
235
|
+
const webPage = find(PrimaryWebPageId);
|
|
236
|
+
if (webPage)
|
|
237
|
+
setIfEmpty(webPage, "breadcrumb", idReference(node));
|
|
238
|
+
}
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
const imageResolver = defineSchemaOrgResolver({
|
|
242
|
+
alias: "image",
|
|
243
|
+
cast(input) {
|
|
244
|
+
if (typeof input === "string") {
|
|
245
|
+
input = {
|
|
246
|
+
url: input
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
return input;
|
|
250
|
+
},
|
|
251
|
+
defaults: {
|
|
252
|
+
"@type": "ImageObject"
|
|
253
|
+
},
|
|
254
|
+
inheritMeta: [
|
|
255
|
+
"inLanguage"
|
|
256
|
+
],
|
|
257
|
+
idPrefix: "host",
|
|
258
|
+
resolve(image, { meta }) {
|
|
259
|
+
image.url = resolveWithBase(meta.host, image.url);
|
|
260
|
+
setIfEmpty(image, "contentUrl", image.url);
|
|
261
|
+
if (image.height && !image.width)
|
|
262
|
+
delete image.height;
|
|
263
|
+
if (image.width && !image.height)
|
|
264
|
+
delete image.width;
|
|
265
|
+
return image;
|
|
266
|
+
}
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
const addressResolver = defineSchemaOrgResolver({
|
|
270
|
+
defaults: {
|
|
271
|
+
"@type": "PostalAddress"
|
|
272
|
+
}
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
const organizationResolver = defineSchemaOrgResolver({
|
|
276
|
+
defaults: {
|
|
277
|
+
"@type": "Organization"
|
|
278
|
+
},
|
|
279
|
+
idPrefix: ["host", IdentityId],
|
|
280
|
+
inheritMeta: [
|
|
281
|
+
{ meta: "host", key: "url" }
|
|
282
|
+
],
|
|
283
|
+
resolve(node, ctx) {
|
|
284
|
+
resolveDefaultType(node, "Organization");
|
|
285
|
+
node.address = resolveRelation(node.address, ctx, addressResolver);
|
|
286
|
+
return node;
|
|
287
|
+
},
|
|
288
|
+
resolveRootNode(node, ctx) {
|
|
289
|
+
const isIdentity = resolveAsGraphKey(node["@id"]) === IdentityId;
|
|
290
|
+
const webPage = ctx.find(PrimaryWebPageId);
|
|
291
|
+
if (node.logo) {
|
|
292
|
+
node.logo = resolveRelation(node.logo, ctx, imageResolver, {
|
|
293
|
+
root: true,
|
|
294
|
+
afterResolve(logo) {
|
|
295
|
+
if (isIdentity)
|
|
296
|
+
logo["@id"] = prefixId(ctx.meta.host, "#logo");
|
|
297
|
+
setIfEmpty(logo, "caption", node.name);
|
|
298
|
+
}
|
|
299
|
+
});
|
|
300
|
+
if (webPage)
|
|
301
|
+
setIfEmpty(webPage, "primaryImageOfPage", idReference(node.logo));
|
|
302
|
+
}
|
|
303
|
+
if (isIdentity && webPage)
|
|
304
|
+
setIfEmpty(webPage, "about", idReference(node));
|
|
305
|
+
const webSite = ctx.find(PrimaryWebSiteId);
|
|
306
|
+
if (webSite)
|
|
307
|
+
setIfEmpty(webSite, "publisher", idReference(node));
|
|
308
|
+
}
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
const personResolver = defineSchemaOrgResolver({
|
|
312
|
+
cast(node) {
|
|
313
|
+
if (typeof node === "string") {
|
|
314
|
+
return {
|
|
315
|
+
name: node
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
return node;
|
|
319
|
+
},
|
|
320
|
+
defaults: {
|
|
321
|
+
"@type": "Person"
|
|
322
|
+
},
|
|
323
|
+
idPrefix: ["host", IdentityId],
|
|
324
|
+
resolveRootNode(node, { find, meta }) {
|
|
325
|
+
if (resolveAsGraphKey(node["@id"]) === IdentityId) {
|
|
326
|
+
setIfEmpty(node, "url", meta.host);
|
|
327
|
+
const webPage = find(PrimaryWebPageId);
|
|
328
|
+
if (webPage)
|
|
329
|
+
setIfEmpty(webPage, "about", idReference(node));
|
|
330
|
+
const webSite = find(PrimaryWebSiteId);
|
|
331
|
+
if (webSite)
|
|
332
|
+
setIfEmpty(webSite, "publisher", idReference(node));
|
|
333
|
+
}
|
|
334
|
+
const article = find(PrimaryArticleId);
|
|
335
|
+
if (article)
|
|
336
|
+
setIfEmpty(article, "author", idReference(node));
|
|
337
|
+
}
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
const readActionResolver = defineSchemaOrgResolver({
|
|
341
|
+
defaults: {
|
|
342
|
+
"@type": "ReadAction"
|
|
343
|
+
},
|
|
344
|
+
resolve(node, ctx) {
|
|
345
|
+
if (!node.target.includes(ctx.meta.url))
|
|
346
|
+
node.target.unshift(ctx.meta.url);
|
|
347
|
+
return node;
|
|
348
|
+
}
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
const PrimaryWebPageId = "#webpage";
|
|
352
|
+
const webPageResolver = defineSchemaOrgResolver({
|
|
353
|
+
defaults({ meta }) {
|
|
354
|
+
const endPath = withoutTrailingSlash(meta.url.substring(meta.url.lastIndexOf("/") + 1));
|
|
355
|
+
let type = "WebPage";
|
|
356
|
+
switch (endPath) {
|
|
357
|
+
case "about":
|
|
358
|
+
case "about-us":
|
|
359
|
+
type = "AboutPage";
|
|
360
|
+
break;
|
|
361
|
+
case "search":
|
|
362
|
+
type = "SearchResultsPage";
|
|
363
|
+
break;
|
|
364
|
+
case "checkout":
|
|
365
|
+
type = "CheckoutPage";
|
|
366
|
+
break;
|
|
367
|
+
case "contact":
|
|
368
|
+
case "get-in-touch":
|
|
369
|
+
case "contact-us":
|
|
370
|
+
type = "ContactPage";
|
|
371
|
+
break;
|
|
372
|
+
case "faq":
|
|
373
|
+
type = "FAQPage";
|
|
374
|
+
break;
|
|
375
|
+
}
|
|
376
|
+
const defaults = {
|
|
377
|
+
"@type": type
|
|
378
|
+
};
|
|
379
|
+
return defaults;
|
|
380
|
+
},
|
|
381
|
+
idPrefix: ["url", PrimaryWebPageId],
|
|
382
|
+
inheritMeta: [
|
|
383
|
+
{ meta: "title", key: "name" },
|
|
384
|
+
"description",
|
|
385
|
+
"datePublished",
|
|
386
|
+
"dateModified",
|
|
387
|
+
"url"
|
|
388
|
+
],
|
|
389
|
+
resolve(node, ctx) {
|
|
390
|
+
node.dateModified = resolvableDateToIso(node.dateModified);
|
|
391
|
+
node.datePublished = resolvableDateToIso(node.datePublished);
|
|
392
|
+
resolveDefaultType(node, "WebPage");
|
|
393
|
+
node.about = resolveRelation(node.about, ctx, organizationResolver);
|
|
394
|
+
node.breadcrumb = resolveRelation(node.breadcrumb, ctx, breadcrumbResolver);
|
|
395
|
+
node.author = resolveRelation(node.author, ctx, personResolver);
|
|
396
|
+
node.primaryImageOfPage = resolveRelation(node.primaryImageOfPage, ctx, imageResolver);
|
|
397
|
+
node.potentialAction = resolveRelation(node.potentialAction, ctx, readActionResolver);
|
|
398
|
+
if (node["@type"] === "WebPage") {
|
|
399
|
+
setIfEmpty(node, "potentialAction", [
|
|
400
|
+
{
|
|
401
|
+
"@type": "ReadAction",
|
|
402
|
+
"target": [ctx.meta.url]
|
|
403
|
+
}
|
|
404
|
+
]);
|
|
405
|
+
}
|
|
406
|
+
return node;
|
|
407
|
+
},
|
|
408
|
+
resolveRootNode(webPage, { find, meta }) {
|
|
409
|
+
const identity = find(IdentityId);
|
|
410
|
+
const webSite = find(PrimaryWebSiteId);
|
|
411
|
+
const logo = find("#logo");
|
|
412
|
+
if (identity && meta.url === meta.host)
|
|
413
|
+
setIfEmpty(webPage, "about", idReference(identity));
|
|
414
|
+
if (logo)
|
|
415
|
+
setIfEmpty(webPage, "primaryImageOfPage", idReference(logo));
|
|
416
|
+
if (webSite)
|
|
417
|
+
setIfEmpty(webPage, "isPartOf", idReference(webSite));
|
|
418
|
+
const breadcrumb = find(PrimaryBreadcrumbId);
|
|
419
|
+
if (breadcrumb)
|
|
420
|
+
setIfEmpty(webPage, "breadcrumb", idReference(breadcrumb));
|
|
421
|
+
return webPage;
|
|
422
|
+
}
|
|
423
|
+
});
|
|
424
|
+
|
|
425
|
+
const PrimaryArticleId = "#article";
|
|
426
|
+
const articleResolver = defineSchemaOrgResolver({
|
|
427
|
+
defaults: {
|
|
428
|
+
"@type": "Article"
|
|
429
|
+
},
|
|
430
|
+
inheritMeta: [
|
|
431
|
+
"inLanguage",
|
|
432
|
+
"description",
|
|
433
|
+
"image",
|
|
434
|
+
"dateModified",
|
|
435
|
+
"datePublished",
|
|
436
|
+
{ meta: "title", key: "headline" }
|
|
437
|
+
],
|
|
438
|
+
idPrefix: ["url", PrimaryArticleId],
|
|
439
|
+
resolve(node, ctx) {
|
|
440
|
+
node.author = resolveRelation(node.author, ctx, personResolver, {
|
|
441
|
+
root: true
|
|
442
|
+
});
|
|
443
|
+
node.publisher = resolveRelation(node.publisher, ctx);
|
|
444
|
+
node.dateModified = resolvableDateToIso(node.dateModified);
|
|
445
|
+
node.datePublished = resolvableDateToIso(node.datePublished);
|
|
446
|
+
resolveDefaultType(node, "Article");
|
|
447
|
+
node.headline = trimLength(node.headline, 110);
|
|
448
|
+
return node;
|
|
449
|
+
},
|
|
450
|
+
resolveRootNode(node, { find, meta }) {
|
|
451
|
+
const webPage = find(PrimaryWebPageId);
|
|
452
|
+
const identity = find(IdentityId);
|
|
453
|
+
if (node.image && !node.thumbnailUrl) {
|
|
454
|
+
const firstImage = asArray(node.image)[0];
|
|
455
|
+
if (typeof firstImage === "string")
|
|
456
|
+
setIfEmpty(node, "thumbnailUrl", resolveWithBase(meta.host, firstImage));
|
|
457
|
+
else if (firstImage?.["@id"])
|
|
458
|
+
setIfEmpty(node, "thumbnailUrl", find(firstImage["@id"])?.url);
|
|
459
|
+
}
|
|
460
|
+
if (identity) {
|
|
461
|
+
setIfEmpty(node, "publisher", idReference(identity));
|
|
462
|
+
setIfEmpty(node, "author", idReference(identity));
|
|
463
|
+
}
|
|
464
|
+
if (webPage) {
|
|
465
|
+
setIfEmpty(node, "isPartOf", idReference(webPage));
|
|
466
|
+
setIfEmpty(node, "mainEntityOfPage", idReference(webPage));
|
|
467
|
+
setIfEmpty(webPage, "potentialAction", [
|
|
468
|
+
{
|
|
469
|
+
"@type": "ReadAction",
|
|
470
|
+
"target": [meta.url]
|
|
471
|
+
}
|
|
472
|
+
]);
|
|
473
|
+
setIfEmpty(webPage, "dateModified", node.dateModified);
|
|
474
|
+
setIfEmpty(webPage, "datePublished", node.datePublished);
|
|
475
|
+
}
|
|
476
|
+
return node;
|
|
477
|
+
}
|
|
478
|
+
});
|
|
479
|
+
|
|
480
|
+
const bookEditionResolver = defineSchemaOrgResolver({
|
|
481
|
+
defaults: {
|
|
482
|
+
"@type": "Book"
|
|
483
|
+
},
|
|
484
|
+
inheritMeta: [
|
|
485
|
+
"inLanguage"
|
|
486
|
+
],
|
|
487
|
+
resolve(node, ctx) {
|
|
488
|
+
if (node.bookFormat)
|
|
489
|
+
node.bookFormat = withBase(node.bookFormat, "https://schema.org/");
|
|
490
|
+
if (node.datePublished)
|
|
491
|
+
node.datePublished = resolvableDateToDate(node.datePublished);
|
|
492
|
+
node.author = resolveRelation(node.author, ctx);
|
|
493
|
+
return node;
|
|
494
|
+
},
|
|
495
|
+
resolveRootNode(node, { find }) {
|
|
496
|
+
const identity = find(IdentityId);
|
|
497
|
+
if (identity)
|
|
498
|
+
setIfEmpty(node, "provider", idReference(identity));
|
|
499
|
+
return node;
|
|
500
|
+
}
|
|
501
|
+
});
|
|
502
|
+
const PrimaryBookId = "#book";
|
|
503
|
+
const bookResolver = defineSchemaOrgResolver({
|
|
504
|
+
defaults: {
|
|
505
|
+
"@type": "Book"
|
|
506
|
+
},
|
|
507
|
+
inheritMeta: [
|
|
508
|
+
"description",
|
|
509
|
+
"url",
|
|
510
|
+
{ meta: "title", key: "name" }
|
|
511
|
+
],
|
|
512
|
+
idPrefix: ["url", PrimaryBookId],
|
|
513
|
+
resolve(node, ctx) {
|
|
514
|
+
node.workExample = resolveRelation(node.workExample, ctx, bookEditionResolver);
|
|
515
|
+
node.author = resolveRelation(node.author, ctx);
|
|
516
|
+
if (node.url)
|
|
517
|
+
withBase(node.url, ctx.meta.host);
|
|
518
|
+
return node;
|
|
519
|
+
},
|
|
520
|
+
resolveRootNode(node, { find }) {
|
|
521
|
+
const identity = find(IdentityId);
|
|
522
|
+
if (identity)
|
|
523
|
+
setIfEmpty(node, "author", idReference(identity));
|
|
524
|
+
return node;
|
|
525
|
+
}
|
|
526
|
+
});
|
|
527
|
+
|
|
528
|
+
const commentResolver = defineSchemaOrgResolver({
|
|
529
|
+
defaults: {
|
|
530
|
+
"@type": "Comment"
|
|
531
|
+
},
|
|
532
|
+
idPrefix: "url",
|
|
533
|
+
resolve(node, ctx) {
|
|
534
|
+
node.author = resolveRelation(node.author, ctx, personResolver, {
|
|
535
|
+
root: true
|
|
536
|
+
});
|
|
537
|
+
return node;
|
|
538
|
+
},
|
|
539
|
+
resolveRootNode(node, { find }) {
|
|
540
|
+
const article = find(PrimaryArticleId);
|
|
541
|
+
if (article)
|
|
542
|
+
setIfEmpty(node, "about", idReference(article));
|
|
543
|
+
}
|
|
544
|
+
});
|
|
545
|
+
|
|
546
|
+
const courseResolver = defineSchemaOrgResolver({
|
|
547
|
+
defaults: {
|
|
548
|
+
"@type": "Course"
|
|
549
|
+
},
|
|
550
|
+
resolve(node, ctx) {
|
|
551
|
+
node.provider = resolveRelation(node.provider, ctx, organizationResolver, {
|
|
552
|
+
root: true
|
|
553
|
+
});
|
|
554
|
+
return node;
|
|
555
|
+
},
|
|
556
|
+
resolveRootNode(node, { find }) {
|
|
557
|
+
const identity = find(IdentityId);
|
|
558
|
+
if (identity)
|
|
559
|
+
setIfEmpty(node, "provider", idReference(identity));
|
|
560
|
+
return node;
|
|
561
|
+
}
|
|
562
|
+
});
|
|
563
|
+
|
|
564
|
+
const placeResolver = defineSchemaOrgResolver({
|
|
565
|
+
defaults: {
|
|
566
|
+
"@type": "Place"
|
|
567
|
+
},
|
|
568
|
+
resolve(node, ctx) {
|
|
569
|
+
node.address = resolveRelation(node.address, ctx, addressResolver);
|
|
570
|
+
return node;
|
|
571
|
+
}
|
|
572
|
+
});
|
|
573
|
+
|
|
574
|
+
const virtualLocationResolver = defineSchemaOrgResolver({
|
|
575
|
+
cast(node) {
|
|
576
|
+
if (typeof node === "string") {
|
|
577
|
+
return {
|
|
578
|
+
url: node
|
|
579
|
+
};
|
|
580
|
+
}
|
|
581
|
+
return node;
|
|
582
|
+
},
|
|
583
|
+
defaults: {
|
|
584
|
+
"@type": "VirtualLocation"
|
|
585
|
+
}
|
|
586
|
+
});
|
|
587
|
+
|
|
588
|
+
const PrimaryEventId = "#event";
|
|
589
|
+
const eventResolver = defineSchemaOrgResolver({
|
|
590
|
+
defaults: {
|
|
591
|
+
"@type": "Event"
|
|
592
|
+
},
|
|
593
|
+
inheritMeta: [
|
|
594
|
+
"inLanguage",
|
|
595
|
+
"description",
|
|
596
|
+
"image",
|
|
597
|
+
{ meta: "title", key: "name" }
|
|
598
|
+
],
|
|
599
|
+
idPrefix: ["url", PrimaryEventId],
|
|
600
|
+
resolve(node, ctx) {
|
|
601
|
+
if (node.location) {
|
|
602
|
+
const isVirtual = node.location === "string" || node.location?.url !== "undefined";
|
|
603
|
+
node.location = resolveRelation(node.location, ctx, isVirtual ? virtualLocationResolver : placeResolver);
|
|
604
|
+
}
|
|
605
|
+
node.performer = resolveRelation(node.performer, ctx, personResolver, {
|
|
606
|
+
root: true
|
|
607
|
+
});
|
|
608
|
+
node.organizer = resolveRelation(node.organizer, ctx, organizationResolver, {
|
|
609
|
+
root: true
|
|
610
|
+
});
|
|
611
|
+
node.offers = resolveRelation(node.offers, ctx, offerResolver);
|
|
612
|
+
if (node.eventAttendanceMode)
|
|
613
|
+
node.eventAttendanceMode = withBase(node.eventAttendanceMode, "https://schema.org/");
|
|
614
|
+
if (node.eventStatus)
|
|
615
|
+
node.eventStatus = withBase(node.eventStatus, "https://schema.org/");
|
|
616
|
+
const isOnline = node.eventStatus === "https://schema.org/EventMovedOnline";
|
|
617
|
+
const dates = ["startDate", "previousStartDate", "endDate"];
|
|
618
|
+
dates.forEach((date) => {
|
|
619
|
+
if (!isOnline) {
|
|
620
|
+
if (node[date] instanceof Date && node[date].getHours() === 0 && node[date].getMinutes() === 0)
|
|
621
|
+
node[date] = resolvableDateToDate(node[date]);
|
|
622
|
+
} else {
|
|
623
|
+
node[date] = resolvableDateToIso(node[date]);
|
|
624
|
+
}
|
|
625
|
+
});
|
|
626
|
+
setIfEmpty(node, "endDate", node.startDate);
|
|
627
|
+
return node;
|
|
628
|
+
},
|
|
629
|
+
resolveRootNode(node, { find }) {
|
|
630
|
+
const identity = find(IdentityId);
|
|
631
|
+
if (identity)
|
|
632
|
+
setIfEmpty(node, "organizer", idReference(identity));
|
|
633
|
+
}
|
|
634
|
+
});
|
|
635
|
+
|
|
636
|
+
const howToStepDirectionResolver = defineSchemaOrgResolver({
|
|
637
|
+
cast(node) {
|
|
638
|
+
if (typeof node === "string") {
|
|
639
|
+
return {
|
|
640
|
+
text: node
|
|
641
|
+
};
|
|
642
|
+
}
|
|
643
|
+
return node;
|
|
644
|
+
},
|
|
645
|
+
defaults: {
|
|
646
|
+
"@type": "HowToDirection"
|
|
647
|
+
}
|
|
648
|
+
});
|
|
649
|
+
|
|
650
|
+
const howToStepResolver = defineSchemaOrgResolver({
|
|
651
|
+
cast(node) {
|
|
652
|
+
if (typeof node === "string") {
|
|
653
|
+
return {
|
|
654
|
+
text: node
|
|
655
|
+
};
|
|
656
|
+
}
|
|
657
|
+
return node;
|
|
658
|
+
},
|
|
659
|
+
defaults: {
|
|
660
|
+
"@type": "HowToStep"
|
|
661
|
+
},
|
|
662
|
+
resolve(step, ctx) {
|
|
663
|
+
if (step.url)
|
|
664
|
+
step.url = resolveWithBase(ctx.meta.url, step.url);
|
|
665
|
+
if (step.image) {
|
|
666
|
+
step.image = resolveRelation(step.image, ctx, imageResolver, {
|
|
667
|
+
root: true
|
|
668
|
+
});
|
|
669
|
+
}
|
|
670
|
+
if (step.itemListElement)
|
|
671
|
+
step.itemListElement = resolveRelation(step.itemListElement, ctx, howToStepDirectionResolver);
|
|
672
|
+
return step;
|
|
673
|
+
}
|
|
674
|
+
});
|
|
675
|
+
|
|
676
|
+
const HowToId = "#howto";
|
|
677
|
+
const howToResolver = defineSchemaOrgResolver({
|
|
678
|
+
defaults: {
|
|
679
|
+
"@type": "HowTo"
|
|
680
|
+
},
|
|
681
|
+
inheritMeta: [
|
|
682
|
+
"description",
|
|
683
|
+
"image",
|
|
684
|
+
"inLanguage",
|
|
685
|
+
{ meta: "title", key: "name" }
|
|
686
|
+
],
|
|
687
|
+
idPrefix: ["url", HowToId],
|
|
688
|
+
resolve(node, ctx) {
|
|
689
|
+
node.step = resolveRelation(node.step, ctx, howToStepResolver);
|
|
690
|
+
return node;
|
|
691
|
+
},
|
|
692
|
+
resolveRootNode(node, { find }) {
|
|
693
|
+
const webPage = find(PrimaryWebPageId);
|
|
694
|
+
if (webPage)
|
|
695
|
+
setIfEmpty(node, "mainEntityOfPage", idReference(webPage));
|
|
696
|
+
}
|
|
697
|
+
});
|
|
698
|
+
|
|
699
|
+
const itemListResolver = defineSchemaOrgResolver({
|
|
700
|
+
defaults: {
|
|
701
|
+
"@type": "ItemList"
|
|
702
|
+
},
|
|
703
|
+
resolve(node, ctx) {
|
|
704
|
+
if (node.itemListElement) {
|
|
705
|
+
let index = 1;
|
|
706
|
+
node.itemListElement = resolveRelation(node.itemListElement, ctx, resolveListItem, {
|
|
707
|
+
array: true,
|
|
708
|
+
afterResolve(node2) {
|
|
709
|
+
setIfEmpty(node2, "position", index++);
|
|
710
|
+
}
|
|
711
|
+
});
|
|
712
|
+
}
|
|
713
|
+
return node;
|
|
714
|
+
}
|
|
715
|
+
});
|
|
716
|
+
|
|
717
|
+
const openingHoursResolver = defineSchemaOrgResolver({
|
|
718
|
+
defaults: {
|
|
719
|
+
"@type": "OpeningHoursSpecification",
|
|
720
|
+
"opens": "00:00",
|
|
721
|
+
"closes": "23:59"
|
|
722
|
+
}
|
|
723
|
+
});
|
|
724
|
+
|
|
725
|
+
const localBusinessResolver = defineSchemaOrgResolver({
|
|
726
|
+
defaults: {
|
|
727
|
+
"@type": ["Organization", "LocalBusiness"]
|
|
728
|
+
},
|
|
729
|
+
inheritMeta: [
|
|
730
|
+
{ key: "url", meta: "host" },
|
|
731
|
+
{ key: "currenciesAccepted", meta: "currency" }
|
|
732
|
+
],
|
|
733
|
+
idPrefix: ["host", IdentityId],
|
|
734
|
+
resolve(node, ctx) {
|
|
735
|
+
resolveDefaultType(node, ["Organization", "LocalBusiness"]);
|
|
736
|
+
node.address = resolveRelation(node.address, ctx, addressResolver);
|
|
737
|
+
node.openingHoursSpecification = resolveRelation(node.openingHoursSpecification, ctx, openingHoursResolver);
|
|
738
|
+
node.logo = resolveRelation(node.logo, ctx, imageResolver, {
|
|
739
|
+
afterResolve(logo) {
|
|
740
|
+
const hasLogo = !!ctx.find("#logo");
|
|
741
|
+
if (!hasLogo)
|
|
742
|
+
logo["@id"] = prefixId(ctx.meta.host, "#logo");
|
|
743
|
+
setIfEmpty(logo, "caption", node.name);
|
|
744
|
+
}
|
|
745
|
+
});
|
|
746
|
+
return node;
|
|
747
|
+
}
|
|
748
|
+
});
|
|
749
|
+
|
|
750
|
+
const ratingResolver = defineSchemaOrgResolver({
|
|
751
|
+
cast(node) {
|
|
752
|
+
if (node === "number") {
|
|
753
|
+
return {
|
|
754
|
+
ratingValue: node
|
|
755
|
+
};
|
|
756
|
+
}
|
|
757
|
+
return node;
|
|
758
|
+
},
|
|
759
|
+
defaults: {
|
|
760
|
+
"@type": "Rating",
|
|
761
|
+
"bestRating": 5,
|
|
762
|
+
"worstRating": 1
|
|
763
|
+
}
|
|
764
|
+
});
|
|
765
|
+
|
|
766
|
+
const reviewResolver = defineSchemaOrgResolver({
|
|
767
|
+
defaults: {
|
|
768
|
+
"@type": "Review"
|
|
769
|
+
},
|
|
770
|
+
inheritMeta: [
|
|
771
|
+
"inLanguage"
|
|
772
|
+
],
|
|
773
|
+
resolve(review, ctx) {
|
|
774
|
+
review.reviewRating = resolveRelation(review.reviewRating, ctx, ratingResolver);
|
|
775
|
+
review.author = resolveRelation(review.author, ctx, personResolver);
|
|
776
|
+
return review;
|
|
777
|
+
}
|
|
778
|
+
});
|
|
779
|
+
|
|
780
|
+
const movieResolver = defineSchemaOrgResolver({
|
|
781
|
+
defaults: {
|
|
782
|
+
"@type": "Movie"
|
|
783
|
+
},
|
|
784
|
+
resolve(node, ctx) {
|
|
785
|
+
node.aggregateRating = resolveRelation(node.aggregateRating, ctx, aggregateRatingResolver);
|
|
786
|
+
node.review = resolveRelation(node.review, ctx, reviewResolver);
|
|
787
|
+
node.director = resolveRelation(node.director, ctx, personResolver);
|
|
788
|
+
if (node.dateCreated)
|
|
789
|
+
node.dateCreated = resolvableDateToDate(node.dateCreated);
|
|
790
|
+
return node;
|
|
791
|
+
}
|
|
792
|
+
});
|
|
793
|
+
|
|
794
|
+
const ProductId = "#product";
|
|
795
|
+
const productResolver = defineSchemaOrgResolver({
|
|
796
|
+
defaults: {
|
|
797
|
+
"@type": "Product"
|
|
798
|
+
},
|
|
799
|
+
inheritMeta: [
|
|
800
|
+
"description",
|
|
801
|
+
"image",
|
|
802
|
+
{ meta: "title", key: "name" }
|
|
803
|
+
],
|
|
804
|
+
idPrefix: ["url", ProductId],
|
|
805
|
+
resolve(node, ctx) {
|
|
806
|
+
setIfEmpty(node, "sku", hash(node.name));
|
|
807
|
+
node.aggregateOffer = resolveRelation(node.aggregateOffer, ctx, aggregateOfferResolver);
|
|
808
|
+
node.aggregateRating = resolveRelation(node.aggregateRating, ctx, aggregateRatingResolver);
|
|
809
|
+
node.offers = resolveRelation(node.offers, ctx, offerResolver);
|
|
810
|
+
node.review = resolveRelation(node.review, ctx, reviewResolver);
|
|
811
|
+
return node;
|
|
812
|
+
},
|
|
813
|
+
resolveRootNode(product, { find }) {
|
|
814
|
+
const webPage = find(PrimaryWebPageId);
|
|
815
|
+
const identity = find(IdentityId);
|
|
816
|
+
if (identity)
|
|
817
|
+
setIfEmpty(product, "brand", idReference(identity));
|
|
818
|
+
if (webPage)
|
|
819
|
+
setIfEmpty(product, "mainEntityOfPage", idReference(webPage));
|
|
820
|
+
return product;
|
|
821
|
+
}
|
|
822
|
+
});
|
|
823
|
+
|
|
824
|
+
const answerResolver = defineSchemaOrgResolver({
|
|
825
|
+
cast(node) {
|
|
826
|
+
if (typeof node === "string") {
|
|
827
|
+
return {
|
|
828
|
+
text: node
|
|
829
|
+
};
|
|
830
|
+
}
|
|
831
|
+
return node;
|
|
832
|
+
},
|
|
833
|
+
defaults: {
|
|
834
|
+
"@type": "Answer"
|
|
835
|
+
}
|
|
836
|
+
});
|
|
837
|
+
|
|
838
|
+
const questionResolver = defineSchemaOrgResolver({
|
|
839
|
+
defaults: {
|
|
840
|
+
"@type": "Question"
|
|
841
|
+
},
|
|
842
|
+
inheritMeta: [
|
|
843
|
+
"inLanguage"
|
|
844
|
+
],
|
|
845
|
+
idPrefix: "url",
|
|
846
|
+
resolve(question, ctx) {
|
|
847
|
+
if (question.question)
|
|
848
|
+
question.name = question.question;
|
|
849
|
+
if (question.answer)
|
|
850
|
+
question.acceptedAnswer = question.answer;
|
|
851
|
+
question.acceptedAnswer = resolveRelation(question.acceptedAnswer, ctx, answerResolver);
|
|
852
|
+
return question;
|
|
853
|
+
},
|
|
854
|
+
resolveRootNode(question, { find }) {
|
|
855
|
+
const webPage = find(PrimaryWebPageId);
|
|
856
|
+
if (webPage && asArray(webPage["@type"]).includes("FAQPage"))
|
|
857
|
+
dedupeMerge(webPage, "mainEntity", idReference(question));
|
|
858
|
+
}
|
|
859
|
+
});
|
|
860
|
+
|
|
861
|
+
const RecipeId = "#recipe";
|
|
862
|
+
const recipeResolver = defineSchemaOrgResolver({
|
|
863
|
+
defaults: {
|
|
864
|
+
"@type": "Recipe"
|
|
865
|
+
},
|
|
866
|
+
inheritMeta: [
|
|
867
|
+
{ meta: "title", key: "name" },
|
|
868
|
+
"description",
|
|
869
|
+
"image",
|
|
870
|
+
"datePublished"
|
|
871
|
+
],
|
|
872
|
+
idPrefix: ["url", RecipeId],
|
|
873
|
+
resolve(node, ctx) {
|
|
874
|
+
node.recipeInstructions = resolveRelation(node.recipeInstructions, ctx, howToStepResolver);
|
|
875
|
+
return node;
|
|
876
|
+
},
|
|
877
|
+
resolveRootNode(node, { find }) {
|
|
878
|
+
const article = find(PrimaryArticleId);
|
|
879
|
+
const webPage = find(PrimaryWebPageId);
|
|
880
|
+
if (article)
|
|
881
|
+
setIfEmpty(node, "mainEntityOfPage", idReference(article));
|
|
882
|
+
else if (webPage)
|
|
883
|
+
setIfEmpty(node, "mainEntityOfPage", idReference(webPage));
|
|
884
|
+
if (article?.author)
|
|
885
|
+
setIfEmpty(node, "author", article.author);
|
|
886
|
+
return node;
|
|
887
|
+
}
|
|
888
|
+
});
|
|
889
|
+
|
|
890
|
+
const softwareAppResolver = defineSchemaOrgResolver({
|
|
891
|
+
defaults: {
|
|
892
|
+
"@type": "SoftwareApplication"
|
|
893
|
+
},
|
|
894
|
+
resolve(node, ctx) {
|
|
895
|
+
resolveDefaultType(node, "SoftwareApplication");
|
|
896
|
+
node.offers = resolveRelation(node.offers, ctx, offerResolver);
|
|
897
|
+
node.aggregateRating = resolveRelation(node.aggregateRating, ctx, aggregateRatingResolver);
|
|
898
|
+
node.review = resolveRelation(node.review, ctx, reviewResolver);
|
|
899
|
+
return node;
|
|
900
|
+
}
|
|
901
|
+
});
|
|
902
|
+
|
|
903
|
+
const videoResolver = defineSchemaOrgResolver({
|
|
904
|
+
cast(input) {
|
|
905
|
+
if (typeof input === "string") {
|
|
906
|
+
input = {
|
|
907
|
+
url: input
|
|
908
|
+
};
|
|
909
|
+
}
|
|
910
|
+
return input;
|
|
911
|
+
},
|
|
912
|
+
alias: "video",
|
|
913
|
+
defaults: {
|
|
914
|
+
"@type": "VideoObject"
|
|
915
|
+
},
|
|
916
|
+
inheritMeta: [
|
|
917
|
+
{ meta: "title", key: "name" },
|
|
918
|
+
"description",
|
|
919
|
+
"image",
|
|
920
|
+
"inLanguage",
|
|
921
|
+
{ meta: "datePublished", key: "uploadDate" }
|
|
922
|
+
],
|
|
923
|
+
idPrefix: "host",
|
|
924
|
+
resolve(video, ctx) {
|
|
925
|
+
if (video.uploadDate)
|
|
926
|
+
video.uploadDate = resolvableDateToIso(video.uploadDate);
|
|
927
|
+
video.url = resolveWithBase(ctx.meta.host, video.url);
|
|
928
|
+
if (video.caption && !video.description)
|
|
929
|
+
video.description = video.caption;
|
|
930
|
+
if (!video.description)
|
|
931
|
+
video.description = "No description";
|
|
932
|
+
if (video.thumbnailUrl)
|
|
933
|
+
video.thumbnailUrl = resolveRelation(video.thumbnailUrl, ctx, imageResolver);
|
|
934
|
+
return video;
|
|
935
|
+
},
|
|
936
|
+
resolveRootNode(video, { find }) {
|
|
937
|
+
if (video.image && !video.thumbnailUrl) {
|
|
938
|
+
const firstImage = asArray(video.image)[0];
|
|
939
|
+
setIfEmpty(video, "thumbnailUrl", find(firstImage["@id"])?.url);
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
});
|
|
943
|
+
|
|
944
|
+
function loadResolver(resolver) {
|
|
945
|
+
switch (resolver) {
|
|
946
|
+
case "address":
|
|
947
|
+
return addressResolver;
|
|
948
|
+
case "aggregateOffer":
|
|
949
|
+
return aggregateOfferResolver;
|
|
950
|
+
case "aggregateRating":
|
|
951
|
+
return aggregateRatingResolver;
|
|
952
|
+
case "article":
|
|
953
|
+
return articleResolver;
|
|
954
|
+
case "breadcrumb":
|
|
955
|
+
return breadcrumbResolver;
|
|
956
|
+
case "comment":
|
|
957
|
+
return commentResolver;
|
|
958
|
+
case "event":
|
|
959
|
+
return eventResolver;
|
|
960
|
+
case "virtualLocation":
|
|
961
|
+
return virtualLocationResolver;
|
|
962
|
+
case "place":
|
|
963
|
+
return placeResolver;
|
|
964
|
+
case "howTo":
|
|
965
|
+
return howToResolver;
|
|
966
|
+
case "howToStep":
|
|
967
|
+
return howToStepResolver;
|
|
968
|
+
case "image":
|
|
969
|
+
return imageResolver;
|
|
970
|
+
case "localBusiness":
|
|
971
|
+
return localBusinessResolver;
|
|
972
|
+
case "offer":
|
|
973
|
+
return offerResolver;
|
|
974
|
+
case "openingHours":
|
|
975
|
+
return openingHoursResolver;
|
|
976
|
+
case "organization":
|
|
977
|
+
return organizationResolver;
|
|
978
|
+
case "person":
|
|
979
|
+
return personResolver;
|
|
980
|
+
case "product":
|
|
981
|
+
return productResolver;
|
|
982
|
+
case "question":
|
|
983
|
+
return questionResolver;
|
|
984
|
+
case "recipe":
|
|
985
|
+
return recipeResolver;
|
|
986
|
+
case "review":
|
|
987
|
+
return reviewResolver;
|
|
988
|
+
case "video":
|
|
989
|
+
return videoResolver;
|
|
990
|
+
case "webPage":
|
|
991
|
+
return webPageResolver;
|
|
992
|
+
case "webSite":
|
|
993
|
+
return webSiteResolver;
|
|
994
|
+
case "book":
|
|
995
|
+
return bookResolver;
|
|
996
|
+
case "course":
|
|
997
|
+
return courseResolver;
|
|
998
|
+
case "itemList":
|
|
999
|
+
return itemListResolver;
|
|
1000
|
+
case "movie":
|
|
1001
|
+
return movieResolver;
|
|
1002
|
+
case "searchAction":
|
|
1003
|
+
return searchActionResolver;
|
|
1004
|
+
case "readAction":
|
|
1005
|
+
return readActionResolver;
|
|
1006
|
+
case "softwareApp":
|
|
1007
|
+
return softwareAppResolver;
|
|
1008
|
+
case "bookEdition":
|
|
1009
|
+
return bookEditionResolver;
|
|
1010
|
+
}
|
|
1011
|
+
return null;
|
|
1012
|
+
}
|
|
1013
|
+
|
|
1014
|
+
const resolver = {
|
|
1015
|
+
__proto__: null,
|
|
1016
|
+
loadResolver: loadResolver
|
|
1017
|
+
};
|
|
1018
|
+
|
|
1019
|
+
const resolveMeta = (meta) => {
|
|
1020
|
+
if (!meta.host && meta.canonicalHost)
|
|
1021
|
+
meta.host = meta.canonicalHost;
|
|
1022
|
+
if (!meta.tagPosition && meta.position)
|
|
1023
|
+
meta.tagPosition = meta.position;
|
|
1024
|
+
if (!meta.currency && meta.defaultCurrency)
|
|
1025
|
+
meta.currency = meta.defaultCurrency;
|
|
1026
|
+
if (!meta.inLanguage && meta.defaultLanguage)
|
|
1027
|
+
meta.inLanguage = meta.defaultLanguage;
|
|
1028
|
+
if (!meta.path)
|
|
1029
|
+
meta.path = "/";
|
|
1030
|
+
if (!meta.host && typeof document !== "undefined")
|
|
1031
|
+
meta.host = document.location.host;
|
|
1032
|
+
if (!meta.url && meta.canonicalUrl)
|
|
1033
|
+
meta.url = meta.canonicalUrl;
|
|
1034
|
+
meta.url = joinURL(meta.host, meta.path);
|
|
1035
|
+
return {
|
|
1036
|
+
host: meta.host,
|
|
1037
|
+
url: meta.url,
|
|
1038
|
+
currency: meta.currency,
|
|
1039
|
+
image: meta.image,
|
|
1040
|
+
inLanguage: meta.inLanguage,
|
|
1041
|
+
title: meta.title,
|
|
1042
|
+
description: meta.description,
|
|
1043
|
+
datePublished: meta.datePublished,
|
|
1044
|
+
dateModified: meta.dateModified
|
|
1045
|
+
};
|
|
1046
|
+
};
|
|
1047
|
+
const resolveNode = (node, ctx, resolver) => {
|
|
1048
|
+
if (resolver?.cast)
|
|
1049
|
+
node = resolver.cast(node, ctx);
|
|
1050
|
+
if (resolver?.defaults) {
|
|
1051
|
+
let defaults = resolver.defaults || {};
|
|
1052
|
+
if (typeof defaults === "function")
|
|
1053
|
+
defaults = defaults(ctx);
|
|
1054
|
+
node = defu(node, defaults);
|
|
1055
|
+
}
|
|
1056
|
+
resolver.inheritMeta?.forEach((entry) => {
|
|
1057
|
+
if (typeof entry === "string")
|
|
1058
|
+
setIfEmpty(node, entry, ctx.meta[entry]);
|
|
1059
|
+
else
|
|
1060
|
+
setIfEmpty(node, entry.key, ctx.meta[entry.meta]);
|
|
1061
|
+
});
|
|
1062
|
+
if (resolver?.resolve)
|
|
1063
|
+
node = resolver.resolve(node, ctx);
|
|
1064
|
+
for (const k in node) {
|
|
1065
|
+
const v = node[k];
|
|
1066
|
+
if (typeof v === "object" && v?._resolver)
|
|
1067
|
+
node[k] = resolveRelation(v, ctx, v._resolver);
|
|
1068
|
+
}
|
|
1069
|
+
stripEmptyProperties(node);
|
|
1070
|
+
return node;
|
|
1071
|
+
};
|
|
1072
|
+
const resolveNodeId = (node, ctx, resolver, resolveAsRoot = false) => {
|
|
1073
|
+
const prefix = Array.isArray(resolver.idPrefix) ? resolver.idPrefix[0] : resolver.idPrefix;
|
|
1074
|
+
if (!prefix)
|
|
1075
|
+
return node;
|
|
1076
|
+
if (node["@id"] && !node["@id"].startsWith(ctx.meta.host)) {
|
|
1077
|
+
node["@id"] = prefixId(ctx.meta[prefix], node["@id"]);
|
|
1078
|
+
return node;
|
|
1079
|
+
}
|
|
1080
|
+
const rootId = Array.isArray(resolver.idPrefix) ? resolver.idPrefix?.[1] : void 0;
|
|
1081
|
+
if (resolveAsRoot && rootId) {
|
|
1082
|
+
node["@id"] = prefixId(ctx.meta[prefix], rootId);
|
|
1083
|
+
}
|
|
1084
|
+
if (!node["@id"]) {
|
|
1085
|
+
let alias = resolver?.alias;
|
|
1086
|
+
if (!alias) {
|
|
1087
|
+
const type = asArray(node["@type"])?.[0] || "";
|
|
1088
|
+
alias = type.toLowerCase();
|
|
1089
|
+
}
|
|
1090
|
+
const hashNodeData = {};
|
|
1091
|
+
Object.entries(node).forEach(([key, val]) => {
|
|
1092
|
+
if (!key.startsWith("_"))
|
|
1093
|
+
hashNodeData[key] = val;
|
|
1094
|
+
});
|
|
1095
|
+
node["@id"] = prefixId(ctx.meta[prefix], `#/schema/${alias}/${hash(hashNodeData)}`);
|
|
1096
|
+
}
|
|
1097
|
+
return node;
|
|
1098
|
+
};
|
|
1099
|
+
function resolveRelation(input, ctx, fallbackResolver, options = {}) {
|
|
1100
|
+
if (!input)
|
|
1101
|
+
return input;
|
|
1102
|
+
const ids = asArray(input).map((a) => {
|
|
1103
|
+
if (Object.keys(a).length === 1 && a["@id"])
|
|
1104
|
+
return a;
|
|
1105
|
+
let resolver = fallbackResolver;
|
|
1106
|
+
if (a._resolver) {
|
|
1107
|
+
resolver = a._resolver;
|
|
1108
|
+
if (typeof resolver === "string")
|
|
1109
|
+
resolver = loadResolver(resolver);
|
|
1110
|
+
delete a._resolver;
|
|
1111
|
+
}
|
|
1112
|
+
if (!resolver)
|
|
1113
|
+
return a;
|
|
1114
|
+
let node = resolveNode(a, ctx, resolver);
|
|
1115
|
+
if (options.afterResolve)
|
|
1116
|
+
options.afterResolve(node);
|
|
1117
|
+
if (options.generateId || options.root)
|
|
1118
|
+
node = resolveNodeId(node, ctx, resolver, false);
|
|
1119
|
+
if (options.root) {
|
|
1120
|
+
if (resolver.resolveRootNode)
|
|
1121
|
+
resolver.resolveRootNode(node, ctx);
|
|
1122
|
+
ctx.push(node);
|
|
1123
|
+
return idReference(node["@id"]);
|
|
1124
|
+
}
|
|
1125
|
+
return node;
|
|
1126
|
+
});
|
|
1127
|
+
if (!options.array && ids.length === 1)
|
|
1128
|
+
return ids[0];
|
|
1129
|
+
return ids;
|
|
1130
|
+
}
|
|
1131
|
+
|
|
1132
|
+
const groupBy = (array, predicate) => array.reduce((acc, value, index, array2) => {
|
|
1133
|
+
const key = predicate(value, index, array2);
|
|
1134
|
+
if (!acc[key])
|
|
1135
|
+
acc[key] = [];
|
|
1136
|
+
acc[key].push(value);
|
|
1137
|
+
return acc;
|
|
1138
|
+
}, {});
|
|
1139
|
+
const dedupeNodes = (nodes) => {
|
|
1140
|
+
const sortedNodeKeys = nodes.keys();
|
|
1141
|
+
const dedupedNodes = {};
|
|
1142
|
+
for (const key of sortedNodeKeys) {
|
|
1143
|
+
const n = nodes[key];
|
|
1144
|
+
const nodeKey = resolveAsGraphKey(n["@id"] || hash(n));
|
|
1145
|
+
const groupedKeys = groupBy(Object.keys(n), (key2) => {
|
|
1146
|
+
const val = n[key2];
|
|
1147
|
+
if (key2.startsWith("_"))
|
|
1148
|
+
return "ignored";
|
|
1149
|
+
if (Array.isArray(val) || typeof val === "object")
|
|
1150
|
+
return "relations";
|
|
1151
|
+
return "primitives";
|
|
1152
|
+
});
|
|
1153
|
+
const keys = [
|
|
1154
|
+
...(groupedKeys.primitives || []).sort(),
|
|
1155
|
+
...(groupedKeys.relations || []).sort()
|
|
1156
|
+
];
|
|
1157
|
+
const newNode = {};
|
|
1158
|
+
for (const key2 of keys)
|
|
1159
|
+
newNode[key2] = n[key2];
|
|
1160
|
+
dedupedNodes[nodeKey] = newNode;
|
|
1161
|
+
}
|
|
1162
|
+
return Object.values(dedupedNodes);
|
|
1163
|
+
};
|
|
1164
|
+
|
|
1165
|
+
const createSchemaOrgGraph = () => {
|
|
1166
|
+
const ctx = {
|
|
1167
|
+
find(id) {
|
|
1168
|
+
const key = resolveAsGraphKey(id);
|
|
1169
|
+
return ctx.nodes.filter((n) => !!n["@id"]).find((n) => resolveAsGraphKey(n["@id"]) === key);
|
|
1170
|
+
},
|
|
1171
|
+
push(input) {
|
|
1172
|
+
asArray(input).forEach((node) => {
|
|
1173
|
+
const registeredNode = node;
|
|
1174
|
+
ctx.nodes.push(registeredNode);
|
|
1175
|
+
});
|
|
1176
|
+
},
|
|
1177
|
+
resolveGraph(meta) {
|
|
1178
|
+
ctx.meta = resolveMeta({ ...meta });
|
|
1179
|
+
ctx.nodes.forEach((node, key) => {
|
|
1180
|
+
const resolver = node._resolver;
|
|
1181
|
+
if (resolver) {
|
|
1182
|
+
node = resolveNode(node, ctx, resolver);
|
|
1183
|
+
node = resolveNodeId(node, ctx, resolver, true);
|
|
1184
|
+
}
|
|
1185
|
+
ctx.nodes[key] = node;
|
|
1186
|
+
});
|
|
1187
|
+
ctx.nodes.forEach((node) => {
|
|
1188
|
+
if (node.image && typeof node.image === "string") {
|
|
1189
|
+
node.image = resolveRelation(node.image, ctx, imageResolver, {
|
|
1190
|
+
root: true
|
|
1191
|
+
});
|
|
1192
|
+
}
|
|
1193
|
+
if (node._resolver?.resolveRootNode)
|
|
1194
|
+
node._resolver.resolveRootNode(node, ctx);
|
|
1195
|
+
delete node._resolver;
|
|
1196
|
+
});
|
|
1197
|
+
return dedupeNodes(ctx.nodes);
|
|
1198
|
+
},
|
|
1199
|
+
nodes: [],
|
|
1200
|
+
meta: {}
|
|
1201
|
+
};
|
|
1202
|
+
return ctx;
|
|
1203
|
+
};
|
|
1204
|
+
|
|
1205
|
+
function SchemaOrgUnheadPlugin(config, meta) {
|
|
1206
|
+
config = resolveMeta({ ...config });
|
|
1207
|
+
let graph;
|
|
1208
|
+
const resolvedMeta = {};
|
|
1209
|
+
return {
|
|
1210
|
+
hooks: {
|
|
1211
|
+
"entries:resolve": function() {
|
|
1212
|
+
graph = createSchemaOrgGraph();
|
|
1213
|
+
},
|
|
1214
|
+
"tag:normalise": async function({ tag }) {
|
|
1215
|
+
if (tag.key === "schema-org-graph") {
|
|
1216
|
+
const { loadResolver } = await Promise.resolve().then(function () { return resolver; });
|
|
1217
|
+
const nodes = await tag.props.nodes;
|
|
1218
|
+
for (const node of Array.isArray(nodes) ? nodes : [nodes]) {
|
|
1219
|
+
const newNode = {
|
|
1220
|
+
...node,
|
|
1221
|
+
_resolver: loadResolver(await node._resolver)
|
|
1222
|
+
};
|
|
1223
|
+
graph.push(newNode);
|
|
1224
|
+
}
|
|
1225
|
+
tag.tagPosition = config.tagPosition === "head" ? "head" : "bodyClose";
|
|
1226
|
+
}
|
|
1227
|
+
if (tag.tag === "title")
|
|
1228
|
+
resolvedMeta.title = tag.children;
|
|
1229
|
+
else if (tag.tag === "meta" && tag.props.name === "description")
|
|
1230
|
+
resolvedMeta.description = tag.props.content;
|
|
1231
|
+
else if (tag.tag === "link" && tag.props.rel === "canonical")
|
|
1232
|
+
resolvedMeta.url = tag.props.href;
|
|
1233
|
+
else if (tag.tag === "meta" && tag.props.property === "og:image")
|
|
1234
|
+
resolvedMeta.image = tag.props.content;
|
|
1235
|
+
},
|
|
1236
|
+
"tags:resolve": async function(ctx) {
|
|
1237
|
+
for (const tag of ctx.tags) {
|
|
1238
|
+
if (tag.tag === "script" && tag.key === "schema-org-graph") {
|
|
1239
|
+
tag.children = JSON.stringify({
|
|
1240
|
+
"@context": "https://schema.org",
|
|
1241
|
+
"@graph": graph.resolveGraph({ ...config, ...resolvedMeta, ...await meta() })
|
|
1242
|
+
}, null, 2);
|
|
1243
|
+
delete tag.props.nodes;
|
|
1244
|
+
}
|
|
1245
|
+
}
|
|
1246
|
+
}
|
|
1247
|
+
}
|
|
1248
|
+
};
|
|
1249
|
+
}
|
|
1250
|
+
|
|
1251
|
+
const provideResolver = (input, resolver) => {
|
|
1252
|
+
if (!input)
|
|
1253
|
+
input = {};
|
|
1254
|
+
input._resolver = resolver;
|
|
1255
|
+
return input;
|
|
1256
|
+
};
|
|
1257
|
+
const defineAddress = (input) => provideResolver(input, "address");
|
|
1258
|
+
const defineAggregateOffer = (input) => provideResolver(input, "aggregateOffer");
|
|
1259
|
+
const defineAggregateRating = (input) => provideResolver(input, "aggregateRating");
|
|
1260
|
+
const defineArticle = (input) => provideResolver(input, "article");
|
|
1261
|
+
const defineBreadcrumb = (input) => provideResolver(input, "breadcrumb");
|
|
1262
|
+
const defineComment = (input) => provideResolver(input, "comment");
|
|
1263
|
+
const defineEvent = (input) => provideResolver(input, "event");
|
|
1264
|
+
const defineVirtualLocation = (input) => provideResolver(input, "virtualLocation");
|
|
1265
|
+
const definePlace = (input) => provideResolver(input, "place");
|
|
1266
|
+
const defineHowTo = (input) => provideResolver(input, "howTo");
|
|
1267
|
+
const defineHowToStep = (input) => provideResolver(input, "howToStep");
|
|
1268
|
+
const defineImage = (input) => provideResolver(input, "image");
|
|
1269
|
+
const defineLocalBusiness = (input) => provideResolver(input, "localBusiness");
|
|
1270
|
+
const defineOffer = (input) => provideResolver(input, "offer");
|
|
1271
|
+
const defineOpeningHours = (input) => provideResolver(input, "openingHours");
|
|
1272
|
+
const defineOrganization = (input) => provideResolver(input, "organization");
|
|
1273
|
+
const definePerson = (input) => provideResolver(input, "person");
|
|
1274
|
+
const defineProduct = (input) => provideResolver(input, "product");
|
|
1275
|
+
const defineQuestion = (input) => provideResolver(input, "question");
|
|
1276
|
+
const defineRecipe = (input) => provideResolver(input, "recipe");
|
|
1277
|
+
const defineReview = (input) => provideResolver(input, "review");
|
|
1278
|
+
const defineVideo = (input) => provideResolver(input, "video");
|
|
1279
|
+
const defineWebPage = (input) => provideResolver(input, "webPage");
|
|
1280
|
+
const defineWebSite = (input) => provideResolver(input, "webSite");
|
|
1281
|
+
const defineBook = (input) => provideResolver(input, "book");
|
|
1282
|
+
const defineCourse = (input) => provideResolver(input, "course");
|
|
1283
|
+
const defineItemList = (input) => provideResolver(input, "itemList");
|
|
1284
|
+
const defineMovie = (input) => provideResolver(input, "movie");
|
|
1285
|
+
const defineSearchAction = (input) => provideResolver(input, "searchAction");
|
|
1286
|
+
const defineReadAction = (input) => provideResolver(input, "readAction");
|
|
1287
|
+
const defineSoftwareApp = (input) => provideResolver(input, "softwareApp");
|
|
1288
|
+
const defineBookEdition = (input) => provideResolver(input, "bookEdition");
|
|
1289
|
+
function useSchemaOrg(input) {
|
|
1290
|
+
return useHead({
|
|
1291
|
+
script: [
|
|
1292
|
+
{
|
|
1293
|
+
type: "application/ld+json",
|
|
1294
|
+
key: "schema-org-graph",
|
|
1295
|
+
nodes: input
|
|
1296
|
+
}
|
|
1297
|
+
]
|
|
1298
|
+
}, { mode: process.env.NODE_ENV === "development" ? "all" : "server" });
|
|
1299
|
+
}
|
|
1300
|
+
|
|
1301
|
+
export { HowToId, PrimaryArticleId, PrimaryBookId, PrimaryBreadcrumbId, PrimaryEventId, PrimaryWebPageId, PrimaryWebSiteId, ProductId, RecipeId, SchemaOrgUnheadPlugin, addressResolver, aggregateOfferResolver, aggregateRatingResolver, articleResolver, bookEditionResolver, bookResolver, breadcrumbResolver, commentResolver, courseResolver, createSchemaOrgGraph, dedupeNodes, defineAddress, defineAggregateOffer, defineAggregateRating, defineArticle, defineBook, defineBookEdition, defineBreadcrumb, defineComment, defineCourse, defineEvent, defineHowTo, defineHowToStep, defineImage, defineItemList, defineLocalBusiness, defineMovie, defineOffer, defineOpeningHours, defineOrganization, definePerson, definePlace, defineProduct, defineQuestion, defineReadAction, defineRecipe, defineReview, defineSchemaOrgResolver, defineSearchAction, defineSoftwareApp, defineVideo, defineVirtualLocation, defineWebPage, defineWebSite, eventResolver, howToResolver, howToStepDirectionResolver, howToStepResolver, imageResolver, itemListResolver, localBusinessResolver, movieResolver, offerResolver, openingHoursResolver, organizationResolver, personResolver, placeResolver, productResolver, questionResolver, ratingResolver, readActionResolver, recipeResolver, resolveListItem, resolveMeta, resolveNode, resolveNodeId, resolveRelation, reviewResolver, searchActionResolver, softwareAppResolver, useSchemaOrg, videoResolver, virtualLocationResolver, webPageResolver, webSiteResolver };
|