Skip to content

Commit

Permalink
feat: another dump
Browse files Browse the repository at this point in the history
  • Loading branch information
johnbchron committed Mar 17, 2024
1 parent 804fbe6 commit 0de5737
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 23 deletions.
54 changes: 39 additions & 15 deletions app/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,20 @@ pub fn App() -> impl IntoView {
<div class="bg-neutral-800 min-h-screen">
<Stylesheet href="/pkg/blog.css"/>
<Style>{include_str!("../../style/iosevka_term.css")}</Style>

// preloads the fonts
<leptos_meta::Link
rel="preload" href="/fonts/Firava.woff2"
as_="font" type_="font/woff2" crossorigin="anonymous"
/>
<leptos_meta::Link rel="preload" href="/fonts/IosevkaTerm-Regular.woff2" as_="font" type_="font/woff2" crossorigin="anonymous" />
<leptos_meta::Link
rel="preload" href="/fonts/IosevkaTerm-Regular.woff2"
as_="font" type_="font/woff2" crossorigin="anonymous"
/>

// sets the document title
<Title text="Welcome to Leptos"/>
<Title text="John Lewis' Blog" />

// content for this welcome page
<Router fallback=|| {
let mut outside_errors = Errors::default();
outside_errors.insert_with_default_key(AppError::NotFound);
Expand All @@ -37,9 +41,7 @@ pub fn App() -> impl IntoView {
<div class="px-4 md:px-0 md:mx-auto md:w-[48rem] pt-4 text-neutral-100 text-lg">
// header
<div class="flex gap-2 w-full text-lg font-light">
<Link href="/">"John Lewis\' Blog"</Link>
"|"
<Link href="/posts">Posts</Link>
<StyledLink href="/">"John Lewis\' Blog"</StyledLink>
<div class="flex-1" />
<p class="items-center font-light">"Rust, Games, Musings"</p>
</div>
Expand All @@ -56,9 +58,9 @@ pub fn App() -> impl IntoView {

/// A styled hyperlink.
#[component]
fn Link(
fn StyledLink(
#[prop(into, default = String::new())] class: String,
href: &'static str,
#[prop(into)] href: String,
children: Children,
) -> impl IntoView {
view! {
Expand All @@ -78,16 +80,38 @@ fn HomePage() -> impl IntoView {
let posts_resource =
create_blocking_resource(|| (), |_| posts::get_all_posts());

view! {
let post_list_item = |p: posts::Post| {
view! {
<li>
<a href={format!("/post/{}", p.path)}>
{p.metadata.title}
</a>
" - " {p.metadata.written_on}
</li>
}
};

let post_elements = view! {
<Suspense>
{ move || posts_resource.get().map(|p| match p {
Ok(posts) => view! {
<div class="flex flex-col gap-4">
{ posts.iter().map(|p| view! { <p>{p.metadata.title.clone()}</p> }).collect_view() }
</div>
}.into_view(),
_ => view! { <p>"Loading..."</p> }.into_view()
Ok(posts) => posts.clone().into_iter().map(post_list_item).collect_view(),
_ => view! { <p>"This should never happen"</p> }.into_view()
})}
</Suspense>
};

view! {
<div class="markdown">
<h2>"Hey, John here!"</h2>
<p>
"Welcome to my blog. I write about my findings and thoughts, mostly regarding Rust, Nix, and game development. If you'd like to hire me, I'm available to hire! Contact me "
<a href="mailto:contact@jlewis.sh">"here"</a>
"."
</p>
<h3>"Recent Posts"</h3>
<ul>
{post_elements}
</ul>
</div>
}
}
21 changes: 15 additions & 6 deletions app/src/posts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Post {
pub html_content: String,
pub path: String,
pub metadata: PostMetadata,
}

Expand All @@ -29,12 +30,13 @@ pub struct PostMetadata {
}

#[cfg(feature = "ssr")]
pub fn extract_post(input: &str) -> Post {
pub fn extract_post(path: &str, input: &str) -> Post {
let matter = Matter::<YAML>::new().parse(input);
let metadata = matter.data.unwrap();

Post {
html_content: crate::markdown::markdown_to_html(&matter.content),
path: path.to_string(),
metadata: PostMetadata {
title: metadata["title"].as_string().unwrap(),
written_on: metadata["written_on"].as_string().unwrap(),
Expand All @@ -45,8 +47,6 @@ pub fn extract_post(input: &str) -> Post {

#[server]
pub async fn get_all_posts() -> Result<Vec<Post>, ServerFnError> {
// find all files in `content/posts`

let mut posts = Vec::new();

for entry in std::fs::read_dir("./content/posts").unwrap() {
Expand All @@ -60,11 +60,14 @@ pub async fn get_all_posts() -> Result<Vec<Post>, ServerFnError> {
.read_to_string(&mut input)
.expect("failed to read file");

posts.push(extract_post(&input));
posts.push(extract_post(
path.file_stem().unwrap().to_str().unwrap(),
&input,
));
}
}

Ok(posts)
Ok(posts.into_iter().filter(|p| p.metadata.public).collect())
}

#[server]
Expand All @@ -76,7 +79,13 @@ pub async fn get_post_by_path(path: String) -> Result<Post, ServerFnError> {
.read_to_string(&mut input)
.expect("failed to read file");

Ok(extract_post(&input))
let post = extract_post(&path, &input);

if post.metadata.public {
Ok(post)
} else {
Err(ServerFnError::new("Post not found"))
}
}

#[component]
Expand Down
3 changes: 1 addition & 2 deletions tailwind.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
content: [
"./app/src/**/*.rs"
"./**/*.rs"
],
theme: {
fontFamily: {
Expand All @@ -13,5 +13,4 @@ module.exports = {
},
},
},
plugins: [ require("@tailwindcss/typography") ],
}

0 comments on commit 0de5737

Please sign in to comment.