diff --git a/src/lib/client.ts b/src/lib/client.ts index 2db59f2..0780bcc 100644 --- a/src/lib/client.ts +++ b/src/lib/client.ts @@ -17,22 +17,39 @@ export const createUeuse = async (data: ApiMap["ueuse/create"]["body"], title: s const mem = Memory.memory; const maxLength = mem.max_length; const excessedMessage = "πŸ‘‰θΏ”δΏ‘γ«ηΆšγγŒγ‚γ‚ŠγΎγ™γ€‚"; - - let remainingText = data.text; + + // θ‘Œε˜δ½γ§εˆ†ε‰² + let lines = data.text.split(EOL); let firstUniqid = ""; - while (remainingText.length > 0) { + while (lines.length > 0) { let currentText = ""; - let bodyTextLength = 0; + + const limit = maxLength - (excessedMessage.length + EOL.length * 2); - if (remainingText.length <= maxLength) { - currentText = remainingText; - bodyTextLength = remainingText.length; - remainingText = ""; - } else { - bodyTextLength = maxLength - excessedMessage.length - 2; - currentText = remainingText.slice(0, bodyTextLength) + EOL.repeat(2) + excessedMessage; - remainingText = remainingText.slice(bodyTextLength); + while (lines.length > 0) { + const nextLine = lines[0]; + if (nextLine === undefined) break; + + if (nextLine.length > limit && currentText === "") { + const targetLine = lines.shift() || ""; + currentText = targetLine.slice(0, limit); + + lines.unshift(targetLine.slice(limit)); + break; + } + + const potentialText = currentText ? currentText + EOL + nextLine : nextLine; + if (potentialText.length <= limit) { + currentText = potentialText; + lines.shift(); + } else { + break; + } + } + + if (lines.length > 0) { + currentText += EOL.repeat(2) + excessedMessage; } let postedUniqid = ""; @@ -64,10 +81,7 @@ export const createUeuse = async (data: ApiMap["ueuse/create"]["body"], title: s break; } - if (firstUniqid === "") { - firstUniqid = postedUniqid; - } - + if (firstUniqid === "") firstUniqid = postedUniqid; console.log(`${title}γ‚’ζŠ•η¨Ώ:`, postedUniqid); } -} +} \ No newline at end of file