use std::sync::LazyLock; use regex::Regex; static P_REGEX: LazyLock = LazyLock::new(|| Regex::new(r"(?i)]*>").unwrap()); static BR_REGEX: LazyLock = LazyLock::new(|| Regex::new(r"(?i)").unwrap()); static SPAN_HEADING_REGEX: LazyLock = LazyLock::new(|| Regex::new(r"(?i)]*>").unwrap()); static DOUBLE_NEWLINE_REGEX: LazyLock = LazyLock::new(|| Regex::new(r"\r?\n\s*\n").unwrap()); static SPACES_REGEX: LazyLock = LazyLock::new(|| Regex::new(r"[ \t]+").unwrap()); /// Returns the current UNIX timestamp in seconds. /// Equivalent to TS `System.timeStampInSeconds()`. pub fn timestamp_in_seconds() -> i64 { std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH) .map(|duration| duration.as_secs() as i64) .unwrap_or(0) } /// Creates a new UUID v4 string, or reuses an existing ID if provided. /// Equivalent to TS `System.createUniqueId()`. pub fn create_unique_id(existing_id: Option<&str>) -> String { match existing_id { Some(id) => id.to_string(), None => uuid::Uuid::new_v4().to_string(), } } /// Converts HTML content to plain text by stripping tags and decoding entities. /// Equivalent to TS `System.htmlToText()`. pub fn html_to_text(html_node: &str) -> String { let mut text: String = P_REGEX.replace_all(html_node, "\n").to_string(); text = BR_REGEX.replace_all(&text, "\n").to_string(); text = SPAN_HEADING_REGEX.replace_all(&text, "").to_string(); text = text.replace("'", "'"); text = text.replace(""", "\""); text = text.replace("&", "&"); text = text.replace("<", "<"); text = text.replace(">", ">"); text = text.replace("'", "'"); text = DOUBLE_NEWLINE_REGEX.replace_all(&text, "\n").to_string(); text = SPACES_REGEX.replace_all(&text, " ").to_string(); text.trim().to_string() }