Commit e63341fe32

e63341fe32746f134e5962556913765cf0f2a858

parent: 657e61fe2e

Unregistered key

Ulyssa <git@ulyssa.dev> · 2024-08-17 06:56 UTC
committer: <noreply@github.com>

Avoid treating simple messages as Markdown (#325)

Layout: unified · split

src/message/compose.rs +98 −12
@@ -53,8 +53,12 @@ impl SlashCommand {
5353 fn to_message(&self, input: &str) -> anyhow::Result<MessageType> {
5454 let msgtype = match self {
5555 SlashCommand::Emote => {
56 let html = text_to_html(input);
57 let msg = EmoteMessageEventContent::html(input, html);
56 let msg = if let Some(html) = text_to_html(input) {
57 EmoteMessageEventContent::html(input, html)
58 } else {
59 EmoteMessageEventContent::plain(input)
60 };
61
5862 MessageType::Emote(msg)
5963 },
6064 SlashCommand::Html => {
@@ -66,8 +70,7 @@ impl SlashCommand {
6670 MessageType::Text(msg)
6771 },
6872 SlashCommand::Markdown => {
69 let html = text_to_html(input);
70 let msg = TextMessageEventContent::html(input, html);
73 let msg = text_to_message_content(input.to_string());
7174 MessageType::Text(msg)
7275 },
7376 SlashCommand::Confetti => {
@@ -128,18 +131,43 @@ fn parse_slash_command(input: &str) -> anyhow::Result<(&str, SlashCommand)> {
128131 }
129132}
130133
131fn 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.
139fn 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.
148fn not_markdown(input: &str) -> bool {
149 input.chars().all(not_markdown_char)
150}
151
152fn text_to_html(input: &str) -> Option<String> {
153 if not_markdown(input) {
154 return None;
155 }
156
132157 let mut options = ComrakOptions::default();
133158 options.extension.autolink = true;
134159 options.extension.shortcodes = true;
135160 options.extension.strikethrough = true;
136161 options.render.hardbreaks = true;
137 markdown_to_html(input, &options)
162 markdown_to_html(input, &options).into()
138163}
139164
140165fn text_to_message_content(input: String) -> TextMessageEventContent {
141 let html = text_to_html(input.as_str());
142 TextMessageEventContent::html(input, html)
166 if let Some(html) = text_to_html(input.as_str()) {
167 TextMessageEventContent::html(input, html)
168 } else {
169 TextMessageEventContent::plain(input)
170 }
143171}
144172
145173pub fn text_to_message(input: String) -> RoomMessageEventContent {
@@ -211,15 +239,18 @@ pub mod tests {
211239 assert_eq!(content.body, input);
212240 assert_eq!(content.formatted.unwrap().body, "<p>\u{2764}\u{FE0F}</p>\n");
213241
214 let input = "para 1\n\npara 2\n";
242 let input = "para *1*\n\npara _2_\n";
215243 let content = text_to_message_content(input.into());
216244 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 );
218249
219 let input = "line 1\nline 2\n";
250 let input = "line 1\nline ~~2~~\n";
220251 let content = text_to_message_content(input.into());
221252 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");
223254
224255 let input = "# Heading\n## Subheading\n\ntext\n";
225256 let content = text_to_message_content(input.into());
@@ -230,6 +261,61 @@ pub mod tests {
230261 );
231262 }
232263
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
233319 #[test]
234320 fn text_to_message_slash_commands() {
235321 let MessageType::Text(content) = text_to_message("/html <b>bold</b>".into()).msgtype else {