// tslint:disable-next-line:max-line-length const timestampRegex = /^(?:(\d{2,})(:))?([0-5][0-9])(:)([0-5][0-9])([,.])(\d{3})( --> )(?:(\d{2,})(:))?([0-5][0-9])(:)([0-5][0-9])([,.])(\d{3})/; function fixSrt(input) { const trim = (str) => str.trim() .replace(/^[^\S\n]+/gm, '') .replace(/\u0000/g, '\uFFFD') .replace(/\r\n/g, '\n') .replace(/\r/g, '\n') .replace(/\n{3,}/g, '\n\n'); const origin = trim(input) .split('\n'); let id = 0; let idCache = ''; let state = 'root'; const processLine = line => { switch (state) { case 'note': /* falls through */ case 'title': if (line === '') { state = 'root'; } return []; case 'root': if (line.match(/^WEBVTT/)) { state = 'title'; return []; } else if (line.match(/^NOTE/)) { state = 'note'; return []; } else if (line === '') return [line]; else if (!line.match(/-->/)) { state = 'timeline'; idCache = line; return []; } /* falls through */ case 'timeline': const match = timestampRegex.exec(line); if (match) { idCache = ''; id++; state = 'text'; return [ id + '', // tslint:disable-next-line:max-line-length `${match[1] || '00'}:${match[3]}:${match[5]},${match[7]} --> ${match[9] || '00'}:${match[11]}:${match[13]},${match[15]}` ]; } else { // 坏的字幕块 state = 'root'; // 原样输出 const lastLine = idCache; idCache = ''; return lastLine ? [lastLine, line] : [line]; // 忽略这个字幕块 // return []; } case 'text': if (line === '') { state = 'root'; } return [line]; } }; const result = []; origin.forEach(line => result.push(...processLine(line))); // 没写完的字幕块 // 原样输出 if (idCache) result.push(idCache); return trim(result.join('\n')) + '\n'; }