-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetadata.js
38 lines (33 loc) · 973 Bytes
/
metadata.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/* eslint-disable no-console */
const { getMetadata } = require("./parser");
const { metadataRuleSets } = require("./ruleSets");
/**
* Parse page metadata and also LD+JSON if present.
*
* @param {Document} document
* @param {string} url
* @returns {import("./types").PageMetadata}
*/
export function getPageMetadata(document, url) {
const metadata = getMetadata(document, url, metadataRuleSets);
const ld = document.querySelector('script[type="application/ld+json"]');
if (ld && ld.textContent) {
try {
const info = JSON.parse(ld.textContent);
if (info["@graph"]) {
// @ts-ignore
info["@graph"].forEach((l) => {
if (l.datePublished) {
metadata.published = l.datePublished;
}
});
}
if (info.datePublished) {
metadata.published = info.datePublished;
}
} catch (e) {
console.warn("Failed to parse ld+json for", url, e);
}
}
return metadata;
}