diff --git a/Cargo.lock b/Cargo.lock index 2382d2b..2c18ba8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -384,6 +384,7 @@ dependencies = [ "tracing", "tracing-appender", "tracing-subscriber", + "url", "validator", ] @@ -763,9 +764,9 @@ checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" [[package]] name = "form_urlencoded" -version = "1.1.0" +version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a9c384f161156f5260c24a097c56119f9be8c798586aecc13afbcbe7b7e26bf8" +checksum = "a62bc1cf6f830c2ec14a513a9fb124d0a213a629668a4186f329db21fe045652" dependencies = [ "percent-encoding", ] @@ -1126,9 +1127,9 @@ dependencies = [ [[package]] name = "idna" -version = "0.3.0" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e14ddfc70884202db2244c223200c204c2bda1bc6e0998d11b5e024d657209e6" +checksum = "7d20d6b07bfbc108882d88ed8e37d39636dcc260e15e30c45e6ba089610b917c" dependencies = [ "unicode-bidi", "unicode-normalization", @@ -1662,9 +1663,9 @@ checksum = "9f746c4065a8fa3fe23974dd82f15431cc8d40779821001404d10d2e79ca7d79" [[package]] name = "percent-encoding" -version = "2.2.0" +version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "478c572c3d73181ff3c2539045f6eb99e5491218eae919370993b890cdbdd98e" +checksum = "9b2a4787296e9989611394c33f193f676704af1686e70b8f8033ab5ba9a35a94" [[package]] name = "pin-project" @@ -2754,12 +2755,12 @@ checksum = "39ec24b3121d976906ece63c9daad25b85969647682eee313cb5779fdd69e14e" [[package]] name = "url" -version = "2.3.1" +version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0d68c799ae75762b8c3fe375feb6600ef5602c883c5d21eb51c09f22b83c4643" +checksum = "50bff7831e19200a85b17131d085c25d7811bc4e186efdaf54bbd132994a88cb" dependencies = [ "form_urlencoded", - "idna 0.3.0", + "idna 0.4.0", "percent-encoding", ] diff --git a/Cargo.toml b/Cargo.toml index 7eda352..d7ee4c8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -42,4 +42,5 @@ tower-http = { version = "0.4", features = ["trace", "fs"] } tracing = "0.1" tracing-appender = "0.2" tracing-subscriber = { version = "0.3", features = ["env-filter"] } +url = "2.4" validator = { version = "0.16", features = ["derive"] } diff --git a/src/handlers/home.rs b/src/handlers/home.rs index 5495077..b023682 100644 --- a/src/handlers/home.rs +++ b/src/handlers/home.rs @@ -6,6 +6,7 @@ use sqlx::PgPool; use crate::error::Result; use crate::models::entry::{get_entries, GetEntriesOptions}; use crate::partials::layout::Layout; +use crate::utils::get_domain; pub async fn get(State(pool): State, layout: Layout) -> Result { let entries = get_entries(&pool, GetEntriesOptions::default()).await?; @@ -14,7 +15,8 @@ pub async fn get(State(pool): State, layout: Layout) -> Result @for entry in entries { @let title = entry.title.unwrap_or_else(|| "Untitled".to_string()); @let url = format!("/entry/{}", entry.id); - li { a href=(url) { (title) } } + @let domain = get_domain(&entry.url).unwrap_or_default(); + li { a href=(url) { (title) } em class="domain" { (domain) }} } } })) diff --git a/src/lib.rs b/src/lib.rs index 62d1a21..96217d9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,3 +6,4 @@ pub mod log; pub mod models; pub mod partials; pub mod state; +pub mod utils; diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..dca8b1e --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,14 @@ +use url::Url; + +pub fn get_domain(url: &str) -> Option { + Url::parse(url) + .ok() + .and_then(|url| url.host_str().map(|s| s.to_string())) + .map(|domain| { + if domain.starts_with("www.") && domain.matches('.').count() > 1 { + domain[4..].to_string() + } else { + domain + } + }) +} diff --git a/static/styles.css b/static/styles.css index d7b3226..eecb86a 100644 --- a/static/styles.css +++ b/static/styles.css @@ -37,3 +37,8 @@ ul.entries { ul.entries li { margin-bottom: 8px; } + +ul.entries li em.domain { + margin-left: 8px; + color: rgba(0, 0, 0, 0.75); +}