const SLACK_MAX_LENGTH = 4000; export function formatForSlack(text: string): string[] { let converted = text .replace(/\*\*(.+?)\*\*/g, "*$1*") .replace(/\[([^\]]+)\]\(([^)]+)\)/g, "<$2|$1>") .replace(/^#{1,6}\s+(.+)$/gm, "*$1*"); return chunkText(converted, SLACK_MAX_LENGTH); } function chunkText(text: string, maxLen: number): string[] { if (text.length <= maxLen) return [text]; const chunks: string[] = []; let remaining = text; while (remaining.length > maxLen) { let splitAt = maxLen; const newlineIdx = remaining.lastIndexOf("\n", maxLen); if (newlineIdx > maxLen * 0.5) { splitAt = newlineIdx + 1; } else { const spaceIdx = remaining.lastIndexOf(" ", maxLen); if (spaceIdx > maxLen * 0.5) { splitAt = spaceIdx + 1; } } chunks.push(remaining.slice(0, splitAt)); remaining = remaining.slice(splitAt); } if (remaining) chunks.push(remaining); return chunks; }