diff --git a/index.es6 b/index.es6 index 97013c9..fe3853f 100644 --- a/index.es6 +++ b/index.es6 @@ -4,11 +4,13 @@ import pumpify from 'pumpify'; module.exports = function() { + let count = 0; + const write = (line, enc, cb) => { if(!line.trim()) return cb(); - const vttLine = line + let vttLine = line .replace(/(WEBVTT\s*(FILE)?.*)(\r\n)*/g, '') .replace(/(\d{2}:\d{2}:\d{2})\.(\d{3}\s+)\-\-\>(\s+\d{2}:\d{2}:\d{2})\.(\d{3}\s*)/g, '$1,$2-->$3,$4') .replace(/\<.+\>(.+)/g, '$1') @@ -16,6 +18,18 @@ module.exports = function() { if(!vttLine.trim()) return cb(); + if (/^Kind:|^Language:/m.test(vttLine)) { + return cb(); + } + + if (/^[0-9]+:/m.test(vttLine)) { + if (count === 0) { + vttLine = `${++count}\r\n${vttLine}`; + } else { + vttLine = `\r\n${++count}\r\n${vttLine}`; + } + } + cb(null, vttLine); }; diff --git a/index.js b/index.js index 0caaf4f..94829a0 100644 --- a/index.js +++ b/index.js @@ -16,6 +16,8 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de module.exports = function () { + var count = 0; + var write = function write(line, enc, cb) { if (!line.trim()) return cb(); @@ -24,6 +26,18 @@ module.exports = function () { if (!vttLine.trim()) return cb(); + if (/^Kind:|^Language:/m.test(vttLine)) { + return cb(); + } + + if (/^[0-9]+:/m.test(vttLine)) { + if (count === 0) { + vttLine = ++count + '\r\n' + vttLine; + } else { + vttLine = '\r\n' + ++count + '\r\n' + vttLine; + } + } + cb(null, vttLine); }; diff --git a/package.json b/package.json index 9f1c3a8..8ce0990 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "through2": "^0.6.3" }, "devDependencies": { + "babel-cli": "^6.26.0", "babel-core": "^6.9.1", "babel-preset-es2015": "^6.9.0", "concat-stream": "^1.4.10", diff --git a/test.js b/test.js index b7fe7cd..858f412 100644 --- a/test.js +++ b/test.js @@ -24,7 +24,7 @@ tape('one entry', function(t) { var convert = vtt2srt(); - convert.write('WEBVTT FILE\r\n\r\n1\r\n00:00:10.500 --> 00:00:13.000\r\nthis is a test\r\n\r\n'); + convert.write('WEBVTT FILE\r\n\r\n00:00:10.500 --> 00:00:13.000\r\nthis is a test\r\n\r\n'); convert.end(); @@ -42,13 +42,13 @@ tape('two entries', function(t) { var convert = vtt2srt(); - convert.write('WEBVTT FILE\r\n\r\n1\r\n00:00:10.500 --> 00:00:13.000\r\nthis is a test\r\n\r\n2\r\n00:00:14.500 --> 00:00:15.000\r\nthis is a test\r\n\r\n'); + convert.write('WEBVTT FILE\r\n\r\n00:00:10.500 --> 00:00:13.000\r\nthis is a test\r\n00:00:14.500 --> 00:00:15.000\r\nthis is a test\r\n\r\n'); convert.end(); convert.pipe(concat(function(data) { - t.same(data.toString(), '1\r\n00:00:10,500 --> 00:00:13,000\r\nthis is a test\r\n2\r\n00:00:14,500 --> 00:00:15,000\r\nthis is a test\r\n'); + t.same(data.toString(), '1\r\n00:00:10,500 --> 00:00:13,000\r\nthis is a test\r\n\r\n2\r\n00:00:14,500 --> 00:00:15,000\r\nthis is a test\r\n'); t.end();