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

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() {