* Strips quoted reply content from email body. * Email clients append the prior thread below a marker line like: * - "On [date] [person] wrote:" * - Lines starting with ">" * - Gmail's "---------- Forwarded message ----------" * * We keep only the new content above the first quote marker.
(text: string)
| 65 | * We keep only the new content above the first quote marker. |
| 66 | */ |
| 67 | function stripQuotedReply(text: string): string { |
| 68 | const lines = text.split('\n') |
| 69 | const cutIndex = lines.findIndex((line, i) => { |
| 70 | const trimmed = line.trim() |
| 71 | |
| 72 | if (/^On .+ wrote:\s*$/i.test(trimmed)) return true |
| 73 | |
| 74 | if (trimmed.startsWith('>') && i > 0) { |
| 75 | const prevTrimmed = lines[i - 1].trim() |
| 76 | if (prevTrimmed === '' || /^On .+ wrote:\s*$/i.test(prevTrimmed)) return true |
| 77 | } |
| 78 | |
| 79 | return false |
| 80 | }) |
| 81 | |
| 82 | if (cutIndex < 0) return text |
| 83 | if (cutIndex === 0) return '(reply with no new content above the quote)' |
| 84 | |
| 85 | return lines.slice(0, cutIndex).join('\n').trim() |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Detects whether an email is a forwarded message based on subject/body patterns. |
no test coverage detected