From 24ef3d00d1ac884a53208f7ca1bb6265db90f710 Mon Sep 17 00:00:00 2001 From: Bat Date: Wed, 20 Jun 2018 16:25:09 +0100 Subject: [PATCH] Fix the bugs with markdown rendering when there was mentions --- src/utils.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index 597ad09c..787adcbc 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -26,12 +26,13 @@ pub fn md_to_html(md: &str) -> String { let parser = parser.flat_map(|evt| match evt { Event::Text(txt) => txt.chars().fold((vec![], false, String::new(), 0), |(mut events, in_mention, text_acc, n), c| { if in_mention { - if c.is_alphanumeric() || c == '@' || c == '.' || c == '-' || c == '_' { + if (c.is_alphanumeric() || c == '@' || c == '.' || c == '-' || c == '_') && (n < (txt.chars().count() - 1)) { (events, in_mention, text_acc + c.to_string().as_ref(), n + 1) } else { - let short_mention = text_acc.clone(); + let mention = text_acc + c.to_string().as_ref(); + let short_mention = mention.clone(); let short_mention = short_mention.splitn(1, '@').nth(0).unwrap_or(""); - let link = Tag::Link(format!("/@/{}/", text_acc).into(), short_mention.to_string().into()); + let link = Tag::Link(format!("/@/{}/", mention).into(), short_mention.to_string().into()); events.push(Event::Start(link.clone())); events.push(Event::Text(format!("@{}", short_mention).into())); @@ -44,7 +45,7 @@ pub fn md_to_html(md: &str) -> String { events.push(Event::Text(text_acc.into())); (events, true, String::new(), n + 1) } else { - if n >= (txt.len() - 1) { // Add the text after at the end, even if it is not followed by a mention. + if n >= (txt.chars().count() - 1) { // Add the text after at the end, even if it is not followed by a mention. events.push(Event::Text((text_acc.clone() + c.to_string().as_ref()).into())) } (events, in_mention, text_acc + c.to_string().as_ref(), n + 1)