Plugin: keep <details> blocks intact when chunking replies

A reply is split into <=900-byte TeamSpeak messages; never split inside a
<details>…</details> block (each top-level block starts a fresh message and is
kept whole), otherwise a collapsible section spanning two messages would not
render.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
faumarayandClaude Opus 4.8 committed 2026-06-26 03:15:40 +03:00
1 parent ac88cd6eb1
commit 446416c723
1 file changed
+37 -15
+37 -15
View File
@@ -14,6 +14,8 @@ namespace Echoion::Plugin
// TeamSpeak rejects over-long text messages (long replies such as
// search results or the queue silently never arrive). Split into
// UTF-8-safe chunks on line boundaries so every line is delivered.
// A <details>…</details> block is kept whole in its own message —
// splitting it across messages would break the collapsible.
constexpr std::size_t kMaxMessageBytes = 900;
std::vector<std::string> chunkMessage(const std::string& message,
@@ -26,6 +28,7 @@ namespace Echoion::Plugin
}
std::string current;
int detailsDepth = 0;
auto flush = [&] {
if (!current.empty()) {
chunks.push_back(current);
@@ -41,26 +44,45 @@ namespace Echoion::Plugin
: message.substr(pos, nl - pos + 1);
pos = (nl == std::string::npos) ? message.size() : nl + 1;
// Hard-split a single over-long line on a UTF-8 boundary.
while (line.size() > limit) {
std::size_t cut = limit;
while (cut > 0 &&
(static_cast<unsigned char>(line[cut]) & 0xC0) ==
0x80) {
--cut;
const bool opens =
line.find("<details>") != std::string::npos;
const bool closes =
line.find("</details>") != std::string::npos;
if (detailsDepth == 0 && !current.empty()) {
// Start each top-level <details> block in a fresh message,
// and otherwise split when adding the line would overflow.
if (opens || current.size() + line.size() > limit) {
flush();
}
if (cut == 0) {
cut = limit; // pathological line; avoid an infinite loop
}
flush();
chunks.push_back(line.substr(0, cut));
line.erase(0, cut);
}
if (current.size() + line.size() > limit) {
flush();
// Hard-split an over-long line on a UTF-8 boundary, but never
// inside a <details> block (it stays whole even if oversized).
if (detailsDepth == 0 && !opens) {
while (line.size() > limit) {
std::size_t cut = limit;
while (cut > 0 && (static_cast<unsigned char>(
line[cut]) &
0xC0) == 0x80) {
--cut;
}
if (cut == 0) {
cut = limit; // pathological line; avoid infinite loop
}
flush();
chunks.push_back(line.substr(0, cut));
line.erase(0, cut);
}
}
current += line;
if (opens) {
++detailsDepth;
}
if (closes && detailsDepth > 0) {
--detailsDepth;
}
}
flush();
return chunks;