Skip to content

Commit a1edc7b

Browse files
improve long slack message chunking by chunking at newlines
1 parent aa12593 commit a1edc7b

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

plugins/slack/src/index.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,15 @@ const CHANGELOG_LINE = /^\s*•/;
9292
type Messages = [Block[], ...Array<Block[] | FileUpload>];
9393

9494
/** Split a long spring into chunks by character limit */
95-
const splitCharacterLimit = (line: string, charLimit: number) => {
95+
const splitCharacterLimitAtNewline = (line: string, charLimit: number) => {
9696
const splitLines = [];
9797
let buffer = line;
9898

9999
while (buffer) {
100-
splitLines.push(buffer.slice(0, charLimit));
101-
buffer = buffer.slice(charLimit);
100+
// get the \n closest to the char limit
101+
const newlineIndex = buffer.lastIndexOf("\n", charLimit) || charLimit;
102+
splitLines.push(buffer.slice(0, newlineIndex));
103+
buffer = buffer.slice(newlineIndex);
102104
}
103105

104106
return splitLines;
@@ -172,7 +174,7 @@ export function convertToBlocks(
172174
const fullSection = lines.join("\n");
173175

174176
if (fullSection.length > 3000) {
175-
const splitLines = splitCharacterLimit(fullSection, 3000);
177+
const splitLines = splitCharacterLimitAtNewline(fullSection, 3000);
176178

177179
splitLines.forEach((splitLine) => {
178180
currentMessage.push(createSectionBlock(splitLine));
@@ -183,7 +185,7 @@ export function convertToBlocks(
183185

184186
currentMessage.push(createSpacerBlock());
185187
} else if (line.length > 3000) {
186-
const splitLines = splitCharacterLimit(line, 3000);
188+
const splitLines = splitCharacterLimitAtNewline(line, 3000);
187189

188190
splitLines.forEach((splitLine) => {
189191
currentMessage.push(createSectionBlock(splitLine));

0 commit comments

Comments
 (0)