spam list + socket events
This commit is contained in:
+43
-4
@@ -81,6 +81,30 @@ export const formatDate = (dateString: string) => {
|
||||
}).format(date);
|
||||
};
|
||||
|
||||
export const detectTextDirection = (text: string): "ltr" | "rtl" => {
|
||||
// حذف تگهای HTML برای تحلیل فقط متن
|
||||
const plainText = text.replace(/<[^>]*>/g, " ").trim();
|
||||
|
||||
// اگر متن خالی است، از RTL استفاده کن
|
||||
if (!plainText) return "rtl";
|
||||
|
||||
// الگوی کاراکترهای انگلیسی و اعداد
|
||||
const englishPattern = /[a-zA-Z0-9\s.,!?;:'"()\-@#$%^&*+=<>{}[\]\\|`~_]/g;
|
||||
const englishMatches = plainText.match(englishPattern) || [];
|
||||
|
||||
// الگوی کاراکترهای فارسی و عربی (برای آینده ممکن است نیاز باشد)
|
||||
// const persianArabicPattern = /[\u0600-\u06FF\u0750-\u077F\u08A0-\u08FF\uFB50-\uFDFF\uFE70-\uFEFF]/g;
|
||||
|
||||
// محاسبه درصد کاراکترهای انگلیسی
|
||||
const totalChars = plainText.replace(/\s/g, "").length;
|
||||
const englishChars = englishMatches.join("").replace(/\s/g, "").length;
|
||||
const englishPercentage =
|
||||
totalChars > 0 ? (englishChars / totalChars) * 100 : 0;
|
||||
|
||||
// اگر بیش از 70% متن انگلیسی است، LTR استفاده کن
|
||||
return englishPercentage > 70 ? "ltr" : "rtl";
|
||||
};
|
||||
|
||||
export const sanitizeEmailHTML = (html: string): string => {
|
||||
try {
|
||||
// Remove style tags and their content
|
||||
@@ -95,15 +119,30 @@ export const sanitizeEmailHTML = (html: string): string => {
|
||||
// Remove potentially harmful attributes
|
||||
sanitized = sanitized.replace(/\s*on\w+\s*=\s*["'][^"']*["']/gi, "");
|
||||
|
||||
// فقط font-family و font-size رو از inline styles حذف کن، بقیه رو حفظ کن
|
||||
// تشخیص جهت متن اصلی
|
||||
const detectedDirection = detectTextDirection(sanitized);
|
||||
|
||||
// فقط font-family، font-size و font-weight رو از inline styles حذف کن
|
||||
// text-align و direction رو فقط اگر با جهت تشخیص داده شده مطابقت نداشته باشد حذف کن
|
||||
sanitized = sanitized.replace(
|
||||
/style\s*=\s*["']([^"']*?)["']/gi,
|
||||
(_, styles) => {
|
||||
// فقط font-family و font-size رو حذف کن
|
||||
const cleanedStyles = styles
|
||||
let cleanedStyles = styles
|
||||
.replace(/font-family\s*:\s*[^;]*;?/gi, "")
|
||||
.replace(/font-size\s*:\s*[^;]*;?/gi, "")
|
||||
.replace(/font-weight\s*:\s*[^;]*;?/gi, "")
|
||||
.replace(/font-weight\s*:\s*[^;]*;?/gi, "");
|
||||
|
||||
// اگر جهت تشخیص داده شده LTR است، direction و text-align را حفظ کن
|
||||
if (detectedDirection === "ltr") {
|
||||
// direction و text-align را حفظ کن
|
||||
} else {
|
||||
// اگر RTL است، این استایلها را حذف کن تا استایل سایت اعمال شود
|
||||
cleanedStyles = cleanedStyles
|
||||
.replace(/text-align\s*:\s*[^;]*;?/gi, "")
|
||||
.replace(/direction\s*:\s*[^;]*;?/gi, "");
|
||||
}
|
||||
|
||||
cleanedStyles = cleanedStyles
|
||||
.replace(/;\s*;/g, ";") // حذف سمیکالنهای اضافی
|
||||
.replace(/^\s*;\s*|\s*;\s*$/g, ""); // حذف سمیکالنهای ابتدا و انتها
|
||||
|
||||
|
||||
Reference in New Issue
Block a user