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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.fallback = void 0;
const chardet_1 = require("chardet");
const utils_1 = require("./utils");
const doesElementExist = (selector, attribute, $) => ($(selector).attr(attribute) && $(selector).attr(attribute).length > 0);
/**
* ogs fallbacks
*
* @param {object} ogObject - the current ogObject
* @param {object} options - options for ogs
* @param {object} $ - cheerio.load() of the current html
* @return {object} object with ogs results with updated fallback values
*
*/
function fallback(ogObject, options, $, rawBody) {
// title fallback
if (!ogObject.ogTitle) {
if ($('title').text() && $('title').text().length > 0) {
ogObject.ogTitle = $('title').first().text();
}
else if ($('head > meta[name="title"]').attr('content') && $('head > meta[name="title"]').attr('content').length > 0) {
ogObject.ogTitle = $('head > meta[name="title"]').attr('content');
}
else if ($('.post-title').text() && $('.post-title').text().length > 0) {
ogObject.ogTitle = $('.post-title').text();
}
else if ($('.entry-title').text() && $('.entry-title').text().length > 0) {
ogObject.ogTitle = $('.entry-title').text();
}
else if ($('h1[class*="title" i] a').text() && $('h1[class*="title" i] a').text().length > 0) {
ogObject.ogTitle = $('h1[class*="title" i] a').text();
}
else if ($('h1[class*="title" i]').text() && $('h1[class*="title" i]').text().length > 0) {
ogObject.ogTitle = $('h1[class*="title" i]').text();
}
}
// Get meta description tag if og description was not provided
if (!ogObject.ogDescription) {
if (doesElementExist('head > meta[name="description"]', 'content', $)) {
ogObject.ogDescription = $('head > meta[name="description"]').attr('content');
}
else if (doesElementExist('head > meta[itemprop="description"]', 'content', $)) {
ogObject.ogDescription = $('head > meta[itemprop="description"]').attr('content');
}
else if ($('#description').text() && $('#description').text().length > 0) {
ogObject.ogDescription = $('#description').text();
}
}
// Get all of images if there is no og:image info
if (!ogObject.ogImage && options.ogImageFallback) {
ogObject.ogImage = [];
$('img').map((index, imageElement) => {
if (doesElementExist(imageElement, 'src', $)) {
const source = $(imageElement).attr('src');
const type = (0, utils_1.findImageTypeFromUrl)(source);
if (!(0, utils_1.isUrlValid)(source, options.urlValidatorSettings) || !(0, utils_1.isImageTypeValid)(type))
return false;
ogObject.ogImage.push({
url: source,
width: $(imageElement).attr('width') || null,
height: $(imageElement).attr('height') || null,
type,
});
}
return false;
});
if (ogObject.ogImage.length === 0)
delete ogObject.ogImage;
}
else if (ogObject.ogImage) {
// if there isn't a type, try to pull it from the URL
if (Array.isArray(ogObject.ogImage)) {
ogObject.ogImage.map((image) => {
if (image.url && !image.type) {
const type = (0, utils_1.findImageTypeFromUrl)(image.url);
if ((0, utils_1.isImageTypeValid)(type))
image.type = type;
}
return false;
});
}
else if (ogObject.ogImage.url && !ogObject.ogImage.type) {
const type = (0, utils_1.findImageTypeFromUrl)(ogObject.ogImage.url);
if ((0, utils_1.isImageTypeValid)(type))
ogObject.ogImage.type = type;
}
}
// audio fallback
if (!ogObject.ogAudioURL && !ogObject.ogAudioSecureURL) {
const audioElementValue = $('audio').attr('src');
const audioSourceElementValue = $('audio > source').attr('src');
if (doesElementExist('audio', 'src', $)) {
if (audioElementValue.startsWith('https')) {
ogObject.ogAudioSecureURL = audioElementValue;
}
else {
ogObject.ogAudioURL = audioElementValue;
}
const audioElementTypeValue = $('audio').attr('type');
if (!ogObject.ogAudioType && doesElementExist('audio', 'type', $))
ogObject.ogAudioType = audioElementTypeValue;
}
else if (doesElementExist('audio > source', 'src', $)) {
if (audioSourceElementValue.startsWith('https')) {
ogObject.ogAudioSecureURL = audioSourceElementValue;
}
else {
ogObject.ogAudioURL = audioSourceElementValue;
}
const audioSourceElementTypeValue = $('audio > source').attr('type');
if (!ogObject.ogAudioType && doesElementExist('audio > source', 'type', $))
ogObject.ogAudioType = audioSourceElementTypeValue;
}
}
// locale fallback
if (!ogObject.ogLocale) {
if (doesElementExist('html', 'lang', $)) {
ogObject.ogLocale = $('html').attr('lang');
}
else if (doesElementExist('head > meta[itemprop="inLanguage"]', 'content', $)) {
ogObject.ogLocale = $('head > meta[itemprop="inLanguage"]').attr('content');
}
}
// logo fallback
if (!ogObject.ogLogo) {
if (doesElementExist('meta[itemprop="logo"]', 'content', $)) {
ogObject.ogLogo = $('meta[itemprop="logo"]').attr('content');
}
else if (doesElementExist('img[itemprop="logo"]', 'src', $)) {
ogObject.ogLogo = $('img[itemprop="logo"]').attr('src');
}
}
// url fallback
if (!ogObject.ogUrl) {
if (doesElementExist('link[rel="canonical"]', 'href', $)) {
ogObject.ogUrl = $('link[rel="canonical"]').attr('href');
}
else if (doesElementExist('link[rel="alternate"][hreflang="x-default"]', 'href', $)) {
ogObject.ogUrl = $('link[rel="alternate"][hreflang="x-default"]').attr('href');
}
}
// date fallback
if (!ogObject.ogDate) {
if (doesElementExist('head > meta[name="date"]', 'content', $)) {
ogObject.ogDate = $('head > meta[name="date"]').attr('content');
}
else if (doesElementExist('[itemprop*="datemodified" i]', 'content', $)) {
ogObject.ogDate = $('[itemprop*="datemodified" i]').attr('content');
}
else if (doesElementExist('[itemprop="datepublished" i]', 'content', $)) {
ogObject.ogDate = $('[itemprop="datepublished" i]').attr('content');
}
else if (doesElementExist('[itemprop*="date" i]', 'content', $)) {
ogObject.ogDate = $('[itemprop*="date" i]').attr('content');
}
else if (doesElementExist('time[itemprop*="date" i]', 'datetime', $)) {
ogObject.ogDate = $('time[itemprop*="date" i]').attr('datetime');
}
else if (doesElementExist('time[datetime]', 'datetime', $)) {
ogObject.ogDate = $('time[datetime]').attr('datetime');
}
}
// favicon fallback
if (!ogObject.favicon) {
if (doesElementExist('link[rel="shortcut icon"]', 'href', $)) {
ogObject.favicon = $('link[rel="shortcut icon"]').attr('href');
}
else if (doesElementExist('link[rel="icon"]', 'href', $)) {
ogObject.favicon = $('link[rel="icon"]').attr('href');
}
else if (doesElementExist('link[rel="mask-icon"]', 'href', $)) {
ogObject.favicon = $('link[rel="mask-icon"]').attr('href');
}
else if (doesElementExist('link[rel="apple-touch-icon"]', 'href', $)) {
ogObject.favicon = $('link[rel="apple-touch-icon"]').attr('href');
}
else if (doesElementExist('link[type="image/png"]', 'href', $)) {
ogObject.favicon = $('link[type="image/png"]').attr('href');
}
else if (doesElementExist('link[type="image/ico"]', 'href', $)) {
ogObject.favicon = $('link[type="image/ico"]').attr('href');
}
else if (doesElementExist('link[type="image/x-icon"]', 'href', $)) {
ogObject.favicon = $('link[type="image/x-icon"]').attr('href');
}
}
// set the charset
if (doesElementExist('meta', 'charset', $)) {
ogObject.charset = $('meta').attr('charset');
}
else if (rawBody) {
ogObject.charset = chardet_1.default.detect(rawBody);
}
return ogObject;
}
exports.fallback = fallback;
exports.default = fallback;
|