| 12 | const DEFAULT_LINE_LIMIT = 4; |
| 13 | |
| 14 | export function createPreview( |
| 15 | message: string | undefined | null, |
| 16 | options: PreviewOptions = {}, |
| 17 | ): PreviewResult { |
| 18 | if (!message) { |
| 19 | return { text: '', truncated: false }; |
| 20 | } |
| 21 | |
| 22 | const charLimit = options.charLimit ?? DEFAULT_CHAR_LIMIT; |
| 23 | const lineLimit = options.lineLimit ?? DEFAULT_LINE_LIMIT; |
| 24 | |
| 25 | const lines = message.split('\n'); |
| 26 | |
| 27 | if (lines.length > lineLimit) { |
| 28 | const text = lines.slice(0, lineLimit).join('\n').trimEnd(); |
| 29 | return { text, truncated: true }; |
| 30 | } |
| 31 | |
| 32 | if (message.length > charLimit) { |
| 33 | const text = message.slice(0, charLimit).replace(/\s+$/g, ''); |
| 34 | return { text, truncated: true }; |
| 35 | } |
| 36 | |
| 37 | return { text: message, truncated: false }; |
| 38 | } |