Skip to content
This repository has been archived by the owner on Oct 17, 2024. It is now read-only.

Commit

Permalink
feat: Linkify things that look like links
Browse files Browse the repository at this point in the history
  • Loading branch information
philipcristiano committed Apr 9, 2024
1 parent 390f2b4 commit c512128
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ tracing = "0.1.40"

tower-http = { version = "0.5.2", features = ["tracing", "trace"] }
service_conventions = "0.0.14"
linkify = "0.10.0"
15 changes: 14 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use nostr_sdk::prelude::*;
use rss::{Guid, Item, ItemBuilder};

pub fn event_to_item(e: Event) -> Item {
let c = e.content.clone();
let c = linkify_content(e.content.clone());
let mut guid = Guid::default();
let event_bech32 = e.id.to_bech32().unwrap();
let event_link = format!("https://snort.social/e/{event_bech32}");
Expand All @@ -25,6 +25,19 @@ fn event_is_reply(e: &Event) -> bool {
})
}

pub fn linkify_content(content: String) -> String {
let finder = linkify::LinkFinder::new();
let links: Vec<_> = finder.links(&content).collect();
let mut new_content = content.clone();

for l in links {
let link_str = l.as_str();
let anchor = format!("<a href=\"{link_str}\">{link_str}</a>");
new_content = new_content.replace(link_str, &anchor);
}
new_content
}

pub fn filter_out_replies(v: Vec<Event>) -> Vec<Event> {
return v.into_iter().filter(|e| !event_is_reply(&e)).collect();
}
Expand Down

0 comments on commit c512128

Please # to comment.