diff --git a/Cargo.lock b/Cargo.lock index 0399e54..1631ec4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3353,13 +3353,14 @@ dependencies = [ [[package]] name = "tetratto-shared" -version = "12.0.2" +version = "12.0.3" dependencies = [ "ammonia", "chrono", "hex_fmt", "pulldown-cmark", "rand 0.9.1", + "regex", "serde", "sha2", "snowflaked", diff --git a/crates/shared/Cargo.toml b/crates/shared/Cargo.toml index 4945db1..13250e4 100644 --- a/crates/shared/Cargo.toml +++ b/crates/shared/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "tetratto-shared" description = "Shared stuff for Tetratto" -version = "12.0.2" +version = "12.0.3" edition = "2024" authors.workspace = true repository.workspace = true @@ -13,6 +13,7 @@ chrono = "0.4.41" hex_fmt = "0.3.0" pulldown-cmark = "0.13.0" rand = "0.9.1" +regex = "1.11.1" serde = { version = "1.0.219", features = ["derive"] } sha2 = "0.10.9" snowflaked = "1.0.3" diff --git a/crates/shared/src/markdown.rs b/crates/shared/src/markdown.rs index e1c44c2..6e4cb04 100644 --- a/crates/shared/src/markdown.rs +++ b/crates/shared/src/markdown.rs @@ -4,7 +4,7 @@ use std::collections::HashSet; /// Render markdown input into HTML pub fn render_markdown(input: &str, proxy_images: bool) -> String { - let input = &parse_alignment(input); + let input = &autolinks(&parse_alignment(input)); let mut options = Options::empty(); options.insert(Options::ENABLE_STRIKETHROUGH); @@ -102,7 +102,7 @@ fn parse_alignment_line(line: &str, output: &mut String, buffer: &mut String, is if !is_alignment_waiting { // we need to go ahead and push/clear the buffer so we don't capture the stuff that came before this // this only needs to be done on the first of these for a single alignment block - output.push_str(&format!("{buffer}\n")); + output.push_str(&buffer); buffer.clear(); } } @@ -171,3 +171,19 @@ pub fn parse_alignment(input: &str) -> String { output.push_str(&buffer); output } + +/// Adapted from . +pub fn autolinks(input: &str) -> String { + if input.len() == 0 { + return String::new(); + } + + let pattern = regex::Regex::new( + r"(?ix)\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))", + ) + .unwrap(); + + pattern + .replace_all(input, "$0") + .to_string() +}