Recognise URLs in plain text message bodies (#476)

This commit is contained in:
vaw 2025-07-23 00:05:23 +00:00 committed by GitHub
parent 34d3b844af
commit ec88f4441e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 29 additions and 6 deletions

10
Cargo.lock generated
View file

@ -2274,6 +2274,7 @@ dependencies = [
"image",
"lazy_static 1.5.0",
"libc",
"linkify",
"markup5ever_rcdom",
"matrix-sdk",
"mime",
@ -2831,6 +2832,15 @@ dependencies = [
"vcpkg",
]
[[package]]
name = "linkify"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1dfa36d52c581e9ec783a7ce2a5e0143da6237be5811a0b3153fedfdbe9f780"
dependencies = [
"memchr",
]
[[package]]
name = "linux-raw-sys"
version = "0.3.8"

View file

@ -64,6 +64,7 @@ unicode-width = "0.1.10"
url = {version = "^2.2.2", features = ["serde"]}
edit = "0.1.4"
humansize = "2.0.0"
linkify = "0.10.0"
[dependencies.comrak]
version = "0.22.0"

View file

@ -524,11 +524,11 @@ impl StyleTree {
}
pub struct TreeGenState {
link_num: u8,
pub link_num: u8,
}
impl TreeGenState {
fn next_link_char(&mut self) -> Option<char> {
pub fn next_link_char(&mut self) -> Option<char> {
let num = self.link_num;
if num < 62 {

View file

@ -72,6 +72,7 @@ mod state;
pub use self::compose::text_to_message;
use self::state::{body_cow_state, html_state};
pub use html::TreeGenState;
type ProtocolPreview<'a> = (&'a Protocol, u16, u16);

View file

@ -86,7 +86,14 @@ use crate::base::{
SendAction,
};
use crate::message::{text_to_message, Message, MessageEvent, MessageKey, MessageTimeStamp};
use crate::message::{
text_to_message,
Message,
MessageEvent,
MessageKey,
MessageTimeStamp,
TreeGenState,
};
use crate::worker::Requester;
use super::scrollback::{Scrollback, ScrollbackState};
@ -226,10 +233,14 @@ impl ChatState {
let links = if let Some(html) = &msg.html {
html.get_links()
} else if let Ok(url) = Url::parse(&msg.event.body()) {
vec![('0', url)]
} else {
vec![]
linkify::LinkFinder::new()
.links(&msg.event.body())
.filter_map(|u| Url::parse(u.as_str()).ok())
.scan(TreeGenState { link_num: 0 }, |state, u| {
state.next_link_char().map(|c| (c, u))
})
.collect()
};
if links.is_empty() {