fix: preserve discord chunk whitespace

This commit is contained in:
Peter Steinberger
2026-01-17 07:11:15 +00:00
parent 3a6ee5ee00
commit 86a46874da
3 changed files with 24 additions and 2 deletions

View File

@@ -71,6 +71,7 @@
- Security: bump `tar` to 7.5.3.
- Models: align ZAI thinking toggles.
- iMessage/Signal: include sender metadata for non-queued group messages. (#1059)
- Discord: preserve whitespace when chunking long lines so message splits keep spacing intact.
## 2026.1.15

View File

@@ -63,6 +63,27 @@ describe("chunkDiscordText", () => {
}
});
it("preserves whitespace when splitting long lines", () => {
const text = Array.from({ length: 40 }, () => "word").join(" ");
const chunks = chunkDiscordText(text, { maxChars: 20, maxLines: 50 });
expect(chunks.length).toBeGreaterThan(1);
expect(chunks.join("")).toBe(text);
});
it("preserves mixed whitespace across chunk boundaries", () => {
const text = "alpha beta\tgamma delta epsilon zeta";
const chunks = chunkDiscordText(text, { maxChars: 12, maxLines: 50 });
expect(chunks.length).toBeGreaterThan(1);
expect(chunks.join("")).toBe(text);
});
it("keeps leading whitespace when splitting long lines", () => {
const text = " indented line with words that force splits";
const chunks = chunkDiscordText(text, { maxChars: 14, maxLines: 50 });
expect(chunks.length).toBeGreaterThan(1);
expect(chunks.join("")).toBe(text);
});
it("keeps reasoning italics balanced across chunks", () => {
const body = Array.from({ length: 25 }, (_, i) => `${i + 1}. line`).join("\n");
const text = `Reasoning:\n_${body}_`;

View File

@@ -76,8 +76,8 @@ function splitLongLine(
}
if (breakIdx <= 0) breakIdx = limit;
out.push(remaining.slice(0, breakIdx));
const brokeOnSeparator = breakIdx < remaining.length && /\s/.test(remaining[breakIdx]);
remaining = remaining.slice(breakIdx + (brokeOnSeparator ? 1 : 0));
// Keep the separator for the next segment so words don't get glued together.
remaining = remaining.slice(breakIdx);
}
if (remaining.length) out.push(remaining);
return out;