Skip to content

Commit

Permalink
improve long slack message chunking by chunking at newlines
Browse files Browse the repository at this point in the history
  • Loading branch information
hipstersmoothie committed Apr 4, 2024
1 parent aa12593 commit a1edc7b
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions plugins/slack/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,15 @@ const CHANGELOG_LINE = /^\s*•/;
type Messages = [Block[], ...Array<Block[] | FileUpload>];

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

while (buffer) {
splitLines.push(buffer.slice(0, charLimit));
buffer = buffer.slice(charLimit);
// get the \n closest to the char limit
const newlineIndex = buffer.lastIndexOf("\n", charLimit) || charLimit;
splitLines.push(buffer.slice(0, newlineIndex));
buffer = buffer.slice(newlineIndex);
}

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

if (fullSection.length > 3000) {
const splitLines = splitCharacterLimit(fullSection, 3000);
const splitLines = splitCharacterLimitAtNewline(fullSection, 3000);

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

currentMessage.push(createSpacerBlock());
} else if (line.length > 3000) {
const splitLines = splitCharacterLimit(line, 3000);
const splitLines = splitCharacterLimitAtNewline(line, 3000);

splitLines.forEach((splitLine) => {
currentMessage.push(createSectionBlock(splitLine));
Expand Down

0 comments on commit a1edc7b

Please sign in to comment.