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:
1 parent
ac88cd6eb1
commit
446416c723
1 file changed
+37
-15
+37
-15
@@ -14,6 +14,8 @@ namespace Echoion::Plugin
|
|||||||
// TeamSpeak rejects over-long text messages (long replies such as
|
// TeamSpeak rejects over-long text messages (long replies such as
|
||||||
// search results or the queue silently never arrive). Split into
|
// search results or the queue silently never arrive). Split into
|
||||||
// UTF-8-safe chunks on line boundaries so every line is delivered.
|
// 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;
|
constexpr std::size_t kMaxMessageBytes = 900;
|
||||||
|
|
||||||
std::vector<std::string> chunkMessage(const std::string& message,
|
std::vector<std::string> chunkMessage(const std::string& message,
|
||||||
@@ -26,6 +28,7 @@ namespace Echoion::Plugin
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::string current;
|
std::string current;
|
||||||
|
int detailsDepth = 0;
|
||||||
auto flush = [&] {
|
auto flush = [&] {
|
||||||
if (!current.empty()) {
|
if (!current.empty()) {
|
||||||
chunks.push_back(current);
|
chunks.push_back(current);
|
||||||
@@ -41,26 +44,45 @@ namespace Echoion::Plugin
|
|||||||
: message.substr(pos, nl - pos + 1);
|
: message.substr(pos, nl - pos + 1);
|
||||||
pos = (nl == std::string::npos) ? message.size() : nl + 1;
|
pos = (nl == std::string::npos) ? message.size() : nl + 1;
|
||||||
|
|
||||||
// Hard-split a single over-long line on a UTF-8 boundary.
|
const bool opens =
|
||||||
while (line.size() > limit) {
|
line.find("<details>") != std::string::npos;
|
||||||
std::size_t cut = limit;
|
const bool closes =
|
||||||
while (cut > 0 &&
|
line.find("</details>") != std::string::npos;
|
||||||
(static_cast<unsigned char>(line[cut]) & 0xC0) ==
|
|
||||||
0x80) {
|
if (detailsDepth == 0 && !current.empty()) {
|
||||||
--cut;
|
// 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) {
|
// Hard-split an over-long line on a UTF-8 boundary, but never
|
||||||
flush();
|
// 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;
|
current += line;
|
||||||
|
if (opens) {
|
||||||
|
++detailsDepth;
|
||||||
|
}
|
||||||
|
if (closes && detailsDepth > 0) {
|
||||||
|
--detailsDepth;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
flush();
|
flush();
|
||||||
return chunks;
|
return chunks;
|
||||||
|
|||||||
Reference in new issue
Block a user