fix(discord): reserve closing-fence space on fence-closing lines (#95… · openclaw/openclaw@d84a8b1
ly-wang19
·
2026-06-23
·
via Recent Commits to openclaw:main
| Original file line number | Diff line number | Diff line change |
|---|
@@ -78,6 +78,17 @@ describe("chunkDiscordText", () => {
|
78 | 78 | } |
79 | 79 | }); |
80 | 80 | |
| 81 | +it("keeps chunks within maxChars when a closing fence line carries trailing text", () => { |
| 82 | +// A line that both closes the fence and carries a long tail must still reserve closing-fence |
| 83 | +// space; otherwise a mid-line flush appended "```" and overflowed maxChars (e.g. 2004 > 2000). |
| 84 | +for (let pad = 1990; pad <= 2000; pad++) { |
| 85 | +const text = "hi\n```lang\n```" + "z".repeat(pad); |
| 86 | +for (const chunk of chunkDiscordText(text, { maxChars: 2000, maxLines: 100 })) { |
| 87 | +expect(chunk.length).toBeLessThanOrEqual(2000); |
| 88 | +} |
| 89 | +} |
| 90 | +}); |
| 91 | + |
81 | 92 | it("preserves whitespace when splitting long lines", () => { |
82 | 93 | const text = Array.from({ length: 40 }, () => "word").join(" "); |
83 | 94 | const chunks = chunkDiscordText(text, { maxChars: 20, maxLines: 50 }); |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -207,8 +207,12 @@ export function chunkDiscordText(text: string, opts: ChunkDiscordTextOpts = {}):
|
207 | 207 | } |
208 | 208 | } |
209 | 209 | |
210 | | -const reserveChars = nextOpenFence ? closeFenceLine(nextOpenFence).length + 1 : 0; |
211 | | -const reserveLines = nextOpenFence ? 1 : 0; |
| 210 | +// A flush can fire mid-line, before `openFence` advances to `nextOpenFence` below, so it closes |
| 211 | +// against the still-open `openFence`. A fence-closing line that also carries trailing text would |
| 212 | +// otherwise reserve 0 yet still get a closing fence appended on flush, overflowing maxChars. |
| 213 | +const fenceToReserve = nextOpenFence ?? openFence; |
| 214 | +const reserveChars = fenceToReserve ? closeFenceLine(fenceToReserve).length + 1 : 0; |
| 215 | +const reserveLines = fenceToReserve ? 1 : 0; |
212 | 216 | const effectiveMaxChars = maxChars - reserveChars; |
213 | 217 | const effectiveMaxLines = maxLines - reserveLines; |
214 | 218 | const charLimit = effectiveMaxChars > 0 ? effectiveMaxChars : maxChars; |
|
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。