From 1f75e0cc94dc37ee21d4f884f961e50e4090e562 Mon Sep 17 00:00:00 2001 From: murmeldin Date: Fri, 20 Dec 2024 09:56:09 +0100 Subject: [PATCH] matrix formatting improvements - still in progress --- src/matrix.rs | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/matrix.rs b/src/matrix.rs index 9479c7e..3a782e8 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -2,6 +2,7 @@ use std::error::Error; use std::io::Read; use colored::Colorize; +use regex::Regex; use serde::{Deserialize, Serialize}; use std::collections::HashMap; use std::time::{SystemTime, UNIX_EPOCH}; @@ -177,7 +178,12 @@ impl MatrixClient { pub fn pandoc_convert_md_to_html(markdown: String) -> Result> { let (output, errors, status) = crate::pipe( "pandoc", - &mut ["--from", "markdown-auto_identifiers", "--to", "html5"], + &mut [ + "--from", "markdown-auto_identifiers", + "--to", "html5", + "--wrap=none", // Verhindert Zeilenumbrüche + "--no-highlight", // Deaktiviert Syntax-Highlighting + ], markdown, )?; if status.success() { @@ -188,6 +194,21 @@ impl MatrixClient { } } + pub fn format_matrix_html(markdown: String) -> Result> { + let mut html = Self::pandoc_convert_md_to_html(markdown)?; + + let url_regex = Regex::new(r"(https?://[^\s<]+)")?; + html = url_regex.replace_all(&html, r#"$1"#).to_string(); + + html = html.replace("\n\n", "

"); + html = html.replace("\n", "
"); + + html = html.replace("**", ""); + html = html.replace("__", ""); + + Ok(html) + } + pub fn pandoc_convert_text_to_md(markdown: String) -> Result> { let (output, errors, status) = crate::pipe( "pandoc", @@ -205,12 +226,13 @@ impl MatrixClient { pub fn send_room_message( &mut self, room_id: &str, text: &str, ) -> Result> { - let formatted_text = Self::pandoc_convert_md_to_html(text.to_string())?; + let formatted_text = Self::format_matrix_html(text.to_string())?; let content = HashMap::from([ ("msgtype", "m.text"), ("body", text), ("format", "org.matrix.custom.html"), ("formatted_body", &formatted_text), + //("m.mentions", "{}"), ]); self.send_room_event(&room_id, "m.room.message", &content) }