38 lines
1.0 KiB
JavaScript
38 lines
1.0 KiB
JavaScript
// Function written by gpt-oss:20b
|
|
export function split_discord_message(text) {
|
|
const MAX = 1900; // maximum payload size
|
|
const MIN = 100; // avoid tiny chunks
|
|
const chunks = [];
|
|
let start = 0;
|
|
|
|
while (start < text.length) {
|
|
let end = Math.min(start + MAX, text.length);
|
|
|
|
// Try to break on a newline first, then on a space
|
|
if (end < text.length) {
|
|
const nl = text.lastIndexOf('\n', end);
|
|
const sp = text.lastIndexOf(' ', end);
|
|
const splitPos = Math.max(nl, sp);
|
|
if (splitPos > start) end = splitPos + 1; // keep the delimiter
|
|
}
|
|
|
|
// If the chunk is too short, pull in the next delimiter
|
|
if (end - start < MIN && end < text.length) {
|
|
const nextNL = text.indexOf('\n', end);
|
|
const nextSP = text.indexOf(' ', end);
|
|
const nextPos = Math.min(
|
|
nextNL === -1 ? Infinity : nextNL,
|
|
nextSP === -1 ? Infinity : nextSP
|
|
);
|
|
if (nextPos !== Infinity && nextPos < text.length) {
|
|
end = nextPos + 1;
|
|
}
|
|
}
|
|
|
|
chunks.push(text.slice(start, end));
|
|
start = end;
|
|
}
|
|
|
|
return chunks;
|
|
}
|