ultisuite-client/lib/mail-plain-text-html.ts
R3D347HR4Y 8a02c10ba3 Add environment configuration and update email view components
- Created a .cursorignore file to manage local environment files.
- Updated .env.example to reflect changes in the public app URL.
- Modified the gmail workspace configuration to include the drive-suite path.
- Enhanced email view components to support attachment handling and fallback for plain text bodies.
- Improved user experience by updating attachment display logic and integrating inline attachment support.
2026-06-04 00:12:43 +02:00

44 lines
1.4 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/** Render plain-text-only messages with clickable links and readable line breaks. */
const URL_RE = /https?:\/\/[^\s<>"')\]]+/gi
function escapeHtml(text: string): string {
return text
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
}
function linkifyEscapedText(text: string): string {
let out = ""
let last = 0
for (const match of text.matchAll(URL_RE)) {
const index = match.index ?? 0
out += text.slice(last, index)
const url = match[0]
out += `<a href="${url}" target="_blank" rel="noopener noreferrer">${url}</a>`
last = index + url.length
}
out += text.slice(last)
return out
}
/** Insert breaks before common transactional-mail markers when the body is one long line. */
function softenPlainTextWall(text: string): string {
if (text.includes("\n")) return text
return text
.replace(/,\s*(Cher(?:e)?\s)/gi, ",\n\n$1")
.replace(/,\s*(Le \d{2}\/\d{2}\/\d{4})/g, ",\n\n$1")
.replace(/\.\s+(Nous restons)/g, ".\n\n$1")
.replace(/\.\s+(L[']équipe)/g, ".\n\n$1")
.replace(/\.\s+(Pour obtenir)/g, ".\n\n$1")
.replace(/\.\s+(Vous pouvez)/g, ".\n\n$1")
}
export function plainTextToDisplayHtml(text: string): string {
const normalized = softenPlainTextWall(text.trim())
const escaped = escapeHtml(normalized)
const linked = linkifyEscapedText(escaped)
return `<pre style="white-space:pre-wrap;font-family:inherit;margin:0;line-height:1.5;">${linked}</pre>`
}