Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandercerutti committed Feb 10, 2025
2 parents eddbc14 + aab9cae commit 1aa77f6
Show file tree
Hide file tree
Showing 9 changed files with 1,629 additions and 1,784 deletions.
9 changes: 7 additions & 2 deletions lerna.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
{
"$schema": "node_modules/lerna/schemas/lerna-schema.json",
"useWorkspaces": true,
"workspaces": ["packages/*"],
"version": "independent",
"npmClient": "pnpm"
"npmClient": "pnpm",
"includeMergedTags": true,
"command": {
"version": {
"allowBranch": "master"
}
}
}
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
},
"homepage": "https://github.com/alexandercerutti/sub37#readme",
"devDependencies": {
"@jest/globals": "^29.4.3",
"@types/jest": "^29.4.0",
"@jest/globals": "^29.7.0",
"@types/jest": "^29.5.14",
"jest": "^29.4.3",
"jest-environment-jsdom": "^29.4.3",
"lerna": "^6.6.1",
"prettier": "^2.8.4",
"jest-environment-jsdom": "^29.7.0",
"lerna": "^8.1.9",
"prettier": "^3.5.0",
"typescript": "^4.9.5"
}
}
2 changes: 1 addition & 1 deletion packages/sample/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@sub37/sample-app",
"private": true,
"version": "1.0.2",
"version": "1.0.3",
"type": "module",
"scripts": {
"dev": "pnpm vite",
Expand Down
8 changes: 8 additions & 0 deletions packages/webvtt-adapter/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# @sub37/webvtt-adapter

## **1.1.0** (10 Feb 2025)

**Changes**:

- Added missing exclusion of cues with the same ids, when available, with error emission;

---

## **1.0.4** (08 Feb 2024)

**Changes**:
Expand Down
2 changes: 1 addition & 1 deletion packages/webvtt-adapter/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sub37/webvtt-adapter",
"version": "1.0.4",
"version": "1.1.0",
"description": "A subtitles adapter for WebVTT subtitles",
"main": "lib/index.js",
"type": "module",
Expand Down
46 changes: 46 additions & 0 deletions packages/webvtt-adapter/specs/Adapter.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,52 @@ This cue should never appear, right?
expect(result.data[1].endTime).toEqual(10000);
});

it("should exclude cues with the same ids except timestamps", () => {
const SAME_IDS_CONTENT = `
WEBVTT
id1
00:00:06.000 --> 00:00:07.000
...
id1
00:00:06.050 --> 00:00:07.050
...
123
00:00:08.000 --> 00:00:10.000
...Right?
123
00:00:08.050 --> 00:00:10.050
...Right?
456
00:00:08.000 --> 00:00:10.000
<00:00:08.250> Test t1
<00:00:08.500> Test t2
`;

const result = adapter.parse(SAME_IDS_CONTENT);
expect(result.data.length).toEqual(4);

expect(result.data[0].id).toEqual("id1");
expect(result.data[0].startTime).toEqual(6000);
expect(result.data[0].endTime).toEqual(7000);

expect(result.data[1].id).toEqual("123");
expect(result.data[1].startTime).toEqual(8000);
expect(result.data[1].endTime).toEqual(10000);

expect(result.data[2].id).toEqual("456");
expect(result.data[2].startTime).toEqual(8250);
expect(result.data[2].endTime).toEqual(10000);

expect(result.data[3].id).toEqual("456");
expect(result.data[3].startTime).toEqual(8500);
expect(result.data[3].endTime).toEqual(10000);
});

it("should return an array containing two cues", () => {
const CLASSIC_CONTENT = `
WEBVTT
Expand Down
44 changes: 40 additions & 4 deletions packages/webvtt-adapter/src/Adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export default class WebVTTAdapter extends BaseAdapter {
]);
}

const cueIdsList: Set<string> = new Set();
const cues: CueNode[] = [];
const content = String(rawContent).replace(/\r?\n/g, "\n");
const block = {
Expand Down Expand Up @@ -127,11 +128,44 @@ export default class WebVTTAdapter extends BaseAdapter {

for (const parsedCue of parsedContent) {
if (parsedCue.startTime >= parsedCue.endTime) {
failures.push({
error: new Error(`A cue cannot start (${parsedCue.startTime}) after its end time (${parsedCue.endTime})`),
failedChunk: content.substring(block.start, block.cursor),
isCritical: false,
});

continue;
}

if (parsedCue.id) {
/**
* "A WebVTT cue identifier must be unique amongst
* all the WebVTT cue identifiers of all WebVTT
* cues of a WebVTT file."
*
* @see https://www.w3.org/TR/webvtt1/#webvtt-cue-identifier
*/

if (!parsedCue.isTimestamp && cueIdsList.has(parsedCue.id)) {
failures.push({
error: new Error(`A WebVTT cue identifier must be unique amongst all the cue identifiers of a WebVTT file. Double id found: '${parsedCue.id}'`),
failedChunk: content.substring(block.start, block.cursor),
isCritical: false,
});

continue;
}

/**
* ... however, when we generate a custom identifier
* for the cue, we re-use the same for the timestamps
* because they must appear on the same line.
*/
cueIdsList.add(parsedCue.id);
}

const cue = CueNode.from(latestRootCue, {
id: parsedCue.id,
id: parsedCue.id || `cue-${block.start}-${block.cursor}`,
startTime: parsedCue.startTime,
endTime: parsedCue.endTime,
content: parsedCue.text,
Expand Down Expand Up @@ -310,13 +344,15 @@ function evaluateBlock(
return new InvalidFormatError("UNKNOWN_BLOCK_ENTITY", contentSection);
}

const { attributes, cueid, endtime, starttime, text } = cueMatch.groups as {
[K in keyof Parser.CueRawData]: Parser.CueRawData[K];
type CueMatchGroups = {
[K in keyof Omit<Parser.CueRawData, "startCharPosition" | "endCharPosition">]: Parser.CueRawData[K];
};

const { attributes, cueid, endtime, starttime, text } = cueMatch.groups as CueMatchGroups;

const cueParsingResult = Parser.parseCue({
attributes,
cueid: cueid || `cue-${start}-${end}`,
cueid: cueid,
endtime,
starttime,
text: text.replace(TABS_REGEX, ""),
Expand Down
5 changes: 5 additions & 0 deletions packages/webvtt-adapter/src/Parser/parseCue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface CueParsedData {
tags: Entities.Tag[];
text: string;
renderingModifiers: RenderingModifiers;
isTimestamp?: boolean;
}

export function parseCue(data: CueRawData): CueParsedData[] {
Expand Down Expand Up @@ -96,6 +97,7 @@ export function parseCue(data: CueRawData): CueParsedData[] {
*/

addCueEntities(currentCue, Tags.createTagEntitiesFromUnpaired(openTagsQueue, currentCue));
currentCue.isTimestamp = true;
hsCues.push(currentCue);
}

Expand All @@ -104,6 +106,7 @@ export function parseCue(data: CueRawData): CueParsedData[] {
currentCue.endTime,
currentCue.id,
currentCue.renderingModifiers,
currentCue.isTimestamp,
);

break;
Expand Down Expand Up @@ -143,6 +146,7 @@ function createCue(
endTime: number,
id?: string,
renderingModifiers?: RenderingModifiers,
isTimestamp: boolean = false,
): CueParsedData {
return {
startTime,
Expand All @@ -151,5 +155,6 @@ function createCue(
tags: [],
id,
renderingModifiers,
isTimestamp,
};
}
Loading

0 comments on commit 1aa77f6

Please sign in to comment.