Recognize links without scheme

This commit is contained in:
Joscha 2023-03-07 14:21:28 +01:00
parent da3d84c9d8
commit 0612d235d7
2 changed files with 13 additions and 6 deletions

View file

@ -24,6 +24,7 @@ Procedure when bumping the version number:
- Respect colon-delimited emoji when calculating nick hue - Respect colon-delimited emoji when calculating nick hue
- Display colon-delimited emoji in nicks and messages - Display colon-delimited emoji in nicks and messages
- Non-export info is now printed to stderr instead of stdout - Non-export info is now printed to stderr instead of stdout
- Recognizes links without scheme (e. g. `euphoria.io` instead of `https://euphoria.io`)
### Fixed ### Fixed
- Mentions not being stopped by `>` - Mentions not being stopped by `>`

View file

@ -27,8 +27,9 @@ const NUMBER_KEYS: [char; 10] = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0
impl LinksState { impl LinksState {
pub fn new(content: &str) -> Self { pub fn new(content: &str) -> Self {
let links = LinkFinder::new() let links = LinkFinder::new()
.url_must_have_scheme(false)
.kinds(&[LinkKind::Url])
.links(content) .links(content)
.filter(|l| *l.kind() == LinkKind::Url)
.map(|l| l.as_str().to_string()) .map(|l| l.as_str().to_string())
.collect(); .collect();
@ -76,11 +77,16 @@ impl LinksState {
fn open_link_by_id(&self, id: usize) -> EventResult { fn open_link_by_id(&self, id: usize) -> EventResult {
if let Some(link) = self.links.get(id) { if let Some(link) = self.links.get(id) {
if let Err(error) = open::that(link) { // The `http://` or `https://` schema is necessary for open::that to
return EventResult::ErrorOpeningLink { // successfully open the link in the browser.
link: link.to_string(), let link = if link.starts_with("http://") || link.starts_with("https://") {
error, link.clone()
} else {
format!("https://{link}")
}; };
if let Err(error) = open::that(&link) {
return EventResult::ErrorOpeningLink { link, error };
} }
} }
EventResult::Handled EventResult::Handled