From 8424f889e8db2956b637d6ca9362070c3a137fb1 Mon Sep 17 00:00:00 2001 From: Mike Dilger Date: Mon, 2 Jan 2023 07:22:55 +1300 Subject: [PATCH] Render links as links --- Cargo.lock | 10 ++++++++++ Cargo.toml | 1 + src/ui/feed.rs | 13 ++++++++++++- 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 93dfdca7..4641fcfa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1695,6 +1695,7 @@ dependencies = [ "http", "image", "lazy_static", + "linkify", "nostr-types", "rand 0.8.5", "reqwest", @@ -2087,6 +2088,15 @@ dependencies = [ "cc", ] +[[package]] +name = "linkify" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96dd5884008358112bc66093362197c7248ece00d46624e2cf71e50029f8cff5" +dependencies = [ + "memchr", +] + [[package]] name = "lock_api" version = "0.4.9" diff --git a/Cargo.toml b/Cargo.toml index 71caab8e..7e6ce072 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,7 @@ hex = "0.4" http = "0.2" image = "0.24" lazy_static = "1.4" +linkify = "0.9" nostr-types = { git = "https://github.com/mikedilger/nostr-types" } rand = "0.8" reqwest = { version = "0.11", features = ["json"] } diff --git a/src/ui/feed.rs b/src/ui/feed.rs index ffcf29bd..9dd3d77b 100644 --- a/src/ui/feed.rs +++ b/src/ui/feed.rs @@ -7,6 +7,7 @@ use egui::{ Align, Color32, Context, Frame, Image, Label, Layout, RichText, ScrollArea, Sense, TextEdit, Ui, Vec2, }; +use linkify::{LinkFinder, LinkKind}; use nostr_types::{EventKind, Id, PublicKeyHex}; struct FeedPostParams { @@ -413,7 +414,7 @@ fn render_post_actual( } }); - ui.label(&event.content); + render_content(ui, &event.content); // Under row if !as_reply_to { @@ -454,6 +455,16 @@ fn render_post_actual( } } +fn render_content(ui: &mut Ui, content: &str) { + for span in LinkFinder::new().kinds(&[LinkKind::Url]).spans(content) { + if span.kind().is_some() { + ui.hyperlink_to(span.as_str(), span.as_str()); + } else { + ui.label(span.as_str()); + } + } +} + fn set_person_view(app: &mut GossipUi, pubkeyhex: &PublicKeyHex) { if let Some(dbperson) = GLOBALS.people.blocking_write().get(pubkeyhex) { app.person_view_name = if let Some(name) = &dbperson.name {