Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(web): add transcript paragraph formatting helper

Richie McIlroyb179ed3b42fd
diff --git a/apps/web/__tests__/unit/transcript-text.test.ts b/apps/web/__tests__/unit/transcript-text.test.ts
new file mode 100644
index 000000000..a86770a28
--- /dev/null
+++ b/apps/web/__tests__/unit/transcript-text.test.ts
@@ -0,0 +1,93 @@
+import { describe, expect, it } from "vitest";
+import {
+ formatTranscriptAsParagraphs,
+ type TranscriptTextEntry,
+} from "@/lib/transcript-text";
+
+function entry(
+ text: string,
+ startTime: number,
+ endTime: number,
+): TranscriptTextEntry {
+ return { text, startTime, endTime };
+}
+
+describe("formatTranscriptAsParagraphs", () => {
+ it("returns an empty string for no entries", () => {
+ expect(formatTranscriptAsParagraphs([])).toBe("");
+ });
+
+ it("joins caption fragments into one flowing paragraph", () => {
+ const result = formatTranscriptAsParagraphs([
+ entry("This is my test to make sure that,", 0.9, 2.4),
+ entry("the video is working correctly.", 3.0, 4.3),
+ entry("I'm going to stop the recording now.", 4.4, 5.8),
+ ]);
+ expect(result).toBe(
+ "This is my test to make sure that, the video is working correctly. I'm going to stop the recording now.",
+ );
+ });
+
+ it("breaks a paragraph at a sentence end followed by a long pause", () => {
+ const result = formatTranscriptAsParagraphs([
+ entry("First topic ends here.", 0, 2),
+ entry("Second topic starts fresh.", 4.5, 6),
+ ]);
+ expect(result).toBe("First topic ends here.\n\nSecond topic starts fresh.");
+ });
+
+ it("does not break on a pause mid-sentence", () => {
+ const result = formatTranscriptAsParagraphs([
+ entry("This sentence keeps", 0, 2),
+ entry("going after a pause.", 5, 6),
+ ]);
+ expect(result).toBe("This sentence keeps going after a pause.");
+ });
+
+ it("ignores pauses when the entry has no usable end time", () => {
+ const result = formatTranscriptAsParagraphs([
+ entry("A full sentence.", 2, 2),
+ entry("Another sentence.", 10, 11),
+ ]);
+ expect(result).toBe("A full sentence. Another sentence.");
+ });
+
+ it("breaks long paragraphs at the next sentence boundary", () => {
+ const sentence = `${"word ".repeat(30).trim()}.`;
+ const entries = Array.from({ length: 6 }, (_, index) =>
+ entry(sentence, index, index + 0.5),
+ );
+ const result = formatTranscriptAsParagraphs(entries);
+ const paragraphs = result.split("\n\n");
+ expect(paragraphs.length).toBeGreaterThan(1);
+ for (const paragraph of paragraphs) {
+ expect(paragraph.endsWith(".")).toBe(true);
+ }
+ });
+
+ it("hard-breaks transcripts that never end a sentence", () => {
+ const fragment = "no punctuation here just words".repeat(3);
+ const entries = Array.from({ length: 20 }, (_, index) =>
+ entry(fragment, index, index + 0.5),
+ );
+ const result = formatTranscriptAsParagraphs(entries);
+ expect(result.split("\n\n").length).toBeGreaterThan(1);
+ });
+
+ it("skips empty fragments", () => {
+ const result = formatTranscriptAsParagraphs([
+ entry("Hello there.", 0, 1),
+ entry(" ", 1, 2),
+ entry("General greeting.", 2, 3),
+ ]);
+ expect(result).toBe("Hello there. General greeting.");
+ });
+
+ it("respects sentence enders followed by closing quotes", () => {
+ const result = formatTranscriptAsParagraphs([
+ entry('He said "stop."', 0, 1),
+ entry("Then we did.", 4, 5),
+ ]);
+ expect(result).toBe('He said "stop."\n\nThen we did.');
+ });
+});
diff --git a/apps/web/lib/transcript-text.ts b/apps/web/lib/transcript-text.ts
new file mode 100644
index 000000000..3f982e35b
--- /dev/null
+++ b/apps/web/lib/transcript-text.ts
@@ -0,0 +1,59 @@
+export type TranscriptTextEntry = {
+ text: string;
+ startTime: number;
+ endTime: number;
+};
+
+const PARAGRAPH_GAP_SECONDS = 1.75;
+const SOFT_PARAGRAPH_CHAR_LIMIT = 500;
+const HARD_PARAGRAPH_CHAR_LIMIT = 1200;
+
+function endsSentence(text: string) {
+ return /[.!?]["')\]]?$/.test(text);
+}
+
+/**
+ * Joins caption-sized fragments into flowing paragraphs for use in documents.
+ * Paragraphs break at sentence boundaries when the speaker pauses, or when a
+ * paragraph grows past a comfortable reading length; a hard limit guards
+ * against transcripts with no sentence punctuation at all.
+ */
+export function formatTranscriptAsParagraphs(
+ entries: readonly TranscriptTextEntry[],
+): string {
+ const paragraphs: string[] = [];
+ let current = "";
+
+ for (let index = 0; index < entries.length; index++) {
+ const entry = entries[index];
+ if (!entry) continue;
+ const text = entry.text.trim();
+ if (!text) continue;
+
+ current = current ? `${current} ${text}` : text;
+
+ const next = entries[index + 1];
+ if (!next) break;
+
+ if (current.length >= HARD_PARAGRAPH_CHAR_LIMIT) {
+ paragraphs.push(current);
+ current = "";
+ continue;
+ }
+
+ if (!endsSentence(text)) continue;
+
+ const gapSeconds =
+ entry.endTime > entry.startTime ? next.startTime - entry.endTime : 0;
+ if (
+ gapSeconds >= PARAGRAPH_GAP_SECONDS ||
+ current.length >= SOFT_PARAGRAPH_CHAR_LIMIT
+ ) {
+ paragraphs.push(current);
+ current = "";
+ }
+ }
+
+ if (current) paragraphs.push(current);
+ return paragraphs.join("\n\n");
+}