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)
pub fn md_to_html<'a>(
md: &str,
base_url: &str,
base_url: Option<&str>,
inline: bool,
media_processor: Option<MediaProcessor<'a>>,
) -> (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, mentions, hashtags): (Vec<Event>, Vec<String>, Vec<String>) = 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("<h1>Hello</h1>\n")
);
assert_eq!(
md_to_html("# Hello", "", true, None).0,
md_to_html("# Hello", None, true, None).0,
String::from("<p>Hello</p>\n")
);
}

View File

@ -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])),
);

View File

@ -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![])),
);

View File

@ -692,7 +692,7 @@ impl FromId<PlumeRocket> 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<User, Update, &PlumeRocket> 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())

View File

@ -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,

View File

@ -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])),
);

View File

@ -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,

View File

@ -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])),
);

View File

@ -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,