Skip to content

Commit e091d4c

Browse files
authored
Merge pull request #2 from z9fr/feature/develop/htmx-support
feat: added hidden email and fix issue with result cache
2 parents 20d9c15 + a04a471 commit e091d4c

File tree

4 files changed

+39
-11
lines changed

4 files changed

+39
-11
lines changed

src/handlers/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ pub async fn contact(Extension(state): Extension<Arc<State>>, headers: HeaderMap
9595
crate::tmpl::contact(&cfg.contact_links, is_htmx_request(headers))
9696
}
9797

98+
#[instrument]
99+
pub async fn email_address() -> Markup {
100+
HIT_COUNTER.with_label_values(&["email_view"]).inc();
101+
crate::tmpl::email_address()
102+
}
103+
98104
#[instrument(skip(headers))]
99105
pub async fn stack(headers: HeaderMap) -> Markup {
100106
HIT_COUNTER.with_label_values(&["stack"]).inc();

src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use axum::{
33
extract::Extension,
44
http::header::{self, CONTENT_TYPE},
55
response::Response,
6-
routing::get,
76
routing::get_service,
7+
routing::{get, post},
88
Router,
99
};
1010
use color_eyre::eyre::Result;
@@ -71,6 +71,7 @@ pub async fn run_server() -> Result<()> {
7171
.route("/metrics", get(metrics))
7272
.route("/stack", get(handlers::stack))
7373
.route("/contact", get(handlers::contact))
74+
.route("/email", post(handlers::email_address))
7475
// blog
7576
.route("/blog", get(handlers::blog::index))
7677
.route("/blog/:name", get(handlers::blog::post_view))

src/tmpl/blog.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ fn post_metadata(post: &Post, author: &Author, domain: &str) -> Markup {
4040

4141
pub fn post_index(posts: &[Post], title: &str, show_extra: bool, is_partial: bool) -> Markup {
4242
let today = Utc::now().date_naive();
43+
44+
fn post_url(post: &Post) -> String {
45+
if let Some(redirect_to) = &post.front_matter.redirect_to {
46+
redirect_to.clone()
47+
} else {
48+
"/".to_string() + &post.link
49+
}
50+
}
51+
4352
let markup = html! {
4453
.content {
4554
h1 { (title) }
@@ -54,12 +63,12 @@ pub fn post_index(posts: &[Post], title: &str, show_extra: bool, is_partial: boo
5463
}
5564
}
5665
p {
57-
ul {
66+
ul hx-boost="true" hx-swap="innerHTML" hx-target=".snowframe" hx-include="[name='bustCache']"{
5867
@for post in posts.iter().filter(|p| today.num_days_from_ce() >= p.date.num_days_from_ce()) {
5968
li {
6069
(post.detri())
6170
" - "
62-
a href={ @if post.front_matter.redirect_to.as_ref().is_some() {(post.front_matter.redirect_to.as_ref().unwrap())} @else {"/" (post.link)}} { (post.front_matter.title) }
71+
a href={(post_url(post))} hx-push-url={(post_url(post))} { (post.front_matter.title) }
6372
}
6473
}
6574
}

src/tmpl/mod.rs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,16 @@ pub fn base(title: Option<&str>, styles: Option<&str>, content: Markup) -> Marku
135135
div.indeterminate style="background-color: #ff2e88;"{}
136136
}
137137

138+
input name="bustCache" value={(*CACHEBUSTER)} type="hidden" {}
139+
138140
body.snow.hack.dark-grey hx-ext="preload" hx-indicator=".progress" {
139141
.container {
140142
br;
141143

142144
header {
143145
nav {
144-
div hx-boost="true" hx-push-url="true" hx-swap="innerHTML" hx-target=".snowframe" {
145-
a.logo href="/" { "> z9fr@blog:~$" }
146+
div hx-boost="true" hx-swap="innerHTML" hx-target=".snowframe" hx-include="[name='bustCache']" {
147+
a.logo href="/" hx-push-url="/" { "> z9fr@blog:~$" }
146148
}
147149
}
148150
}
@@ -155,15 +157,15 @@ pub fn base(title: Option<&str>, styles: Option<&str>, content: Markup) -> Marku
155157
}
156158
hr;
157159
footer {
158-
div hx-boost="true" hx-push-url="true" hx-swap="innerHTML" hx-target=".snowframe" {
160+
div hx-boost="true" hx-include="[name='bustCache']" hx-swap="innerHTML" hx-target=".snowframe" {
159161
nav {
160-
a href="/" preload{ "Home" }
162+
a href="/" hx-push-url="/" { "Home" }
161163
" - "
162-
a href="/blog" preload{ "Blog" }
164+
a href="/blog" hx-push-url="/blog" { "Blog" }
163165
" - "
164-
a href="/contact" preload { "Contact" }
166+
a href="/contact" hx-push-url="/contact" { "Contact" }
165167
" - "
166-
a href="/stack" preload{ "Uses" }
168+
a href="/stack" hx-push-url="/stack" { "Uses" }
167169
}
168170
}
169171

@@ -179,6 +181,12 @@ pub fn base(title: Option<&str>, styles: Option<&str>, content: Markup) -> Marku
179181
}
180182
}
181183

184+
pub fn email_address() -> Markup {
185+
return html!(
186+
a href={"mailto:z9fr@protonmail.com"} {"z9fr@protonmail.com"}
187+
);
188+
}
189+
182190
pub fn contact(links: &Vec<Link>, is_partial: bool) -> Markup {
183191
let markup = html! {
184192
h1 {"Contact Information"}
@@ -189,7 +197,11 @@ pub fn contact(links: &Vec<Link>, is_partial: bool) -> Markup {
189197
.grid {
190198
.cell."-6of12" {
191199
h3 {"Email"}
192-
a href={"mailto:z9fr@protonmail.com"} {"z9fr@protonmail.com"}
200+
201+
button."btn btn-default btn-ghost" hx-indicator="#spinner" hx-post="/email" hx-swap="outerHTML" {
202+
"View email address" span.loading id="spinner" style="display:none;"{}
203+
};
204+
193205
br;
194206
br;
195207

0 commit comments

Comments
 (0)