Add domain to entry list
This commit is contained in:
@@ -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) }}
|
||||
}
|
||||
}
|
||||
}))
|
||||
|
||||
@@ -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
14
src/utils.rs
Normal 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
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user