Commit e63341fe32
Unregistered key
Layout: unified · split
src/message/compose.rs +98 −12
| @@ -53,8 +53,12 @@ impl SlashCommand { | |||
| 53 | fn to_message(&self, input: &str) -> anyhow::Result<MessageType> { | 53 | fn to_message(&self, input: &str) -> anyhow::Result<MessageType> { |
| 54 | let msgtype = match self { | 54 | let msgtype = match self { |
| 55 | SlashCommand::Emote => { | 55 | SlashCommand::Emote => { |
| 56 | let html = text_to_html(input); | 56 | let msg = if let Some(html) = text_to_html(input) { |
| 57 | let msg = EmoteMessageEventContent::html(input, html); | 57 | EmoteMessageEventContent::html(input, html) |
| 58 | } else { | ||
| 59 | EmoteMessageEventContent::plain(input) | ||
| 60 | }; | ||
| 61 | |||
| 58 | MessageType::Emote(msg) | 62 | MessageType::Emote(msg) |
| 59 | }, | 63 | }, |
| 60 | SlashCommand::Html => { | 64 | SlashCommand::Html => { |
| @@ -66,8 +70,7 @@ impl SlashCommand { | |||
| 66 | MessageType::Text(msg) | 70 | MessageType::Text(msg) |
| 67 | }, | 71 | }, |
| 68 | SlashCommand::Markdown => { | 72 | SlashCommand::Markdown => { |
| 69 | let html = text_to_html(input); | 73 | let msg = text_to_message_content(input.to_string()); |
| 70 | let msg = TextMessageEventContent::html(input, html); | ||
| 71 | MessageType::Text(msg) | 74 | MessageType::Text(msg) |
| 72 | }, | 75 | }, |
| 73 | SlashCommand::Confetti => { | 76 | SlashCommand::Confetti => { |
| @@ -128,18 +131,43 @@ fn parse_slash_command(input: &str) -> anyhow::Result<(&str, SlashCommand)> { | |||
| 128 | } | 131 | } |
| 129 | } | 132 | } |
| 130 | 133 | ||
| 131 | fn text_to_html(input: &str) -> String { | 134 | /// Check whether this character is not used for markup in Markdown. |
| 135 | /// | ||
| 136 | /// Markdown uses just about every ASCII punctuation symbol in some way, especially | ||
| 137 | /// once autolinking is involved, so we really just check whether it's non-punctuation or | ||
| 138 | /// single/double quotations. | ||
| 139 | fn not_markdown_char(c: char) -> bool { | ||
| 140 | if !c.is_ascii_punctuation() { | ||
| 141 | return true; | ||
| 142 | } | ||
| 143 | |||
| 144 | matches!(c, '"' | '\'') | ||
| 145 | } | ||
| 146 | |||
| 147 | /// Check whether the input actually needs to be processed as Markdown. | ||
| 148 | fn not_markdown(input: &str) -> bool { | ||
| 149 | input.chars().all(not_markdown_char) | ||
| 150 | } | ||
| 151 | |||
| 152 | fn text_to_html(input: &str) -> Option<String> { | ||
| 153 | if not_markdown(input) { | ||
| 154 | return None; | ||
| 155 | } | ||
| 156 | |||
| 132 | let mut options = ComrakOptions::default(); | 157 | let mut options = ComrakOptions::default(); |
| 133 | options.extension.autolink = true; | 158 | options.extension.autolink = true; |
| 134 | options.extension.shortcodes = true; | 159 | options.extension.shortcodes = true; |
| 135 | options.extension.strikethrough = true; | 160 | options.extension.strikethrough = true; |
| 136 | options.render.hardbreaks = true; | 161 | options.render.hardbreaks = true; |
| 137 | markdown_to_html(input, &options) | 162 | markdown_to_html(input, &options).into() |
| 138 | } | 163 | } |
| 139 | 164 | ||
| 140 | fn text_to_message_content(input: String) -> TextMessageEventContent { | 165 | fn text_to_message_content(input: String) -> TextMessageEventContent { |
| 141 | let html = text_to_html(input.as_str()); | 166 | if let Some(html) = text_to_html(input.as_str()) { |
| 142 | TextMessageEventContent::html(input, html) | 167 | TextMessageEventContent::html(input, html) |
| 168 | } else { | ||
| 169 | TextMessageEventContent::plain(input) | ||
| 170 | } | ||
| 143 | } | 171 | } |
| 144 | 172 | ||
| 145 | pub fn text_to_message(input: String) -> RoomMessageEventContent { | 173 | pub fn text_to_message(input: String) -> RoomMessageEventContent { |
| @@ -211,15 +239,18 @@ pub mod tests { | |||
| 211 | assert_eq!(content.body, input); | 239 | assert_eq!(content.body, input); |
| 212 | assert_eq!(content.formatted.unwrap().body, "<p>\u{2764}\u{FE0F}</p>\n"); | 240 | assert_eq!(content.formatted.unwrap().body, "<p>\u{2764}\u{FE0F}</p>\n"); |
| 213 | 241 | ||
| 214 | let input = "para 1\n\npara 2\n"; | 242 | let input = "para *1*\n\npara _2_\n"; |
| 215 | let content = text_to_message_content(input.into()); | 243 | let content = text_to_message_content(input.into()); |
| 216 | assert_eq!(content.body, input); | 244 | assert_eq!(content.body, input); |
| 217 | assert_eq!(content.formatted.unwrap().body, "<p>para 1</p>\n<p>para 2</p>\n"); | 245 | assert_eq!( |
| 246 | content.formatted.unwrap().body, | ||
| 247 | "<p>para <em>1</em></p>\n<p>para <em>2</em></p>\n" | ||
| 248 | ); | ||
| 218 | 249 | ||
| 219 | let input = "line 1\nline 2\n"; | 250 | let input = "line 1\nline ~~2~~\n"; |
| 220 | let content = text_to_message_content(input.into()); | 251 | let content = text_to_message_content(input.into()); |
| 221 | assert_eq!(content.body, input); | 252 | assert_eq!(content.body, input); |
| 222 | assert_eq!(content.formatted.unwrap().body, "<p>line 1<br />\nline 2</p>\n"); | 253 | assert_eq!(content.formatted.unwrap().body, "<p>line 1<br />\nline <del>2</del></p>\n"); |
| 223 | 254 | ||
| 224 | let input = "# Heading\n## Subheading\n\ntext\n"; | 255 | let input = "# Heading\n## Subheading\n\ntext\n"; |
| 225 | let content = text_to_message_content(input.into()); | 256 | let content = text_to_message_content(input.into()); |
| @@ -230,6 +261,61 @@ pub mod tests { | |||
| 230 | ); | 261 | ); |
| 231 | } | 262 | } |
| 232 | 263 | ||
| 264 | #[test] | ||
| 265 | fn test_markdown_headers() { | ||
| 266 | let input = "hello\n=====\n"; | ||
| 267 | let content = text_to_message_content(input.into()); | ||
| 268 | assert_eq!(content.body, input); | ||
| 269 | assert_eq!(content.formatted.unwrap().body, "<h1>hello</h1>\n"); | ||
| 270 | |||
| 271 | let input = "hello\n-----\n"; | ||
| 272 | let content = text_to_message_content(input.into()); | ||
| 273 | assert_eq!(content.body, input); | ||
| 274 | assert_eq!(content.formatted.unwrap().body, "<h2>hello</h2>\n"); | ||
| 275 | } | ||
| 276 | |||
| 277 | #[test] | ||
| 278 | fn test_markdown_lists() { | ||
| 279 | let input = "- A\n- B\n- C\n"; | ||
| 280 | let content = text_to_message_content(input.into()); | ||
| 281 | assert_eq!(content.body, input); | ||
| 282 | assert_eq!( | ||
| 283 | content.formatted.unwrap().body, | ||
| 284 | "<ul>\n<li>A</li>\n<li>B</li>\n<li>C</li>\n</ul>\n" | ||
| 285 | ); | ||
| 286 | |||
| 287 | let input = "1) A\n2) B\n3) C\n"; | ||
| 288 | let content = text_to_message_content(input.into()); | ||
| 289 | assert_eq!(content.body, input); | ||
| 290 | assert_eq!( | ||
| 291 | content.formatted.unwrap().body, | ||
| 292 | "<ol>\n<li>A</li>\n<li>B</li>\n<li>C</li>\n</ol>\n" | ||
| 293 | ); | ||
| 294 | } | ||
| 295 | |||
| 296 | #[test] | ||
| 297 | fn test_no_markdown_conversion_on_simple_text() { | ||
| 298 | let input = "para 1\n\npara 2\n"; | ||
| 299 | let content = text_to_message_content(input.into()); | ||
| 300 | assert_eq!(content.body, input); | ||
| 301 | assert!(content.formatted.is_none()); | ||
| 302 | |||
| 303 | let input = "line 1\nline 2\n"; | ||
| 304 | let content = text_to_message_content(input.into()); | ||
| 305 | assert_eq!(content.body, input); | ||
| 306 | assert!(content.formatted.is_none()); | ||
| 307 | |||
| 308 | let input = "isn't markdown\n"; | ||
| 309 | let content = text_to_message_content(input.into()); | ||
| 310 | assert_eq!(content.body, input); | ||
| 311 | assert!(content.formatted.is_none()); | ||
| 312 | |||
| 313 | let input = "\"scare quotes\"\n"; | ||
| 314 | let content = text_to_message_content(input.into()); | ||
| 315 | assert_eq!(content.body, input); | ||
| 316 | assert!(content.formatted.is_none()); | ||
| 317 | } | ||
| 318 | |||
| 233 | #[test] | 319 | #[test] |
| 234 | fn text_to_message_slash_commands() { | 320 | fn text_to_message_slash_commands() { |
| 235 | let MessageType::Text(content) = text_to_message("/html <b>bold</b>".into()).msgtype else { | 321 | let MessageType::Text(content) = text_to_message("/html <b>bold</b>".into()).msgtype else { |