make hashtags work in profile summary (#562)

* make hashtags work in profile summary

fix #541

* cargo fmt
This commit is contained in:
fdb-hiroshima 2019-05-04 17:33:50 +02:00 committed by GitHub
parent c9070930d2
commit c52aac012c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 36 additions and 25 deletions

View File

@ -154,10 +154,15 @@ fn process_image<'a, 'b>(
/// Returns (HTML, mentions, hashtags) /// Returns (HTML, mentions, hashtags)
pub fn md_to_html<'a>( pub fn md_to_html<'a>(
md: &str, md: &str,
base_url: &str, base_url: Option<&str>,
inline: bool, inline: bool,
media_processor: Option<MediaProcessor<'a>>, media_processor: Option<MediaProcessor<'a>>,
) -> (String, HashSet<String>, HashSet<String>) { ) -> (String, HashSet<String>, HashSet<String>) {
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 = Parser::new_ext(md, Options::all());
let (parser, mentions, hashtags): (Vec<Event>, Vec<String>, Vec<String>) = parser let (parser, mentions, hashtags): (Vec<Event>, Vec<String>, Vec<String>) = parser
@ -185,7 +190,7 @@ pub fn md_to_html<'a>(
let mention = text_acc; let mention = text_acc;
let short_mention = mention.splitn(1, '@').nth(0).unwrap_or(""); let short_mention = mention.splitn(1, '@').nth(0).unwrap_or("");
let link = Tag::Link( let link = Tag::Link(
format!("//{}/@/{}/", base_url, &mention).into(), format!("{}@/{}/", base_url, &mention).into(),
short_mention.to_owned().into(), short_mention.to_owned().into(),
); );
@ -215,7 +220,7 @@ pub fn md_to_html<'a>(
} }
let hashtag = text_acc; let hashtag = text_acc;
let link = Tag::Link( let link = Tag::Link(
format!("//{}/tag/{}", base_url, &hashtag.to_camel_case()) format!("{}tag/{}", base_url, &hashtag.to_camel_case())
.into(), .into(),
hashtag.to_owned().into(), hashtag.to_owned().into(),
); );
@ -337,7 +342,7 @@ mod tests {
for (md, mentions) in tests { for (md, mentions) in tests {
assert_eq!( assert_eq!(
md_to_html(md, "", false, None).1, md_to_html(md, None, false, None).1,
mentions mentions
.into_iter() .into_iter()
.map(|s| s.to_string()) .map(|s| s.to_string())
@ -362,7 +367,7 @@ mod tests {
for (md, mentions) in tests { for (md, mentions) in tests {
assert_eq!( assert_eq!(
md_to_html(md, "", false, None).2, md_to_html(md, None, false, None).2,
mentions mentions
.into_iter() .into_iter()
.map(|s| s.to_string()) .map(|s| s.to_string())
@ -374,11 +379,11 @@ mod tests {
#[test] #[test]
fn test_inline() { fn test_inline() {
assert_eq!( assert_eq!(
md_to_html("# Hello", "", false, None).0, md_to_html("# Hello", None, false, None).0,
String::from("<h1>Hello</h1>\n") String::from("<h1>Hello</h1>\n")
); );
assert_eq!( assert_eq!(
md_to_html("# Hello", "", true, None).0, md_to_html("# Hello", None, true, None).0,
String::from("<p>Hello</p>\n") String::from("<p>Hello</p>\n")
); );
} }

View File

@ -107,7 +107,7 @@ impl Comment {
let author = User::get(&c.conn, self.author_id)?; let author = User::get(&c.conn, self.author_id)?;
let (html, mentions, _hashtags) = utils::md_to_html( let (html, mentions, _hashtags) = utils::md_to_html(
self.content.get().as_ref(), self.content.get().as_ref(),
&Instance::get_local(&c.conn)?.public_domain, Some(&Instance::get_local(&c.conn)?.public_domain),
true, true,
Some(Media::get_media_processor(&c.conn, vec![&author])), Some(Media::get_media_processor(&c.conn, vec![&author])),
); );

View File

@ -131,13 +131,13 @@ impl Instance {
) -> Result<()> { ) -> Result<()> {
let (sd, _, _) = md_to_html( let (sd, _, _) = md_to_html(
short_description.as_ref(), short_description.as_ref(),
&self.public_domain, Some(&self.public_domain),
true, true,
Some(Media::get_media_processor(conn, vec![])), Some(Media::get_media_processor(conn, vec![])),
); );
let (ld, _, _) = md_to_html( let (ld, _, _) = md_to_html(
long_description.as_ref(), long_description.as_ref(),
&self.public_domain, Some(&self.public_domain),
false, false,
Some(Media::get_media_processor(conn, vec![])), Some(Media::get_media_processor(conn, vec![])),
); );

View File

@ -692,7 +692,7 @@ impl FromId<PlumeRocket> for Post {
} }
// save mentions and tags // 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 .2
.into_iter() .into_iter()
.map(|s| s.to_camel_case()) .map(|s| s.to_camel_case())
@ -829,7 +829,7 @@ impl AsObject<User, Update, &PlumeRocket> for PostUpdate {
post.license = license; 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 .2
.into_iter() .into_iter()
.map(|s| s.to_camel_case()) .map(|s| s.to_camel_case())

View File

@ -215,7 +215,7 @@ impl User {
users::email.eq(email), users::email.eq(email),
users::summary_html.eq(utils::md_to_html( users::summary_html.eq(utils::md_to_html(
&summary, &summary,
"", None,
false, false,
Some(Media::get_media_processor(conn, vec![self])), Some(Media::get_media_processor(conn, vec![self])),
) )
@ -931,7 +931,7 @@ impl NewUser {
display_name, display_name,
is_admin, is_admin,
summary: summary.to_owned(), 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), email: Some(email),
hashed_password: Some(password), hashed_password: Some(password),
instance_id: Instance::get_local(conn)?.id, instance_id: Instance::get_local(conn)?.id,

View File

@ -117,7 +117,7 @@ pub fn create(
let domain = &Instance::get_local(conn)?.public_domain; let domain = &Instance::get_local(conn)?.public_domain;
let (content, mentions, hashtags) = md_to_html( let (content, mentions, hashtags) = md_to_html(
&payload.source, &payload.source,
domain, Some(domain),
false, false,
Some(Media::get_media_processor(conn, vec![&author])), Some(Media::get_media_processor(conn, vec![&author])),
); );

View File

@ -293,7 +293,7 @@ pub fn update(
blog.summary_html = SafeString::new( blog.summary_html = SafeString::new(
&utils::md_to_html( &utils::md_to_html(
&form.summary, &form.summary,
"", None,
true, true,
Some(Media::get_media_processor( Some(Media::get_media_processor(
&conn, &conn,

View File

@ -42,9 +42,11 @@ pub fn create(
.map(|_| { .map(|_| {
let (html, mentions, _hashtags) = utils::md_to_html( let (html, mentions, _hashtags) = utils::md_to_html(
form.content.as_ref(), form.content.as_ref(),
&Instance::get_local(&conn) Some(
.expect("comments::create: local instance error") &Instance::get_local(&conn)
.public_domain, .expect("comments::create: local instance error")
.public_domain,
),
true, true,
Some(Media::get_media_processor(&conn, vec![&user])), Some(Media::get_media_processor(&conn, vec![&user])),
); );

View File

@ -262,9 +262,11 @@ pub fn update(
} else { } else {
let (content, mentions, hashtags) = utils::md_to_html( let (content, mentions, hashtags) = utils::md_to_html(
form.content.to_string().as_ref(), form.content.to_string().as_ref(),
&Instance::get_local(&conn) Some(
.expect("posts::update: Error getting local instance") &Instance::get_local(&conn)
.public_domain, .expect("posts::update: Error getting local instance")
.public_domain,
),
false, false,
Some(Media::get_media_processor( Some(Media::get_media_processor(
&conn, &conn,
@ -432,9 +434,11 @@ pub fn create(
let (content, mentions, hashtags) = utils::md_to_html( let (content, mentions, hashtags) = utils::md_to_html(
form.content.to_string().as_ref(), form.content.to_string().as_ref(),
&Instance::get_local(&conn) Some(
.expect("post::create: local instance error") &Instance::get_local(&conn)
.public_domain, .expect("post::create: local instance error")
.public_domain,
),
false, false,
Some(Media::get_media_processor( Some(Media::get_media_processor(
&conn, &conn,