-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathupgrades.js
165 lines (140 loc) · 5.29 KB
/
upgrades.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
var H5PUpgrades = window.H5PUpgrades || {};
/* global H5PUpgrades */
H5PUpgrades['H5P.NDLATimeline'] = (() => {
// Avoiding to use H5P.createUUID as H5P function may change
const createUUID = () => {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
.replace(/[xy]/g, (char) => {
const random = Math.random() * 16 | 0;
const newChar = (char === 'x') ? random : (random & 0x3 | 0x8);
return newChar.toString(16);
});
};
return {
0: {
/**
* Replace custom copyright widget and use metadata fields.
* Remove obsolete fields.
* @param {object} parameters Parameters.
* @param {function} finished Callback when done.
* @param {object} extras Extras such as metadata.
*/
1: (parameters, finished, extras) => {
if (parameters) {
const extractAuthors = (author) => {
if (typeof author !== 'string') {
return [];
}
return author.split(',').map((singleAuthor) => {
return {
name: singleAuthor.trim(),
role: 'Author'
};
});
};
// Extract metadata title from old copyright title
const extractTitle = (title) => {
return (typeof title === 'string') ? title : 'Untitled text';
};
// Extract metadata license info from old copyright license info
const extractLicense = (license) => {
let name = 'U';
let version;
if (typeof license === 'string') {
name = license;
if (name === 'PD') {
name = (version === 'CC PDM') ? 'CC PDM' : 'CC0 1.0';
}
else if (name.indexOf('CC BY') === 0) {
version = '4.0'; // Old copyright widget did NOT store version!
}
}
return [name, version];
};
// Extract metadata source from old copyright source
const extractSource = (source) => {
const valid = /^https?:\/\/.+$/; // veeery simple ...
if (!valid.test(source)) {
return null;
}
return (typeof source === 'string') ? source : null;
};
// Extract metadata year info from old copyright year info
const extractYear = (year) => {
const valid = /^[0-9]+\s*(-\s*[0-9]+)?$/;
let yearFrom, yearTo;
if (typeof year === 'string' && valid.test(year)) {
segments = year.split('-');
yearFrom = parseInt(segments[0]);
yearFrom = isNaN(yearFrom) ? null : yearFrom.toString();
yearTo = parseInt(segments?.[1]);
yearTo = isNaN(yearTo) ? null : yearTo.toString();
}
return [yearFrom, yearTo];
};
// Convert old copyright data to metadata
const convertCopyrightToMedadata = (oldCopyright = {}) => {
let yearFrom, yearTo;
[yearFrom, yearTo] = extractYear(oldCopyright.year);
let license, licenseVersion;
[license, licenseVersion] = extractLicense(oldCopyright.license);
return metadata = {
authors: extractAuthors(oldCopyright?.author),
title: extractTitle(oldCopyright?.title),
contentType: 'Text',
license: license,
...(licenseVersion && { licenseVersion: licenseVersion }),
...(oldCopyright?.source && {
source: extractSource(oldCopyright.source)
}),
...(typeof yearFrom === 'number' && { yearFrom: yearFrom }),
...(typeof yearTo === 'number' && { yearTo: yearTo }),
changes: []
};
};
if (parameters.titleSlide?.description) {
parameters.titleSlide.description = {
library: 'H5P.Text 1.1',
metadata: convertCopyrightToMedadata(
parameters.titleSlide.descriptionCopyright
),
params: { text: parameters.titleSlide.description },
subContentId: createUUID()
};
delete parameters.titleSlide.descriptionCopyright;
}
if (parameters.timelineItems) {
parameters.timelineItems = parameters.timelineItems.map((item) => {
item.description = {
library: 'H5P.Text 1.1',
metadata: convertCopyrightToMedadata(
item.descriptionCopyright
),
params: { text: item.description },
subContentId: createUUID()
};
delete item.descriptionCopyright;
return item;
});
}
}
if (parameters.behaviour) {
delete parameters.behaviour.unused;
}
finished(null, parameters, extras);
},
/**
* Remove obsolete fields from semantics.
* @param {object} parameters Parameters.
* @param {function} finished Callback when done.
* @param {object} extras Extras such as metadata.
*/
2: (parameters, finished, extras) => {
if (parameters) {
delete parameters.l10n;
}
finished(null, parameters, extras);
}
}
};
})();