From 426760cd401ff13bbe506d9eeb927a67054edabd Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Sat, 25 May 2024 15:55:53 -0400 Subject: [PATCH] performant socials --- Cargo.lock | 1 + crates/lib/markdown/Cargo.toml | 1 + .../src/markdown_with_socials_to_html.rs | 201 +++++++++++++----- 3 files changed, 146 insertions(+), 57 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c9d0a62ae4..650068fc8b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3747,6 +3747,7 @@ version = "0.0.1" dependencies = [ "errors", "my-workspace-hack", + "once_cell", "pulldown-cmark", "regex", ] diff --git a/crates/lib/markdown/Cargo.toml b/crates/lib/markdown/Cargo.toml index 26413799b0..77c6305efb 100644 --- a/crates/lib/markdown/Cargo.toml +++ b/crates/lib/markdown/Cargo.toml @@ -24,6 +24,7 @@ errors = { path = "../errors" } my-workspace-hack = { version = "0.1", path = "../../my-workspace-hack" } # External +once_cell = "1.18.0" pulldown-cmark = "0.9.3" # markdown regex = "1.10.4" diff --git a/crates/lib/markdown/src/markdown_with_socials_to_html.rs b/crates/lib/markdown/src/markdown_with_socials_to_html.rs index d67ddd98e0..4ba16630b1 100644 --- a/crates/lib/markdown/src/markdown_with_socials_to_html.rs +++ b/crates/lib/markdown/src/markdown_with_socials_to_html.rs @@ -1,3 +1,5 @@ +use std::borrow::Cow; +use once_cell::sync::Lazy; use pulldown_cmark::{html, Options, Parser}; use regex::{Captures, Regex}; @@ -15,7 +17,6 @@ pub fn markdown_with_socials_to_html(markdown_input: &str) -> String { .replace("\"", """) .replace("'", "'"); // NB: ' is not in the HTML spec - // TODO: Use copy on write let markdown_input = replace_instagram_user(&markdown_input); let markdown_input = replace_reddit_subreddit(&markdown_input); let markdown_input = replace_reddit_user(&markdown_input); @@ -38,52 +39,64 @@ pub fn markdown_with_socials_to_html(markdown_input: &str) -> String { // TODO(bt,2024-05-25): Not sure if the username parsing is right for each service. -fn replace_instagram_user(input: &str) -> String { - let re = Regex::new(r"(insta(gram)?(.com)?/([\w\-_]+))").unwrap(); // TODO: once_cell - let result = re.replace(input, |caps: &Captures| { +static INSTAGRAM_USER : Lazy = Lazy::new(|| { + Regex::new(r"(insta(gram)?(.com)?/([\w\-_]+))").expect("regex should compile") +}); + +fn replace_instagram_user(input: &str) -> Cow { + INSTAGRAM_USER.replace(input, |caps: &Captures| { format!("[{}](https://instagram.com/{})", &caps[1], &caps[4]) - }); - result.to_string() + }) } -fn replace_reddit_subreddit(input: &str) -> String { - let re = Regex::new(r"(r/([\w\-_]+))").unwrap(); // TODO: once_cell - let result = re.replace(input, |caps: &Captures| { +static REDDIT_SUBREDDIT: Lazy = Lazy::new(|| { + Regex::new(r"(r/([\w\-_]+))").expect("regex should compile") +}); + +fn replace_reddit_subreddit(input: &str) -> Cow { + REDDIT_SUBREDDIT.replace(input, |caps: &Captures| { format!("[{}](https://www.reddit.com/r/{})", &caps[1], &caps[2]) - }); - result.to_string() + }) } -fn replace_reddit_user(input: &str) -> String { - let re = Regex::new(r"(u/([\w\-_]+))").unwrap(); // TODO: once_cell - let result = re.replace(input, |caps: &Captures| { +static REDDIT_USER: Lazy = Lazy::new(|| { + Regex::new(r"(u/([\w\-_]+))").expect("regex should compile") +}); + +fn replace_reddit_user(input: &str) -> Cow { + REDDIT_USER.replace(input, |caps: &Captures| { format!("[{}](https://www.reddit.com/user/{})", &caps[1], &caps[2]) - }); - result.to_string() + }) } -fn replace_tiktok_user(input: &str) -> String { - let re = Regex::new(r"(tiktok(.com)?/([@\w\-_]+))").unwrap(); // TODO: once_cell - let result = re.replace(input, |caps: &Captures| { - format!("[{}](https://www.tiktok.com/{})", &caps[1], &caps[3]) - }); - result.to_string() +static TIKTOK_USER: Lazy = Lazy::new(|| { + Regex::new(r"(tiktok(.com)?/@?([\w\-_]+))").expect("regex should compile") +}); + +fn replace_tiktok_user(input: &str) -> Cow { + TIKTOK_USER.replace(input, |caps: &Captures| { + format!("[{}](https://www.tiktok.com/@{})", &caps[1], &caps[3]) + }) } -fn replace_x_user(input: &str) -> String { - let re = Regex::new(r"(x(.com)?/([\w\-_]+))").unwrap(); // TODO: once_cell - let result = re.replace(input, |caps: &Captures| { +static X_USER: Lazy = Lazy::new(|| { + Regex::new(r"(x(.com)?/([\w\-_]+))").expect("regex should compile") +}); + +fn replace_x_user(input: &str) -> Cow { + X_USER.replace(input, |caps: &Captures| { format!("[{}](https://x.com/{})", &caps[1], &caps[3]) - }); - result.to_string() + }) } -fn replace_youtube_channel(input: &str) -> String { - let re = Regex::new(r"((yt|youtube(.com)?)/([@\w\-_]+))").unwrap(); // TODO: once_cell - let result = re.replace(input, |caps: &Captures| { - format!("[{}](https://www.youtube.com/{})", &caps[1], &caps[4]) - }); - result.to_string() +static YOUTUBE_CHANNEL: Lazy = Lazy::new(|| { + Regex::new(r"((yt|youtube(.com)?)/@?([\w\-_]+))").expect("regex should compile") +}); + +fn replace_youtube_channel(input: &str) -> Cow { + YOUTUBE_CHANNEL.replace(input, |caps: &Captures| { + format!("[{}](https://www.youtube.com/@{})", &caps[1], &caps[4]) + }) } #[cfg(test)] @@ -175,22 +188,48 @@ mod tests { mod tiktok { use super::*; - #[test] - pub fn tiktok() { - assert_eq!(&markdown_with_socials_to_html("tiktok/storyteller_ai"), - "

