-
-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
schema.js
189 lines (171 loc) · 5.94 KB
/
schema.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
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
var config = require('../../config'),
escapeExpression = require('../../services/themes/engine').escapeExpression,
social = require('../../lib/social'),
_ = require('lodash');
function schemaImageObject(metaDataVal) {
var imageObject;
if (!metaDataVal) {
return null;
}
if (!metaDataVal.dimensions) {
return metaDataVal.url;
}
imageObject = {
'@type': 'ImageObject',
url: metaDataVal.url,
width: metaDataVal.dimensions.width,
height: metaDataVal.dimensions.height
};
return imageObject;
}
// Creates the final schema object with values that are not null
function trimSchema(schema) {
var schemaObject = {};
_.each(schema, function (value, key) {
if (value !== null && typeof value !== 'undefined') {
schemaObject[key] = value;
}
});
return schemaObject;
}
function trimSameAs(data, context) {
var sameAs = [];
if (context === 'post') {
if (data.post.primary_author.website) {
sameAs.push(escapeExpression(data.post.primary_author.website));
}
if (data.post.primary_author.facebook) {
sameAs.push(social.urls.facebook(data.post.primary_author.facebook));
}
if (data.post.primary_author.twitter) {
sameAs.push(social.urls.twitter(data.post.primary_author.twitter));
}
} else if (context === 'author') {
if (data.author.website) {
sameAs.push(escapeExpression(data.author.website));
}
if (data.author.facebook) {
sameAs.push(social.urls.facebook(data.author.facebook));
}
if (data.author.twitter) {
sameAs.push(social.urls.twitter(data.author.twitter));
}
}
return sameAs;
}
function getPostSchema(metaData, data) {
// CASE: metaData.excerpt for post context is populated by either the custom excerpt, the meta description,
// or the automated excerpt of 50 words. It is empty for any other context.
var description = metaData.excerpt ? escapeExpression(metaData.excerpt) : null,
schema;
schema = {
'@context': 'https://schema.org',
'@type': 'Article',
publisher: {
'@type': 'Organization',
name: escapeExpression(metaData.blog.title),
logo: schemaImageObject(metaData.blog.logo) || null
},
author: {
'@type': 'Person',
name: escapeExpression(data.post.primary_author.name),
image: schemaImageObject(metaData.authorImage),
url: metaData.authorUrl,
sameAs: trimSameAs(data, 'post'),
description: data.post.primary_author.metaDescription ?
escapeExpression(data.post.primary_author.metaDescription) :
null
},
headline: escapeExpression(metaData.metaTitle),
url: metaData.url,
datePublished: metaData.publishedDate,
dateModified: metaData.modifiedDate,
image: schemaImageObject(metaData.coverImage),
keywords: metaData.keywords && metaData.keywords.length > 0 ?
metaData.keywords.join(', ') : null,
description: description,
mainEntityOfPage: {
'@type': 'WebPage',
'@id': metaData.blog.url || null
}
};
schema.author = trimSchema(schema.author);
return trimSchema(schema);
}
function getHomeSchema(metaData) {
var schema = {
'@context': 'https://schema.org',
'@type': 'WebSite',
publisher: {
'@type': 'Organization',
name: escapeExpression(metaData.blog.title),
logo: schemaImageObject(metaData.blog.logo) || null
},
url: metaData.url,
image: schemaImageObject(metaData.coverImage),
mainEntityOfPage: {
'@type': 'WebPage',
'@id': metaData.blog.url || null
},
description: metaData.metaDescription ?
escapeExpression(metaData.metaDescription) :
null
};
return trimSchema(schema);
}
function getTagSchema(metaData, data) {
var schema = {
'@context': 'https://schema.org',
'@type': 'Series',
publisher: {
'@type': 'Organization',
name: escapeExpression(metaData.blog.title),
logo: schemaImageObject(metaData.blog.logo) || null
},
url: metaData.url,
image: schemaImageObject(metaData.coverImage),
name: data.tag.name,
mainEntityOfPage: {
'@type': 'WebPage',
'@id': metaData.blog.url || null
},
description: metaData.metaDescription ?
escapeExpression(metaData.metaDescription) :
null
};
return trimSchema(schema);
}
function getAuthorSchema(metaData, data) {
var schema = {
'@context': 'https://schema.org',
'@type': 'Person',
sameAs: trimSameAs(data, 'author'),
name: escapeExpression(data.author.name),
url: metaData.authorUrl,
image: schemaImageObject(metaData.coverImage),
mainEntityOfPage: {
'@type': 'WebPage',
'@id': metaData.blog.url || null
},
description: metaData.metaDescription ?
escapeExpression(metaData.metaDescription) :
null
};
return trimSchema(schema);
}
function getSchema(metaData, data) {
if (!config.isPrivacyDisabled('useStructuredData')) {
var context = data.context ? data.context : null;
if (_.includes(context, 'post') || _.includes(context, 'page') || _.includes(context, 'amp')) {
return getPostSchema(metaData, data);
} else if (_.includes(context, 'home')) {
return getHomeSchema(metaData);
} else if (_.includes(context, 'tag')) {
return getTagSchema(metaData, data);
} else if (_.includes(context, 'author')) {
return getAuthorSchema(metaData, data);
}
}
return null;
}
module.exports = getSchema;