Commit 16961a1f6e

16961a1f6e071bdd480ae1cb4fad751b974ad783

parent: 8846849cd8

Unregistered key

Ulyssa <git@ulyssa.dev> · 2026-09-14 02:44 UTC
committer: <noreply@github.com>

Use a broken link callback to simplify matrix.to links (#693)

Layout: unified · split

src/base.rs +7 −12
@@ -1087,22 +1087,17 @@ impl DisplayNameStore {
1087 .into_iter() 1087 .into_iter()
1088 .flat_map(|name| { 1088 .flat_map(|name| {
1089 let users = self.by_names.get(&name).unwrap(); 1089 let users = self.by_names.get(&name).unwrap();
1090 users 1090 users.active.iter().map(move |id| format!("[{name}][{}]", id))
1091 .active
1092 .iter()
1093 .map(move |id| format!("[{name}]({})", id.matrix_to_uri()))
1094 }) 1091 })
1095 .collect(); 1092 .collect();
1096 1093
1097 users.extend(self.by_ids.complete(prefix).into_iter().map(|id| { 1094 users.extend(self.by_ids.complete(prefix).into_iter().map(|id| {
1098 format!( 1095 let name = self
1099 "[{}]({})", 1096 .by_ids
1100 self.by_ids 1097 .get(&id)
1101 .get(&id) 1098 .and_then(|(name, _)| name.as_deref())
1102 .and_then(|(name, _)| name.as_deref()) 1099 .unwrap_or(id.as_str());
1103 .unwrap_or(id.as_str()), 1100 format!("[{}][{}]", name, id)
1104 id.matrix_to_uri()
1105 )
1106 })); 1101 }));
1107 1102
1108 users.into_iter().collect() 1103 users.into_iter().collect()
src/completions.rs +34 −5
@@ -697,26 +697,39 @@ fn complete_msgbar(
697 .unwrap_or_else(EditRope::empty); 697 .unwrap_or_else(EditRope::empty);
698 let id = Cow::from(&id); 698 let id = Cow::from(&id);
699 699
700 let mut bracket_cursor = cursor.clone();
701 bracket_cursor.left(1);
702 let bracket = text.get_char_at_cursor(&bracket_cursor);
703 let has_bracket = bracket.is_some_and(|c| c == '[');
704
700 let info = store.rooms.get_or_default(room_id.to_owned()); 705 let info = store.rooms.get_or_default(room_id.to_owned());
701 706
702 match id.chars().next() { 707 match id.chars().next() {
703 // Complete room aliases. 708 // Complete room aliases.
704 Some('#') => { 709 Some('#') => {
710 if has_bracket {
711 *cursor = bracket_cursor;
712 }
713
705 store 714 store
706 .names 715 .names
707 .complete(id.as_ref()) 716 .complete(id.as_ref())
708 .into_iter() 717 .into_iter()
709 .map(|i| format!("[{}]({})", i, i.matrix_to_uri())) 718 .map(|i| format!("[{}]", i))
710 .collect() 719 .collect()
711 }, 720 },
712 721
713 // Complete room identifiers. 722 // Complete room identifiers.
714 Some('!') => { 723 Some('!') => {
724 if has_bracket {
725 *cursor = bracket_cursor;
726 }
727
715 store 728 store
716 .rooms 729 .rooms
717 .complete(id.as_ref()) 730 .complete(id.as_ref())
718 .into_iter() 731 .into_iter()
719 .map(|i| format!("[{}]({})", i, i.matrix_to_uri())) 732 .map(|i| format!("[{}]", i))
720 .collect() 733 .collect()
721 }, 734 },
722 735
@@ -729,7 +742,13 @@ fn complete_msgbar(
729 }, 742 },
730 743
731 // Complete usernames for @ and empty strings. 744 // Complete usernames for @ and empty strings.
732 Some('@') | None => info.display_names.complete_mention(&id), 745 Some('@') | None => {
746 if has_bracket {
747 *cursor = bracket_cursor;
748 }
749
750 info.display_names.complete_mention(&id)
751 },
733 752
734 // Unknown sigil. 753 // Unknown sigil.
735 Some(_) => return vec![], 754 Some(_) => return vec![],
@@ -785,22 +804,32 @@ pub mod tests {
785 let mut store = store.application; 804 let mut store = store.application;
786 let room_id = TEST_ROOM1_ID.clone(); 805 let room_id = TEST_ROOM1_ID.clone();
787 806
807 // Complete Emoji shorthand:
788 let text = EditRope::from("going for a walk :walk "); 808 let text = EditRope::from("going for a walk :walk ");
789 let mut cursor = Cursor::new(0, 22); 809 let mut cursor = Cursor::new(0, 22);
790 let res = complete_msgbar(&text, &mut cursor, &mut store, &room_id); 810 let res = complete_msgbar(&text, &mut cursor, &mut store, &room_id);
791 assert_eq!(res, vec![":walking:", ":walking_man:", ":walking_woman:"]); 811 assert_eq!(res, vec![":walking:", ":walking_man:", ":walking_woman:"]);
792 assert_eq!(cursor, Cursor::new(0, 17)); 812 assert_eq!(cursor, Cursor::new(0, 17));
793 813
814 // Complete user identifier:
794 let text = EditRope::from("hello @user2 "); 815 let text = EditRope::from("hello @user2 ");
795 let mut cursor = Cursor::new(0, 12); 816 let mut cursor = Cursor::new(0, 12);
796 let res = complete_msgbar(&text, &mut cursor, &mut store, &room_id); 817 let res = complete_msgbar(&text, &mut cursor, &mut store, &room_id);
797 assert_eq!(res, vec!["[User 2](https://matrix.to/#/@user2:example.com)"]); 818 assert_eq!(res, vec!["[User 2][@user2:example.com]"]);
798 assert_eq!(cursor, Cursor::new(0, 6)); 819 assert_eq!(cursor, Cursor::new(0, 6));
799 820
821 // Complete room identifier:
800 let text = EditRope::from("see #room "); 822 let text = EditRope::from("see #room ");
801 let mut cursor = Cursor::new(0, 9); 823 let mut cursor = Cursor::new(0, 9);
802 let res = complete_msgbar(&text, &mut cursor, &mut store, &room_id); 824 let res = complete_msgbar(&text, &mut cursor, &mut store, &room_id);
803 assert_eq!(res, vec!["[#room1:example.com](https://matrix.to/#/%23room1:example.com)"]); 825 assert_eq!(res, vec!["[#room1:example.com]"]);
826 assert_eq!(cursor, Cursor::new(0, 4));
827
828 // Complete room identifier with bracket before `#`:
829 let text = EditRope::from("see [#room ");
830 let mut cursor = Cursor::new(0, 10);
831 let res = complete_msgbar(&text, &mut cursor, &mut store, &room_id);
832 assert_eq!(res, vec!["[#room1:example.com]"]);
804 assert_eq!(cursor, Cursor::new(0, 4)); 833 assert_eq!(cursor, Cursor::new(0, 4));
805 } 834 }
806 835
src/message/compose.rs +78 −2
@@ -1,7 +1,8 @@
1//! Code for converting composed messages into content to send to the homeserver. 1//! Code for converting composed messages into content to send to the homeserver.
2use std::sync::Arc;
2 3
3use comrak::markdown_to_html; 4use comrak::options::{BrokenLinkReference, Options};
4use comrak::options::Options; 5use comrak::{ResolvedReference, markdown_to_html};
5use matrix_sdk::ruma::events::room::message::{EmoteMessageEventContent, TextMessageEventContent}; 6use matrix_sdk::ruma::events::room::message::{EmoteMessageEventContent, TextMessageEventContent};
6use nom::branch::alt; 7use nom::branch::alt;
7use nom::bytes::complete::tag; 8use nom::bytes::complete::tag;
@@ -12,6 +13,23 @@ use nom::{IResult, Parser as _};
12use crate::config::MarkupFormat; 13use crate::config::MarkupFormat;
13use crate::prelude::*; 14use crate::prelude::*;
14 15
16fn broken_link_handler(link: BrokenLinkReference<'_>) -> Option<ResolvedReference> {
17 let uri = if let Ok(user_id) = OwnedUserId::from_str(link.normalized) {
18 user_id.matrix_to_uri()
19 } else if let Ok(room_id) = OwnedRoomAliasId::from_str(link.normalized) {
20 room_id.matrix_to_uri()
21 } else if let Ok(room_id) = OwnedRoomId::from_str(link.normalized) {
22 room_id.matrix_to_uri()
23 } else {
24 return None;
25 };
26
27 Some(ResolvedReference {
28 url: uri.to_string(),
29 title: link.normalized.to_string(),
30 })
31}
32
15#[derive(Clone, Debug, Default)] 33#[derive(Clone, Debug, Default)]
16enum SlashCommand { 34enum SlashCommand {
17 /// Send an emote message. 35 /// Send an emote message.
@@ -166,6 +184,7 @@ fn text_to_html(input: &str) -> Option<String> {
166 options.extension.autolink = true; 184 options.extension.autolink = true;
167 options.extension.shortcodes = true; 185 options.extension.shortcodes = true;
168 options.extension.strikethrough = true; 186 options.extension.strikethrough = true;
187 options.parse.broken_link_callback = Some(Arc::new(broken_link_handler));
169 options.render.hardbreaks = true; 188 options.render.hardbreaks = true;
170 markdown_to_html(input, &options).into() 189 markdown_to_html(input, &options).into()
171} 190}
@@ -241,6 +260,63 @@ pub mod tests {
241 ); 260 );
242 } 261 }
243 262
263 #[test]
264 fn test_markdown_link_alias() {
265 let input = "[#room1:example.com]\n";
266 let content = text_to_message_content(input.into());
267 assert_eq!(content.body, input);
268 assert_eq!(
269 content.formatted.unwrap().body,
270 "<p><a href=\"https://matrix.to/#/%23room1:example.com\" title=\"#room1:example.com\">#room1:example.com</a></p>\n"
271 );
272
273 let input = "See [other room][#room1:example.com]\n";
274 let content = text_to_message_content(input.into());
275 assert_eq!(content.body, input);
276 assert_eq!(
277 content.formatted.unwrap().body,
278 "<p>See <a href=\"https://matrix.to/#/%23room1:example.com\" title=\"#room1:example.com\">other room</a></p>\n"
279 );
280 }
281
282 #[test]
283 fn test_markdown_link_room() {
284 let input = "[!room1:example.com]\n";
285 let content = text_to_message_content(input.into());
286 assert_eq!(content.body, input);
287 assert_eq!(
288 content.formatted.unwrap().body,
289 "<p><a href=\"https://matrix.to/#/!room1:example.com\" title=\"!room1:example.com\">!room1:example.com</a></p>\n"
290 );
291
292 let input = "See [other room][!room1:example.com]\n";
293 let content = text_to_message_content(input.into());
294 assert_eq!(content.body, input);
295 assert_eq!(
296 content.formatted.unwrap().body,
297 "<p>See <a href=\"https://matrix.to/#/!room1:example.com\" title=\"!room1:example.com\">other room</a></p>\n"
298 );
299 }
300
301 #[test]
302 fn test_markdown_link_user() {
303 let input = "[@user1:example.com]\n";
304 let content = text_to_message_content(input.into());
305 assert_eq!(content.body, input);
306 assert_eq!(
307 content.formatted.unwrap().body,
308 "<p><a href=\"https://matrix.to/#/@user1:example.com\" title=\"@user1:example.com\">@user1:example.com</a></p>\n"
309 );
310
311 let input = "Talk to [Jane][@user1:example.com]\n";
312 let content = text_to_message_content(input.into());
313 assert_eq!(content.body, input);
314 assert_eq!(
315 content.formatted.unwrap().body,
316 "<p>Talk to <a href=\"https://matrix.to/#/@user1:example.com\" title=\"@user1:example.com\">Jane</a></p>\n"
317 );
318 }
319
244 #[test] 320 #[test]
245 fn test_markdown_message() { 321 fn test_markdown_message() {
246 let input = "**bold**\n"; 322 let input = "**bold**\n";