Files
ERitors-Scribe-Desktop/src-tauri/src/helpers.rs

45 lines
1.8 KiB
Rust

use regex::Regex;
/// 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 = html_node.to_string();
let p_regex: Regex = Regex::new(r"(?i)</?p[^>]*>").unwrap();
let br_regex: Regex = Regex::new(r"(?i)<br\s*/?>").unwrap();
let span_heading_regex: Regex = Regex::new(r"(?i)</?(span|h[1-6])[^>]*>").unwrap();
text = p_regex.replace_all(&text, "\n").to_string();
text = br_regex.replace_all(&text, "\n").to_string();
text = span_heading_regex.replace_all(&text, "").to_string();
text = text.replace("&apos;", "'");
text = text.replace("&quot;", "\"");
text = text.replace("&amp;", "&");
text = text.replace("&lt;", "<");
text = text.replace("&gt;", ">");
text = text.replace("&#39;", "'");
let double_newline_regex: Regex = Regex::new(r"\r?\n\s*\n").unwrap();
text = double_newline_regex.replace_all(&text, "\n").to_string();
let spaces_regex: Regex = Regex::new(r"[ \t]+").unwrap();
text = spaces_regex.replace_all(&text, " ").to_string();
text.trim().to_string()
}