matrix formatting improvements - still in progress
This commit is contained in:
		
							parent
							
								
									57f211036e
								
							
						
					
					
						commit
						1f75e0cc94
					
				
					 1 changed files with 24 additions and 2 deletions
				
			
		|  | @ -2,6 +2,7 @@ use std::error::Error; | ||||||
| use std::io::Read; | use std::io::Read; | ||||||
| 
 | 
 | ||||||
| use colored::Colorize; | use colored::Colorize; | ||||||
|  | use regex::Regex; | ||||||
| use serde::{Deserialize, Serialize}; | use serde::{Deserialize, Serialize}; | ||||||
| use std::collections::HashMap; | use std::collections::HashMap; | ||||||
| use std::time::{SystemTime, UNIX_EPOCH}; | use std::time::{SystemTime, UNIX_EPOCH}; | ||||||
|  | @ -177,7 +178,12 @@ impl MatrixClient { | ||||||
|     pub fn pandoc_convert_md_to_html(markdown: String) -> Result<String, Box<dyn Error>> { |     pub fn pandoc_convert_md_to_html(markdown: String) -> Result<String, Box<dyn Error>> { | ||||||
|         let (output, errors, status) = crate::pipe( |         let (output, errors, status) = crate::pipe( | ||||||
|             "pandoc", |             "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, |             markdown, | ||||||
|         )?; |         )?; | ||||||
|         if status.success() { |         if status.success() { | ||||||
|  | @ -188,6 +194,21 @@ impl MatrixClient { | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     pub fn format_matrix_html(markdown: String) -> Result<String, Box<dyn Error>> { | ||||||
|  |         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#"<a href="$1">$1</a>"#).to_string(); | ||||||
|  |         
 | ||||||
|  |         html = html.replace("\n\n", "</p><p>"); | ||||||
|  |         html = html.replace("\n", "<br>"); | ||||||
|  |         
 | ||||||
|  |         html = html.replace("**", "<strong>"); | ||||||
|  |         html = html.replace("__", "<strong>"); | ||||||
|  |         
 | ||||||
|  |         Ok(html) | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     pub fn pandoc_convert_text_to_md(markdown: String) -> Result<String, Box<dyn Error>> { |     pub fn pandoc_convert_text_to_md(markdown: String) -> Result<String, Box<dyn Error>> { | ||||||
|         let (output, errors, status) = crate::pipe( |         let (output, errors, status) = crate::pipe( | ||||||
|             "pandoc", |             "pandoc", | ||||||
|  | @ -205,12 +226,13 @@ impl MatrixClient { | ||||||
|     pub fn send_room_message( |     pub fn send_room_message( | ||||||
|         &mut self, room_id: &str, text: &str, |         &mut self, room_id: &str, text: &str, | ||||||
|     ) -> Result<Value, Box<dyn Error>> { |     ) -> Result<Value, Box<dyn Error>> { | ||||||
|         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([ |         let content = HashMap::from([ | ||||||
|             ("msgtype", "m.text"), |             ("msgtype", "m.text"), | ||||||
|             ("body", text), |             ("body", text), | ||||||
|             ("format", "org.matrix.custom.html"), |             ("format", "org.matrix.custom.html"), | ||||||
|             ("formatted_body", &formatted_text), |             ("formatted_body", &formatted_text), | ||||||
|  |             //("m.mentions", "{}"),
 | ||||||
|             ]); |             ]); | ||||||
|         self.send_room_event(&room_id, "m.room.message", &content) |         self.send_room_event(&room_id, "m.room.message", &content) | ||||||
|     } |     } | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 murmeldin
						murmeldin