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 {
10871087 .into_iter()
10881088 .flat_map(|name| {
10891089 let users = self.by_names.get(&name).unwrap();
1090 users
1091 .active
1092 .iter()
1093 .map(move |id| format!("[{name}]({})", id.matrix_to_uri()))
1090 users.active.iter().map(move |id| format!("[{name}][{}]", id))
10941091 })
10951092 .collect();
10961093
10971094 users.extend(self.by_ids.complete(prefix).into_iter().map(|id| {
1098 format!(
1099 "[{}]({})",
1100 self.by_ids
1101 .get(&id)
1102 .and_then(|(name, _)| name.as_deref())
1103 .unwrap_or(id.as_str()),
1104 id.matrix_to_uri()
1105 )
1095 let name = self
1096 .by_ids
1097 .get(&id)
1098 .and_then(|(name, _)| name.as_deref())
1099 .unwrap_or(id.as_str());
1100 format!("[{}][{}]", name, id)
11061101 }));
11071102
11081103 users.into_iter().collect()
src/completions.rs +34 −5
@@ -697,26 +697,39 @@ fn complete_msgbar(
697697 .unwrap_or_else(EditRope::empty);
698698 let id = Cow::from(&id);
699699
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
700705 let info = store.rooms.get_or_default(room_id.to_owned());
701706
702707 match id.chars().next() {
703708 // Complete room aliases.
704709 Some('#') => {
710 if has_bracket {
711 *cursor = bracket_cursor;
712 }
713
705714 store
706715 .names
707716 .complete(id.as_ref())
708717 .into_iter()
709 .map(|i| format!("[{}]({})", i, i.matrix_to_uri()))
718 .map(|i| format!("[{}]", i))
710719 .collect()
711720 },
712721
713722 // Complete room identifiers.
714723 Some('!') => {
724 if has_bracket {
725 *cursor = bracket_cursor;
726 }
727
715728 store
716729 .rooms
717730 .complete(id.as_ref())
718731 .into_iter()
719 .map(|i| format!("[{}]({})", i, i.matrix_to_uri()))
732 .map(|i| format!("[{}]", i))
720733 .collect()
721734 },
722735
@@ -729,7 +742,13 @@ fn complete_msgbar(
729742 },
730743
731744 // 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 },
733752
734753 // Unknown sigil.
735754 Some(_) => return vec![],
@@ -785,22 +804,32 @@ pub mod tests {
785804 let mut store = store.application;
786805 let room_id = TEST_ROOM1_ID.clone();
787806
807 // Complete Emoji shorthand:
788808 let text = EditRope::from("going for a walk :walk ");
789809 let mut cursor = Cursor::new(0, 22);
790810 let res = complete_msgbar(&text, &mut cursor, &mut store, &room_id);
791811 assert_eq!(res, vec![":walking:", ":walking_man:", ":walking_woman:"]);
792812 assert_eq!(cursor, Cursor::new(0, 17));
793813
814 // Complete user identifier:
794815 let text = EditRope::from("hello @user2 ");
795816 let mut cursor = Cursor::new(0, 12);
796817 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]"]);
798819 assert_eq!(cursor, Cursor::new(0, 6));
799820
821 // Complete room identifier:
800822 let text = EditRope::from("see #room ");
801823 let mut cursor = Cursor::new(0, 9);
802824 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]"]);
804833 assert_eq!(cursor, Cursor::new(0, 4));
805834 }
806835
src/message/compose.rs +78 −2
@@ -1,7 +1,8 @@
11//! Code for converting composed messages into content to send to the homeserver.
2use std::sync::Arc;
23
3use comrak::markdown_to_html;
4use comrak::options::Options;
4use comrak::options::{BrokenLinkReference, Options};
5use comrak::{ResolvedReference, markdown_to_html};
56use matrix_sdk::ruma::events::room::message::{EmoteMessageEventContent, TextMessageEventContent};
67use nom::branch::alt;
78use nom::bytes::complete::tag;
@@ -12,6 +13,23 @@ use nom::{IResult, Parser as _};
1213use crate::config::MarkupFormat;
1314use crate::prelude::*;
1415
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
1533#[derive(Clone, Debug, Default)]
1634enum SlashCommand {
1735 /// Send an emote message.
@@ -166,6 +184,7 @@ fn text_to_html(input: &str) -> Option<String> {
166184 options.extension.autolink = true;
167185 options.extension.shortcodes = true;
168186 options.extension.strikethrough = true;
187 options.parse.broken_link_callback = Some(Arc::new(broken_link_handler));
169188 options.render.hardbreaks = true;
170189 markdown_to_html(input, &options).into()
171190}
@@ -241,6 +260,63 @@ pub mod tests {
241260 );
242261 }
243262
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
244320 #[test]
245321 fn test_markdown_message() {
246322 let input = "**bold**\n";