From c52aac012c358eb784ab5d872f0b9e08f1e7918f Mon Sep 17 00:00:00 2001 From: fdb-hiroshima <35889323+fdb-hiroshima@users.noreply.github.com> Date: Sat, 4 May 2019 17:33:50 +0200 Subject: [PATCH] make hashtags work in profile summary (#562) * make hashtags work in profile summary fix #541 * cargo fmt --- plume-common/src/utils.rs | 19 ++++++++++++------- plume-models/src/comments.rs | 2 +- plume-models/src/instance.rs | 4 ++-- plume-models/src/posts.rs | 4 ++-- plume-models/src/users.rs | 4 ++-- src/api/posts.rs | 2 +- src/routes/blogs.rs | 2 +- src/routes/comments.rs | 8 +++++--- src/routes/posts.rs | 16 ++++++++++------ 9 files changed, 36 insertions(+), 25 deletions(-) diff --git a/plume-common/src/utils.rs b/plume-common/src/utils.rs index 0abb8adc..a9a95861 100644 --- a/plume-common/src/utils.rs +++ b/plume-common/src/utils.rs @@ -154,10 +154,15 @@ fn process_image<'a, 'b>( /// Returns (HTML, mentions, hashtags) pub fn md_to_html<'a>( md: &str, - base_url: &str, + base_url: Option<&str>, inline: bool, media_processor: Option>, ) -> (String, HashSet, HashSet) { + let base_url = if let Some(base_url) = base_url { + format!("//{}/", base_url) + } else { + "/".to_owned() + }; let parser = Parser::new_ext(md, Options::all()); let (parser, mentions, hashtags): (Vec, Vec, Vec) = parser @@ -185,7 +190,7 @@ pub fn md_to_html<'a>( let mention = text_acc; let short_mention = mention.splitn(1, '@').nth(0).unwrap_or(""); let link = Tag::Link( - format!("//{}/@/{}/", base_url, &mention).into(), + format!("{}@/{}/", base_url, &mention).into(), short_mention.to_owned().into(), ); @@ -215,7 +220,7 @@ pub fn md_to_html<'a>( } let hashtag = text_acc; let link = Tag::Link( - format!("//{}/tag/{}", base_url, &hashtag.to_camel_case()) + format!("{}tag/{}", base_url, &hashtag.to_camel_case()) .into(), hashtag.to_owned().into(), ); @@ -337,7 +342,7 @@ mod tests { for (md, mentions) in tests { assert_eq!( - md_to_html(md, "", false, None).1, + md_to_html(md, None, false, None).1, mentions .into_iter() .map(|s| s.to_string()) @@ -362,7 +367,7 @@ mod tests { for (md, mentions) in tests { assert_eq!( - md_to_html(md, "", false, None).2, + md_to_html(md, None, false, None).2, mentions .into_iter() .map(|s| s.to_string()) @@ -374,11 +379,11 @@ mod tests { #[test] fn test_inline() { assert_eq!( - md_to_html("# Hello", "", false, None).0, + md_to_html("# Hello", None, false, None).0, String::from("

Hello

\n") ); assert_eq!( - md_to_html("# Hello", "", true, None).0, + md_to_html("# Hello", None, true, None).0, String::from("

Hello

\n") ); } diff --git a/plume-models/src/comments.rs b/plume-models/src/comments.rs index 63da5a74..93b9a441 100644 --- a/plume-models/src/comments.rs +++ b/plume-models/src/comments.rs @@ -107,7 +107,7 @@ impl Comment { let author = User::get(&c.conn, self.author_id)?; let (html, mentions, _hashtags) = utils::md_to_html( self.content.get().as_ref(), - &Instance::get_local(&c.conn)?.public_domain, + Some(&Instance::get_local(&c.conn)?.public_domain), true, Some(Media::get_media_processor(&c.conn, vec![&author])), ); diff --git a/plume-models/src/instance.rs b/plume-models/src/instance.rs index d7c527e5..07b9a30e 100644 --- a/plume-models/src/instance.rs +++ b/plume-models/src/instance.rs @@ -131,13 +131,13 @@ impl Instance { ) -> Result<()> { let (sd, _, _) = md_to_html( short_description.as_ref(), - &self.public_domain, + Some(&self.public_domain), true, Some(Media::get_media_processor(conn, vec![])), ); let (ld, _, _) = md_to_html( long_description.as_ref(), - &self.public_domain, + Some(&self.public_domain), false, Some(Media::get_media_processor(conn, vec![])), ); diff --git a/plume-models/src/posts.rs b/plume-models/src/posts.rs index 356f584d..99a2d53f 100644 --- a/plume-models/src/posts.rs +++ b/plume-models/src/posts.rs @@ -692,7 +692,7 @@ impl FromId for Post { } // save mentions and tags - let mut hashtags = md_to_html(&post.source, "", false, None) + let mut hashtags = md_to_html(&post.source, None, false, None) .2 .into_iter() .map(|s| s.to_camel_case()) @@ -829,7 +829,7 @@ impl AsObject for PostUpdate { post.license = license; } - let mut txt_hashtags = md_to_html(&post.source, "", false, None) + let mut txt_hashtags = md_to_html(&post.source, None, false, None) .2 .into_iter() .map(|s| s.to_camel_case()) diff --git a/plume-models/src/users.rs b/plume-models/src/users.rs index 75305de4..0460b7bc 100644 --- a/plume-models/src/users.rs +++ b/plume-models/src/users.rs @@ -215,7 +215,7 @@ impl User { users::email.eq(email), users::summary_html.eq(utils::md_to_html( &summary, - "", + None, false, Some(Media::get_media_processor(conn, vec![self])), ) @@ -931,7 +931,7 @@ impl NewUser { display_name, is_admin, summary: summary.to_owned(), - summary_html: SafeString::new(&utils::md_to_html(&summary, "", false, None).0), + summary_html: SafeString::new(&utils::md_to_html(&summary, None, false, None).0), email: Some(email), hashed_password: Some(password), instance_id: Instance::get_local(conn)?.id, diff --git a/src/api/posts.rs b/src/api/posts.rs index fd97c210..21516584 100644 --- a/src/api/posts.rs +++ b/src/api/posts.rs @@ -117,7 +117,7 @@ pub fn create( let domain = &Instance::get_local(conn)?.public_domain; let (content, mentions, hashtags) = md_to_html( &payload.source, - domain, + Some(domain), false, Some(Media::get_media_processor(conn, vec![&author])), ); diff --git a/src/routes/blogs.rs b/src/routes/blogs.rs index ae935dc7..24d9b832 100644 --- a/src/routes/blogs.rs +++ b/src/routes/blogs.rs @@ -293,7 +293,7 @@ pub fn update( blog.summary_html = SafeString::new( &utils::md_to_html( &form.summary, - "", + None, true, Some(Media::get_media_processor( &conn, diff --git a/src/routes/comments.rs b/src/routes/comments.rs index 991b8308..fcd9b99a 100644 --- a/src/routes/comments.rs +++ b/src/routes/comments.rs @@ -42,9 +42,11 @@ pub fn create( .map(|_| { let (html, mentions, _hashtags) = utils::md_to_html( form.content.as_ref(), - &Instance::get_local(&conn) - .expect("comments::create: local instance error") - .public_domain, + Some( + &Instance::get_local(&conn) + .expect("comments::create: local instance error") + .public_domain, + ), true, Some(Media::get_media_processor(&conn, vec![&user])), ); diff --git a/src/routes/posts.rs b/src/routes/posts.rs index 47f55553..b836b39e 100644 --- a/src/routes/posts.rs +++ b/src/routes/posts.rs @@ -262,9 +262,11 @@ pub fn update( } else { let (content, mentions, hashtags) = utils::md_to_html( form.content.to_string().as_ref(), - &Instance::get_local(&conn) - .expect("posts::update: Error getting local instance") - .public_domain, + Some( + &Instance::get_local(&conn) + .expect("posts::update: Error getting local instance") + .public_domain, + ), false, Some(Media::get_media_processor( &conn, @@ -432,9 +434,11 @@ pub fn create( let (content, mentions, hashtags) = utils::md_to_html( form.content.to_string().as_ref(), - &Instance::get_local(&conn) - .expect("post::create: local instance error") - .public_domain, + Some( + &Instance::get_local(&conn) + .expect("post::create: local instance error") + .public_domain, + ), false, Some(Media::get_media_processor( &conn,