Add domain to entry list

This commit is contained in:
2023-06-27 14:03:52 -04:00
parent 97c4ae73f0
commit 4e41bbd6e1
6 changed files with 34 additions and 10 deletions

View File

@@ -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<PgPool>, layout: Layout) -> Result<Response> {
let entries = get_entries(&pool, GetEntriesOptions::default()).await?;
@@ -14,7 +15,8 @@ pub async fn get(State(pool): State<PgPool>, layout: Layout) -> Result<Response>
@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) }}
}
}
}))

View File

@@ -6,3 +6,4 @@ pub mod log;
pub mod models;
pub mod partials;
pub mod state;
pub mod utils;

14
src/utils.rs Normal file
View File

@@ -0,0 +1,14 @@
use url::Url;
pub fn get_domain(url: &str) -> Option<String> {
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
}
})
}