Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dist/jsu.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/jsu.min.mjs

Large diffs are not rendered by default.

12 changes: 7 additions & 5 deletions src/jsu.js
Original file line number Diff line number Diff line change
Expand Up @@ -689,13 +689,14 @@ export default class JavaScriptUtilities {
// First split each block of content
const rows = subtitle.replace(/\r/g, '').split('\n\n');
const subContent = [];
let id = 1;
for (let i = 0; i < rows.length; i++) {
const row = rows[i];
// split block content
const content = row.split('\n');
const id = i;
if (!content[0].includes(' --> ')) {
// content[0] should contain an id and not the time

while (content.length && !content[0].includes(' --> ')) {
// content[0] could contain an id or lines returns and not the time
// if it's the case we remove it
content.shift();
}
Expand All @@ -712,9 +713,10 @@ export default class JavaScriptUtilities {
subContent.push({
'id': id,
'content': content.join(' ').trim(),
'timeStart': times[0],
'timeEnd': times[1]
'timeStart': times[0].trim(),
'timeEnd': times[1].trim()
});
id += 1;
}
}
}
Expand Down
38 changes: 38 additions & 0 deletions tests/test_jsu.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,4 +244,42 @@ describe('JSU', () => {
jsu.useLang('x');
assert(jsu.translate('lang') == 'en');
});
it('should parse subtitles', () => {
const excepted = (srt) => JSON.stringify([
{
'id': 1,
'content': 'La première phrase.',
'timeStart': `00:00:47${srt ? ',' : '.'}250`,
'timeEnd': `00:00:50${srt ? ',' : '.'}500`
},
{
'id': 2,
'content': 'Une phrase avec des caratères spéciaux: !§.',
'timeStart': `00:00:51${srt ? ',' : '.'}800`,
'timeEnd': `00:00:55${srt ? ',' : '.'}800`
}
]);
let result = JSON.stringify(jsu.parseSubtitle(`
1
00:00:47,250 --> 00:00:50,500
La première phrase.

2
00:00:51,800 --> 00:00:55,800
Une phrase avec des caratères spéciaux: !§.
`));
assert(result === excepted(true), `1) "${result}" not "${excepted(true)}"`);
result = JSON.stringify(jsu.parseSubtitle(`
WEBVTT - Test header

Comment line
00:00:47.250 --> 00:00:50.500
La première phrase.

2
00:00:51.800 --> 00:00:55.800
Une phrase avec des caratères spéciaux: !§.
`));
assert(result === excepted(), `2) "${result}" not ${excepted()}`);
});
});