Skip to content

Commit 6cae6a6

Browse files
author
b
committed
fix: avoid mid-word clipping in comic lines
1 parent a9278ab commit 6cae6a6

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

functions/lib/comic-generator.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ function sanitizeLine(input: unknown, maxLength = 65): string | undefined {
220220
if (typeof input !== 'string') return undefined;
221221
const clean = input.replace(/\s+/g, ' ').trim();
222222
if (!clean) return undefined;
223-
return clean.slice(0, maxLength);
223+
return truncateAtWord(clean, maxLength);
224224
}
225225

226226
function sanitizeThought(input: unknown): string | undefined {
@@ -231,7 +231,7 @@ function sanitizeThought(input: unknown): string | undefined {
231231
.filter(Boolean)
232232
.slice(0, 3)
233233
.map((line) => (line.startsWith('>') ? line : `> ${line}`))
234-
.map((line) => line.slice(0, 34));
234+
.map((line) => truncateAtWord(line, 34));
235235

236236
if (lines.length === 0) return undefined;
237237
return lines.join('\n');
@@ -317,3 +317,11 @@ function parseJsonFromText(raw: string): any {
317317
}
318318
}
319319
}
320+
321+
function truncateAtWord(text: string, maxLength: number): string {
322+
if (text.length <= maxLength) return text;
323+
const clipped = text.slice(0, maxLength);
324+
const boundary = clipped.lastIndexOf(' ');
325+
if (boundary < 10) return clipped.trim();
326+
return clipped.slice(0, boundary).trim();
327+
}

0 commit comments

Comments
 (0)