Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix segments parser supports hourly syntax #105

Merged
merged 1 commit into from
Feb 25, 2024
Merged
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
62 changes: 40 additions & 22 deletions src/actions/comment/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,45 +47,63 @@ export const getComments = async (
parentComment,
};
};
const parseIntroComment = (
comment: string,
): Array<{ start: number; end?: number; title: string }> | null => {
const parseIntroComment = (comment: string) => {
const introPattern = /^intro:\s*([\s\S]*)$/i;
const match = comment.match(introPattern);
if (!match) return [];

const lines = match[1].split('\n').filter((line) => line.trim() !== '');
const segments = lines.map((line) => {
const segments = lines.map((line: string) => {
const parts = line.split('-').map((part) => part.trim());
const timePattern = /(\d{2}):(\d{2})/;
const timePattern = /(\d{1,2}):(\d{2}):(\d{2})|(\d{2}):(\d{2})/;
const startTimeMatch = parts[0].match(timePattern);

const start = startTimeMatch
? parseInt(startTimeMatch[1], 10) * 60 + parseInt(startTimeMatch[2], 10)
: 0;
const title = parts.length > 2 ? parts[2] : parts[1];
let start;
if (startTimeMatch) {
if (startTimeMatch[1]) {
start =
parseInt(startTimeMatch[1], 10) * 3600 +
parseInt(startTimeMatch[2], 10) * 60 +
parseInt(startTimeMatch[3], 10);
} else {
start =
parseInt(startTimeMatch[4], 10) * 60 +
parseInt(startTimeMatch[5], 10);
}
} else {
start = 0;
}

const title = parts.length > 2 ? parts[2] : parts[1];
return { start, title, end: 0 };
});

// Set 'end' to the 'start' of the next segment
for (let i = 0; i < segments.length - 1; i++) {
segments[i].end = segments[i + 1].start;
}

// Handle the last segment
const lastLineParts = lines[lines.length - 1]
.split('-')
.map((part) => part.trim());
if (lastLineParts.length < 3) {
return null;
if (lines.length > 0) {
const lastLineParts = lines[lines.length - 1]
.split('-')
.map((part) => part.trim());
if (lastLineParts.length >= 3) {
const timePattern = /(\d{1,2}):(\d{2}):(\d{2})|(\d{2}):(\d{2})/;
const endTimeMatch = lastLineParts[1].match(timePattern);
let end;
if (endTimeMatch) {
if (endTimeMatch[1]) {
end =
parseInt(endTimeMatch[1], 10) * 3600 +
parseInt(endTimeMatch[2], 10) * 60 +
parseInt(endTimeMatch[3], 10);
} else {
end =
parseInt(endTimeMatch[4], 10) * 60 + parseInt(endTimeMatch[5], 10);
}
segments[segments.length - 1].end = end;
}
}
}
const timePattern = /(\d{2}):(\d{2})/;
const endTimeMatch = lastLineParts[1].match(timePattern);
const end = endTimeMatch
? parseInt(endTimeMatch[1], 10) * 60 + parseInt(endTimeMatch[2], 10)
: 0;
segments[segments.length - 1].end = end;

return segments;
};
Expand Down
Loading