-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
157 lines (133 loc) · 3.95 KB
/
index.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
const yaml = require('yaml-front-matter');
const request = require('sync-request');
const moment = require('moment');
const processDates = (data, badge) => {
if (typeof badge.issuedOn !== 'undefined') {
data.date = badge.issuedOn;
data.updated = badge.issuedOn;
}
return data;
};
const overridePostData = (data, badge) => {
data.title = badge.name;
data.photos.push(badge.image);
data.photo = badge.image;
data.description = badge.description;
return data;
};
const beforePostRender = (data) => {
let badgeUrl = yaml.loadFront(data.raw).badge;
if (badgeUrl !== undefined){
data.badge = getBadgeData('assertion', badgeUrl);
if (typeof data.photos === 'undefined') data.photos = [];
data = processDates(data, data.badge);
if (typeof data.badge.badge !== 'undefined') {
data = overridePostData(data, data.badge.badge);
} else {
data = overridePostData(data, data.badge);
}
data.verify = verificationUrl(data.badge);
}
return data;
};
const verificationUrl = (assertion) => {
switch (assertion.badge.issuer.url) {
case 'http://www.scrum.org':
case 'https://www.scrum.org':
return assertion.badge.issuer.url + '/certificates/' + assertion.uid;
case 'http://www.ibm.com':
case 'https://www.ibm.com':
return 'https://www.youracclaim.com/badges/' + assertion.id.split('/').last;
default:
return assertion.badge.verification.url;
}
};
const processAssertion = (result) => {
if (typeof result.badge !== 'undefined') {
result.badge = getBadgeData('badgeClass', result.badge);
}
if (typeof result.image !== 'undefined') {
result.image = getBadgeData('image', result.image);
}
if (typeof result.evidence !== 'undefined') {
result.evidence = getBadgeData('image', result.evidence);
}
if (typeof result.issuedOn !== 'undefined') {
if (moment(result.issuedOn, moment.ISO_8601, true).isValid()) {
result.issuedOn = moment.utc(result.issuedOn, moment.ISO_8601);
} else {
result.issuedOn = moment.utc(result.issuedOn, 'X');
}
}
return result;
};
const processBadgeClass = (result) => {
if (typeof result.image !== 'undefined') {
result.image = getBadgeData('image', result.image);
}
if (typeof result.criteria !== 'undefined') {
result.criteria = getBadgeData('criteria', result.criteria);
}
if (typeof result.issuer !== 'undefined') {
result.issuer = getBadgeData('profile', result.issuer);
}
return result;
};
const processProfile = (result) => {
if (typeof result.image !== 'undefined') {
result.image = getBadgeData('image', result.image);
}
if (typeof result.publicKey !== 'undefined') {
result.publicKey = getBadgeData('cryptographicKey', result.publicKey);
}
if (typeof result.revocationList !== 'undefined') {
result.revocationList = getBadgeData('revocationList', result.revocationList);
}
return result;
};
const processObject = (result) => {
if (typeof result === 'object') {
result = result.id;
}
return result;
};
const getBadgeData = (type, content) => {
let resultBody = {};
// In some cases the requested field can contain either a document or a remote object
// The try/catch will return the requested URL if we don't get valid JSON
try {
if (typeof content === 'object') {
resultBody = content;
} else {
let rawResult = request('GET', content);
resultBody = JSON.parse(rawResult.getBody());
}
}
catch (e) {
return content;
}
switch (type) {
case 'assertion': // Assertion
resultBody = processAssertion(resultBody);
break;
case 'badgeClass': // BadgeClass
resultBody = processBadgeClass(resultBody);
break;
case 'profile': // Profile
resultBody = processProfile(resultBody);
break;
case 'criteria': // Criteria
case 'image': // Image
resultBody = processObject(resultBody);
break;
// TODO: Add support for the following items
case 'evidence': // Evidence
case 'cryptographicKey': // CryptographicKey
case 'revocationList': // RevocationList
}
return resultBody;
};
hexo.extend.filter.register(
'before_post_render',
beforePostRender
);