@sub37/webvtt-adapter 1.0.4 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/Adapter.js +31 -2
- package/lib/Parser/parseCue.d.ts +1 -0
- package/lib/Parser/parseCue.js +4 -2
- package/package.json +2 -2
package/lib/Adapter.js
CHANGED
|
@@ -31,6 +31,7 @@ export default class WebVTTAdapter extends BaseAdapter {
|
|
|
31
31
|
},
|
|
32
32
|
]);
|
|
33
33
|
}
|
|
34
|
+
const cueIdsList = new Set();
|
|
34
35
|
const cues = [];
|
|
35
36
|
const content = String(rawContent).replace(/\r?\n/g, "\n");
|
|
36
37
|
const block = {
|
|
@@ -98,10 +99,38 @@ export default class WebVTTAdapter extends BaseAdapter {
|
|
|
98
99
|
let latestRootCue = undefined;
|
|
99
100
|
for (const parsedCue of parsedContent) {
|
|
100
101
|
if (parsedCue.startTime >= parsedCue.endTime) {
|
|
102
|
+
failures.push({
|
|
103
|
+
error: new Error(`A cue cannot start (${parsedCue.startTime}) after its end time (${parsedCue.endTime})`),
|
|
104
|
+
failedChunk: content.substring(block.start, block.cursor),
|
|
105
|
+
isCritical: false,
|
|
106
|
+
});
|
|
101
107
|
continue;
|
|
102
108
|
}
|
|
109
|
+
if (parsedCue.id) {
|
|
110
|
+
/**
|
|
111
|
+
* "A WebVTT cue identifier must be unique amongst
|
|
112
|
+
* all the WebVTT cue identifiers of all WebVTT
|
|
113
|
+
* cues of a WebVTT file."
|
|
114
|
+
*
|
|
115
|
+
* @see https://www.w3.org/TR/webvtt1/#webvtt-cue-identifier
|
|
116
|
+
*/
|
|
117
|
+
if (!parsedCue.isTimestamp && cueIdsList.has(parsedCue.id)) {
|
|
118
|
+
failures.push({
|
|
119
|
+
error: new Error(`A WebVTT cue identifier must be unique amongst all the cue identifiers of a WebVTT file. Double id found: '${parsedCue.id}'`),
|
|
120
|
+
failedChunk: content.substring(block.start, block.cursor),
|
|
121
|
+
isCritical: false,
|
|
122
|
+
});
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* ... however, when we generate a custom identifier
|
|
127
|
+
* for the cue, we re-use the same for the timestamps
|
|
128
|
+
* because they must appear on the same line.
|
|
129
|
+
*/
|
|
130
|
+
cueIdsList.add(parsedCue.id);
|
|
131
|
+
}
|
|
103
132
|
const cue = CueNode.from(latestRootCue, {
|
|
104
|
-
id: parsedCue.id
|
|
133
|
+
id: parsedCue.id || `cue-${block.start}-${block.cursor}`,
|
|
105
134
|
startTime: parsedCue.startTime,
|
|
106
135
|
endTime: parsedCue.endTime,
|
|
107
136
|
content: parsedCue.text,
|
|
@@ -229,7 +258,7 @@ function evaluateBlock(content, start, end) {
|
|
|
229
258
|
const { attributes, cueid, endtime, starttime, text } = cueMatch.groups;
|
|
230
259
|
const cueParsingResult = Parser.parseCue({
|
|
231
260
|
attributes,
|
|
232
|
-
cueid: cueid
|
|
261
|
+
cueid: cueid,
|
|
233
262
|
endtime,
|
|
234
263
|
starttime,
|
|
235
264
|
text: text.replace(TABS_REGEX, ""),
|
package/lib/Parser/parseCue.d.ts
CHANGED
package/lib/Parser/parseCue.js
CHANGED
|
@@ -55,9 +55,10 @@ export function parseCue(data) {
|
|
|
55
55
|
* have some tags still open
|
|
56
56
|
*/
|
|
57
57
|
addCueEntities(currentCue, Tags.createTagEntitiesFromUnpaired(openTagsQueue, currentCue));
|
|
58
|
+
currentCue.isTimestamp = true;
|
|
58
59
|
hsCues.push(currentCue);
|
|
59
60
|
}
|
|
60
|
-
currentCue = createCue(Timestamps.parseMs(token.content), currentCue.endTime, currentCue.id, currentCue.renderingModifiers);
|
|
61
|
+
currentCue = createCue(Timestamps.parseMs(token.content), currentCue.endTime, currentCue.id, currentCue.renderingModifiers, currentCue.isTimestamp);
|
|
61
62
|
break;
|
|
62
63
|
}
|
|
63
64
|
default:
|
|
@@ -82,7 +83,7 @@ function addCueEntities(cue, entities) {
|
|
|
82
83
|
cue.tags.push(entity);
|
|
83
84
|
}
|
|
84
85
|
}
|
|
85
|
-
function createCue(startTime, endTime, id, renderingModifiers) {
|
|
86
|
+
function createCue(startTime, endTime, id, renderingModifiers, isTimestamp = false) {
|
|
86
87
|
return {
|
|
87
88
|
startTime,
|
|
88
89
|
endTime,
|
|
@@ -90,6 +91,7 @@ function createCue(startTime, endTime, id, renderingModifiers) {
|
|
|
90
91
|
tags: [],
|
|
91
92
|
id,
|
|
92
93
|
renderingModifiers,
|
|
94
|
+
isTimestamp,
|
|
93
95
|
};
|
|
94
96
|
}
|
|
95
97
|
//# sourceMappingURL=parseCue.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sub37/webvtt-adapter",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "A subtitles adapter for WebVTT subtitles",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -25,5 +25,5 @@
|
|
|
25
25
|
"files": [
|
|
26
26
|
"lib/**/*.+(js|d.ts)!(*.map)"
|
|
27
27
|
],
|
|
28
|
-
"gitHead": "
|
|
28
|
+
"gitHead": "f25f16822f3940d1bc26bc0e7e9b65d968913803"
|
|
29
29
|
}
|