V14 / fad4e1a110e5
MailBodyText.cs
Services/EmailService/MailBodyText.cs
Unchanged from the preceding versionCS
using System.Net;
using System.Text.RegularExpressions;
namespace Lilith.EmailService;
public static class MailBodyText
{
public static (string Text, bool Truncated) Extract(string? plain, string? html)
{
string text = plain ?? "";
if (string.IsNullOrWhiteSpace(text) && html is not null)
{
if (html.Length > 2_000_000) throw new IOException("Message HTML exceeds the supported reading limit.");
text = Regex.Replace(html, @"<(script|style|head)\b[^>]*>.*?</\1\s*>", "", RegexOptions.IgnoreCase | RegexOptions.Singleline, TimeSpan.FromMilliseconds(200));
text = Regex.Replace(text, @"<(br|/p|/div|/li|/tr)\b[^>]*>", "\n", RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(200));
text = WebUtility.HtmlDecode(Regex.Replace(text, "<[^>]*>", "", RegexOptions.Singleline, TimeSpan.FromMilliseconds(200)));
}
text = new string(text.Where(c => !char.IsControl(c) || c is '\n' or '\t').ToArray());
return (text.Length > 12000 ? text[..12000] : text, text.Length > 12000);
}
}