-
Notifications
You must be signed in to change notification settings - Fork 0
/
metadata.js
61 lines (57 loc) · 1.67 KB
/
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/* eslint-disable no-console */
/**
* Keep in JS for compatibility with puppeteer.
*
* This file is used in screenshot function and injected verbatim
* into the Chrome browser context.
*/
function parsePageMetadata(getMetadata, metadataRuleSets, document, url) {
const metadata = getMetadata(document, url, {
...metadataRuleSets,
published: {
rules: [
[
'meta[property="article:published_time"]',
element => element.getAttribute("content")
],
['meta[name="datePublished"]', element => element.getAttribute("content")],
['meta[name="iso-8601-publish-date"]', element => element.getAttribute("content")],
['meta[name="cXenseParse:recs:publishtime"]', element => element.getAttribute("content")],
['meta[name="sailthru.date"]', element => element.getAttribute("content")]
]
},
locale: {
rules: [
[
'meta[property="og:locale"]',
element => element.getAttribute("content")
]
]
}
});
const ld = document.querySelector('script[type="application/ld+json"]');
if (ld && ld.textContent) {
try {
console.info("Parsing LD+JSON", ld.textContent)
const info = JSON.parse(ld.textContent);
if (info["@graph"]) {
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;
}
try {
module.exports = {
parsePageMetadata
};
} catch (e) {} // WHY?