Commit 2d48a2ace9

2d48a2ace9b6ebfcd37240e57851f429789c5c4f

parent: 1045a318c2

Unregistered key

Ulyssa <git@ulyssa.dev> · 2026-08-16 16:00 UTC
committer: <noreply@github.com>

Add `default_markup` for configuring message parsing (#643)

Layout: unified · split

config.example.toml +1
@@ -6,6 +6,7 @@ password_file = "/path/to/password"
66url = "https://matrix.org"
77
88[settings]
9default_markup = "html"
910default_room = "#iamb-users:0x.badd.cafe"
1011default_split = "vertical"
1112external_edit_file_suffix = ".md"
docs/iamb.5 +12 −5
@@ -126,11 +126,13 @@ These options are configured as an object under the
126126key and can be overridden as described in
127127.Sx PROFILES .
128128.Bl -tag -width Ds
129.It Sy encryption
130Configuration subsection for specific settings related to room encryption.
131See
132.Sx ENCRYPTION
133for more details.
129.It Sy default_markup
130Controls how text in the message bar is interpreted by default.
131Possible values are
132.Dq Sy html ,
133.Dq Sy markdown
134(the default) and
135.Dq Sy plaintext .
134136.It Sy default_room
135137The room to show by default instead of the
136138.Sy :welcome
@@ -145,6 +147,11 @@ Possible values are
145147.Dq Sy horizontal
146148(the default) and
147149.Dq Sy vertical .
150.It Sy encryption
151Configuration subsection for specific settings related to room encryption.
152See
153.Sx ENCRYPTION
154for more details.
148155.It Sy external_edit_file_suffix
149156Suffix to append to temporary file names when using the :editor command. Defaults to .md.
150157.It Sy image_preview
src/config.rs +14
@@ -803,6 +803,7 @@ pub struct TerminalValues {
803803#[derive(Clone)]
804804pub struct TunableValues {
805805 pub encryption: EncryptionValues,
806 pub default_markup: MarkupFormat,
806807 pub log_level: String,
807808 pub max_log_files: usize,
808809 pub message_shortcode_display: bool,
@@ -855,6 +856,7 @@ pub struct Tunables {
855856 /// Subsection for overriding how specific Matrix users are rendered.
856857 pub users: Option<UserOverrides>,
857858
859 pub default_markup: Option<MarkupFormat>,
858860 pub log_level: Option<String>,
859861 pub max_log_files: Option<usize>,
860862 pub message_shortcode_display: Option<bool>,
@@ -896,6 +898,7 @@ impl Tunables {
896898 // global settings.
897899 proxy: self.proxy.or(other.proxy),
898900
901 default_markup: self.default_markup.or(other.default_markup),
899902 log_level: self.log_level.or(other.log_level),
900903 max_log_files: self.max_log_files.or(other.max_log_files),
901904 message_shortcode_display: self
@@ -939,6 +942,7 @@ impl Tunables {
939942 terminal: self.terminal.values(),
940943 users: self.users.unwrap_or_default(),
941944
945 default_markup: self.default_markup.unwrap_or_default(),
942946 log_level: self.log_level.unwrap_or_else(|| "warn".to_string()),
943947 max_log_files: self.max_log_files.unwrap_or(7),
944948 message_shortcode_display: self.message_shortcode_display.unwrap_or(false),
@@ -993,6 +997,16 @@ impl From<CursorShape> for modalkit::crossterm::cursor::SetCursorStyle {
993997 }
994998}
995999
1000#[derive(Copy, Clone, Debug, Default, Deserialize, Eq, PartialEq)]
1001#[serde(rename_all = "kebab-case")]
1002#[repr(u8)]
1003pub enum MarkupFormat {
1004 Html,
1005 #[default]
1006 Markdown,
1007 Plaintext,
1008}
1009
9961010#[derive(Clone)]
9971011pub struct DirectoryValues {
9981012 pub cache: PathBuf,
src/message/compose.rs +82 −28
@@ -16,6 +16,8 @@ use matrix_sdk::ruma::events::room::message::{
1616 TextMessageEventContent,
1717};
1818
19use crate::config::MarkupFormat;
20
1921#[derive(Clone, Debug, Default)]
2022enum SlashCommand {
2123 /// Send an emote message.
@@ -102,6 +104,16 @@ impl SlashCommand {
102104 }
103105}
104106
107impl From<MarkupFormat> for SlashCommand {
108 fn from(markup: MarkupFormat) -> Self {
109 match markup {
110 MarkupFormat::Html => Self::Html,
111 MarkupFormat::Markdown => Self::Markdown,
112 MarkupFormat::Plaintext => Self::Plaintext,
113 }
114 }
115}
116
105117fn parse_slash_command_inner(input: &str) -> IResult<&str, SlashCommand> {
106118 let (input, _) = space0(input)?;
107119 let (input, slash) = alt((
@@ -172,37 +184,35 @@ fn text_to_message_content(input: String) -> TextMessageEventContent {
172184 }
173185}
174186
175pub fn text_to_message(input: String) -> RoomMessageEventContent {
176 let msg = parse_slash_command(input.as_str())
177 .and_then(|(input, slash)| slash.to_message(input))
187pub fn text_to_message(input: String, default_markup: MarkupFormat) -> RoomMessageEventContent {
188 let (rest, slash) = parse_slash_command(input.as_str())
189 .unwrap_or_else(|_| (&input, SlashCommand::from(default_markup)));
190 let msg = slash
191 .to_message(rest)
178192 .unwrap_or_else(|_| MessageType::Text(text_to_message_content(input)));
179193
180194 RoomMessageEventContent::new(msg)
181195}
182196
183197/// Returns `None` if `input` contains a non-text slash command.
184pub fn text_to_text_message_event_content(input: String) -> Option<TextMessageEventContent> {
185 let cmd = parse_slash_command(&input);
198pub fn text_to_text_message_event_content(
199 input: String,
200 default_markup: MarkupFormat,
201) -> Option<TextMessageEventContent> {
202 let (body, cmd) = parse_slash_command(&input)
203 .unwrap_or_else(|_| (&input, SlashCommand::from(default_markup)));
186204
187205 let content = match cmd {
188 Ok((body, SlashCommand::Html)) => TextMessageEventContent::html(body, body),
189 Ok((body, SlashCommand::Plaintext)) => TextMessageEventContent::plain(body),
190 Ok((body, SlashCommand::Markdown)) => {
206 SlashCommand::Html => TextMessageEventContent::html(body, body),
207 SlashCommand::Plaintext => TextMessageEventContent::plain(body),
208 SlashCommand::Markdown => {
191209 if let Some(html) = text_to_html(body) {
192210 TextMessageEventContent::html(body, html)
193211 } else {
194212 TextMessageEventContent::plain(body)
195213 }
196214 },
197 Ok(_) => return None,
198
199 _ => {
200 if let Some(html) = text_to_html(&input) {
201 TextMessageEventContent::html(input, html)
202 } else {
203 TextMessageEventContent::plain(input)
204 }
205 },
215 _ => return None,
206216 };
207217
208218 Some(content)
@@ -348,59 +358,103 @@ pub mod tests {
348358
349359 #[test]
350360 fn text_to_message_slash_commands() {
351 let MessageType::Text(content) = text_to_message("/html <b>bold</b>".into()).msgtype else {
361 let MessageType::Text(content) =
362 text_to_message("/html <b>bold</b>".into(), Default::default()).msgtype
363 else {
352364 panic!("Expected MessageType::Text");
353365 };
354366 assert_eq!(content.body, "<b>bold</b>");
355367 assert_eq!(content.formatted.unwrap().body, "<b>bold</b>");
356368
357 let MessageType::Text(content) = text_to_message("/h <b>bold</b>".into()).msgtype else {
369 let MessageType::Text(content) =
370 text_to_message("/h <b>bold</b>".into(), Default::default()).msgtype
371 else {
358372 panic!("Expected MessageType::Text");
359373 };
360374 assert_eq!(content.body, "<b>bold</b>");
361375 assert_eq!(content.formatted.unwrap().body, "<b>bold</b>");
362376
363 let MessageType::Text(content) = text_to_message("/plain <b>bold</b>".into()).msgtype
377 let MessageType::Text(content) =
378 text_to_message("/plain <b>bold</b>".into(), Default::default()).msgtype
364379 else {
365380 panic!("Expected MessageType::Text");
366381 };
367382 assert_eq!(content.body, "<b>bold</b>");
368383 assert!(content.formatted.is_none(), "{:?}", content.formatted);
369384
370 let MessageType::Text(content) = text_to_message("/p <b>bold</b>".into()).msgtype else {
385 let MessageType::Text(content) =
386 text_to_message("/p <b>bold</b>".into(), Default::default()).msgtype
387 else {
371388 panic!("Expected MessageType::Text");
372389 };
373390 assert_eq!(content.body, "<b>bold</b>");
374391 assert!(content.formatted.is_none(), "{:?}", content.formatted);
375392
376 let MessageType::Emote(content) = text_to_message("/me *bold*".into()).msgtype else {
393 let MessageType::Emote(content) =
394 text_to_message("/me *bold*".into(), Default::default()).msgtype
395 else {
377396 panic!("Expected MessageType::Emote");
378397 };
379398 assert_eq!(content.body, "*bold*");
380399 assert_eq!(content.formatted.unwrap().body, "<p><em>bold</em></p>\n");
381400
382 let content = text_to_message("/confetti hello".into()).msgtype;
401 let content = text_to_message("/confetti hello".into(), Default::default()).msgtype;
383402 assert_eq!(content.msgtype(), "nic.custom.confetti");
384403 assert_eq!(content.body(), "hello");
385404
386 let content = text_to_message("/fireworks hello".into()).msgtype;
405 let content = text_to_message("/fireworks hello".into(), Default::default()).msgtype;
387406 assert_eq!(content.msgtype(), "nic.custom.fireworks");
388407 assert_eq!(content.body(), "hello");
389408
390 let content = text_to_message("/hearts hello".into()).msgtype;
409 let content = text_to_message("/hearts hello".into(), Default::default()).msgtype;
391410 assert_eq!(content.msgtype(), "io.element.effect.hearts");
392411 assert_eq!(content.body(), "hello");
393412
394 let content = text_to_message("/rainfall hello".into()).msgtype;
413 let content = text_to_message("/rainfall hello".into(), Default::default()).msgtype;
395414 assert_eq!(content.msgtype(), "io.element.effect.rainfall");
396415 assert_eq!(content.body(), "hello");
397416
398 let content = text_to_message("/snowfall hello".into()).msgtype;
417 let content = text_to_message("/snowfall hello".into(), Default::default()).msgtype;
399418 assert_eq!(content.msgtype(), "io.element.effect.snowfall");
400419 assert_eq!(content.body(), "hello");
401420
402 let content = text_to_message("/spaceinvaders hello".into()).msgtype;
421 let content = text_to_message("/spaceinvaders hello".into(), Default::default()).msgtype;
403422 assert_eq!(content.msgtype(), "io.element.effects.space_invaders");
404423 assert_eq!(content.body(), "hello");
405424 }
425
426 #[test]
427 fn text_to_message_slash_default() {
428 let MessageType::Text(content) =
429 text_to_message("*hello*".into(), MarkupFormat::Html).msgtype
430 else {
431 panic!("Expected MessageType::Text");
432 };
433 assert_eq!(content.body, "*hello*");
434 assert_eq!(content.formatted.unwrap().body, "*hello*");
435
436 let MessageType::Text(content) =
437 text_to_message("/markdown *hello*".into(), MarkupFormat::Html).msgtype
438 else {
439 panic!("Expected MessageType::Text");
440 };
441 assert_eq!(content.body, "*hello*");
442 assert_eq!(content.formatted.unwrap().body, "<p><em>hello</em></p>\n");
443
444 let MessageType::Text(content) =
445 text_to_message("<b>hello</b>".into(), MarkupFormat::Plaintext).msgtype
446 else {
447 panic!("Expected MessageType::Text");
448 };
449 assert_eq!(content.body, "<b>hello</b>");
450 assert!(content.formatted.is_none(), "{:?}", content.formatted);
451
452 let MessageType::Text(content) =
453 text_to_message("/html <b>hello</b>".into(), MarkupFormat::Plaintext).msgtype
454 else {
455 panic!("Expected MessageType::Text");
456 };
457 assert_eq!(content.body, "<b>hello</b>");
458 assert_eq!(content.formatted.unwrap().body, "<b>hello</b>");
459 }
406460}
src/tests.rs +1
@@ -166,6 +166,7 @@ pub fn mock_dirs() -> DirectoryValues {
166166
167167pub fn mock_tunables() -> TunableValues {
168168 TunableValues {
169 default_markup: Default::default(),
169170 default_room: None,
170171 encryption: Encryption::default().values(),
171172 log_level: "warn".into(),
src/windows/room/chat.rs +11 −6
@@ -89,7 +89,7 @@ use crate::base::{
8989 SendAction,
9090};
9191
92use crate::config::EncryptionIndicatorLocation;
92use crate::config::{ApplicationSettings, EncryptionIndicatorLocation};
9393use crate::message::{
9494 MessageEvent,
9595 MessageId,
@@ -604,13 +604,18 @@ impl ChatState {
604604 }
605605
606606 /// Generate an attachment for this room based on the current message bar state.
607 fn generate_attachment_config(&self, info: &RoomInfo, add_caption: bool) -> AttachmentConfig {
607 fn generate_attachment_config(
608 &self,
609 info: &RoomInfo,
610 add_caption: bool,
611 settings: &ApplicationSettings,
612 ) -> AttachmentConfig {
608613 let mut config = AttachmentConfig::new();
609614 config.caption = add_caption
610615 .then(|| self.tbox.get())
611616 .filter(|c| !c.is_blank())
612617 .map(|c| c.trim_end().to_string())
613 .and_then(text_to_text_message_event_content);
618 .and_then(|c| text_to_text_message_event_content(c, settings.tunables.default_markup));
614619 config.reply = self.generate_reply_info(info, add_caption);
615620 config
616621 }
@@ -647,7 +652,7 @@ impl ChatState {
647652 msg.trim_end().to_string()
648653 };
649654
650 let mut msg = text_to_message(msg);
655 let mut msg = text_to_message(msg, tunables.default_markup);
651656
652657 if let Some(key) = &self.editing {
653658 let id = match &key.id {
@@ -740,7 +745,7 @@ impl ChatState {
740745 .unwrap_or_else(|| Cow::from("Attachment"));
741746
742747 let add_caption = add_caption.unwrap_or(false);
743 let config = self.generate_attachment_config(info, add_caption);
748 let config = self.generate_attachment_config(info, add_caption, settings);
744749
745750 room.send_queue()
746751 .send_attachment(name.as_ref(), mime, bytes, config)
@@ -766,7 +771,7 @@ impl ChatState {
766771 let mime = mime::IMAGE_PNG;
767772 let name = "Clipboard.png";
768773
769 let mut config = self.generate_attachment_config(info, add_caption);
774 let mut config = self.generate_attachment_config(info, add_caption, settings);
770775 config.info = Some(AttachmentInfo::Image(BaseImageInfo {
771776 height: height.try_into().ok(),
772777 width: width.try_into().ok(),