tiktok/storyteller_ai

\n"); + mod with_at { + use super::*; + + #[test] + pub fn tiktok() { + assert_eq!(&markdown_with_socials_to_html("tiktok/@storyteller_ai"), + "

tiktok/@storyteller_ai

\n"); + } + + #[test] + pub fn tiktok_dot_com() { + assert_eq!(&markdown_with_socials_to_html("tiktok.com/@storyteller_ai"), + "

tiktok.com/@storyteller_ai

\n"); + } + + #[test] + pub fn tiktok_before_after() { + assert_eq!(&markdown_with_socials_to_html("creator tiktok/@storyteller_ai posted it"), + "

creator tiktok/@storyteller_ai posted it

\n"); + } } - #[test] - pub fn tiktok_dot_com() { - assert_eq!(&markdown_with_socials_to_html("tiktok.com/storyteller_ai"), - "

tiktok.com/storyteller_ai

\n"); - } + mod without_at { + use super::*; - #[test] - pub fn tiktok_before_after() { - assert_eq!(&markdown_with_socials_to_html("creator tiktok/storyteller_ai posted it"), - "

creator tiktok/storyteller_ai posted it

\n"); + #[test] + pub fn tiktok() { + assert_eq!(&markdown_with_socials_to_html("tiktok/storyteller_ai"), + "

tiktok/storyteller_ai

\n"); + } + + #[test] + pub fn tiktok_dot_com() { + assert_eq!(&markdown_with_socials_to_html("tiktok.com/storyteller_ai"), + "

tiktok.com/storyteller_ai

\n"); + } + + #[test] + pub fn tiktok_before_after() { + assert_eq!(&markdown_with_socials_to_html("creator tiktok/storyteller_ai posted it"), + "

creator tiktok/storyteller_ai posted it

\n"); + } } } @@ -219,22 +258,70 @@ mod tests { mod youtube { use super::*; - #[test] - pub fn youtube() { - assert_eq!(&markdown_with_socials_to_html("youtube/@storyteller_ai"), - "

youtube/@storyteller_ai

\n"); + pub mod with_at { + use super::*; + + #[test] + pub fn yt() { + assert_eq!(&markdown_with_socials_to_html("yt/@storyteller_ai"), + "

yt/@storyteller_ai

\n"); + } + + #[test] + pub fn youtube() { + assert_eq!(&markdown_with_socials_to_html("youtube/@storyteller_ai"), + "

youtube/@storyteller_ai

\n"); + } + + #[test] + pub fn youtube_dot_com() { + assert_eq!(&markdown_with_socials_to_html("youtube.com/@storyteller_ai"), + "

youtube.com/@storyteller_ai

\n"); + } + + #[test] + pub fn youtube_before_after() { + assert_eq!(&markdown_with_socials_to_html("creator youtube/@storyteller_ai posted it"), + "

creator youtube/@storyteller_ai posted it

\n"); + } } - #[test] - pub fn youtube_dot_com() { - assert_eq!(&markdown_with_socials_to_html("youtube.com/@storyteller_ai"), - "

youtube.com/@storyteller_ai

\n"); + pub mod without_at { + use super::*; + + #[test] + pub fn yt() { + assert_eq!(&markdown_with_socials_to_html("yt/storyteller_ai"), + "

yt/storyteller_ai

\n"); + } + + #[test] + pub fn youtube() { + assert_eq!(&markdown_with_socials_to_html("youtube/storyteller_ai"), + "

youtube/storyteller_ai

\n"); + } + + #[test] + pub fn youtube_dot_com() { + assert_eq!(&markdown_with_socials_to_html("youtube.com/storyteller_ai"), + "

youtube.com/storyteller_ai

\n"); + } + + #[test] + pub fn youtube_before_after() { + assert_eq!(&markdown_with_socials_to_html("creator youtube/storyteller_ai posted it"), + "

creator youtube/storyteller_ai posted it

\n"); + } } + } + + mod complex { + use super::*; #[test] - pub fn youtube_before_after() { - assert_eq!(&markdown_with_socials_to_html("creator youtube/@storyteller_ai posted it"), - "

creator youtube/@storyteller_ai posted it

\n"); + pub fn multiple_socials() { + assert_eq!(&markdown_with_socials_to_html("the r/foo user x/bar."), + "

the r/foo user x/bar.

\n"); } } }