matrix formatting improvements - still in progress

This commit is contained in:
murmeldin 2024-12-20 09:56:09 +01:00
parent 57f211036e
commit 1f75e0cc94

View file

@ -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<String, Box<dyn Error>> {
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<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>> {
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<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([
("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)
}