Skip to content

Commit

Permalink
markdown is loaded statically
Browse files Browse the repository at this point in the history
  • Loading branch information
johnbchron committed Jan 10, 2024
1 parent 03762aa commit b406c5b
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 25 deletions.
36 changes: 32 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ tokio = { version = "1.33.0", features = ["full"] }
tower = { version = "0.4.13", features = ["full"] }
tower-http = { version = "0.4", features = ["full"] }
wasm-bindgen = "=0.2.89"
pulldown-cmark = "0.9"

# See https://github.com/akesson/cargo-leptos for documentation of all the parameters.

Expand Down
1 change: 1 addition & 0 deletions app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ leptos_axum = { workspace = true, optional = true }
http.workspace = true
cfg-if.workspace = true
thiserror.workspace = true
pulldown-cmark.workspace = true

[features]
default = []
Expand Down
50 changes: 40 additions & 10 deletions app/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::io::Read;

use leptos::*;
use leptos_meta::*;
use leptos_router::*;
Expand All @@ -17,7 +19,7 @@ pub fn App() -> impl IntoView {

// sets the document title
<Title text="Welcome to Leptos"/>
<Script src="https://cdn.tailwindcss.com"/>
<Script src="https://cdn.tailwindcss.com?plugins=forms,typography"/>

// content for this welcome page
<Router fallback=|| {
Expand All @@ -27,14 +29,49 @@ pub fn App() -> impl IntoView {
}>
<main class="mx-auto max-w-xl pt-4 text-[#f5f5f5]">
<Routes>
<Route path="" view=HomePage/>
<StaticRoute path="" view=HomePage static_params=|| Box::pin(async { StaticParamsMap::default() }) />
</Routes>
</main>
</Router>
</div>
}
}

pub async fn get_markdown_content(
path: String,
) -> Result<String, ServerFnError> {
let path = format!("./content/{path}");
let mut file = std::fs::File::open(&path)?;
let mut input = String::new();
file.read_to_string(&mut input)?;

let parser = pulldown_cmark::Parser::new(&input);
let mut html_output = String::new();
pulldown_cmark::html::push_html(&mut html_output, parser);

Ok(html_output)
}

#[component]
fn Markdown(
#[prop(into)] path: String,
#[prop(into, default = String::new())] class: String,
) -> impl IntoView {
let content = create_resource(
|| (),
move |_| {
let path = path.clone();
async move { get_markdown_content(path).await }
},
);

view! {
<Suspense fallback=move || view! { <p>"Loading (Suspense Fallback)..."</p> }>
<div class=format!("prose prose-invert {class}")>{move || html::div().inner_html(content.get().map(|r| r.unwrap_or_default()).unwrap_or_default())}</div>
</Suspense>
}
}

/// A styled hyperlink.
#[component]
fn Link(
Expand All @@ -58,14 +95,7 @@ fn HomePage() -> impl IntoView {
<p class="items-center font-light">"Rust, Games, Musings"</p>
</div>
<div class="h-[1px] w-full border-b border-[#f5f5f5]/50 mb-4" />
<div>
<p>"Hi! John here. I love building backend code and I\'m writing a technologically innovative magic-RPG game. Some other character traits of interest:"</p>
<ul class="list-disc pl-6">
<li>"I've been known to re-invent the wheel periodically"</li>
<li>"I'm a pathological "<Link href="https://www.rust-lang.org/">"Rust"</Link>" evangelist."</li>
<li>"I like walking; a lot."</li>
</ul>
</div>
<Markdown path="homepage.md" />
</div>
}
}
4 changes: 4 additions & 0 deletions content/homepage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Hi! John here. I write games and backends in [Rust](https://www.rust-lang.org). Other projects include nix tooling and the occasional hardware development. Some traits of mine:
- I've been known to re-invent the wheel periodically.
- I'm a pathological [Rust](https://www.rust-lang.org) evangelist.
- I like walking; a lot.
14 changes: 5 additions & 9 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -206,19 +206,15 @@
inputsFrom = builtins.attrValues self.checks;

# Extra inputs can be added here
nativeBuildInputs = with pkgs; [
nativeBuildInputs = commonArgs.buildInputs ++ (with pkgs; [
toolchain
openssl
dive
wasm-pack
pkg-config
binaryen
dive # docker images
tailwindcss
cargo-leptos
dart-sass
flyctl
skopeo
];
skopeo # docker registries
bacon # cargo check w/ hot reload
]);
RUST_SRC_PATH = "${toolchain}/lib/rustlib/src/rust/library";
};
});
Expand Down
6 changes: 4 additions & 2 deletions server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use app::*;
use axum::{routing::post, Router};
use fileserv::file_and_error_handler;
use leptos::*;
use leptos_axum::{generate_route_list, LeptosRoutes};
use leptos_axum::{build_static_routes, generate_route_list, LeptosRoutes};

pub mod fileserv;

Expand All @@ -20,7 +20,9 @@ async fn main() {
let conf = get_configuration(None).await.unwrap();
let leptos_options = conf.leptos_options;
let addr = leptos_options.site_addr;
let routes = generate_route_list(App);
let (routes, static_data_map) =
leptos_axum::generate_route_list_with_ssg(App);
build_static_routes(&leptos_options, App, &routes, static_data_map).await;

// build our application with a route
let app = Router::new()
Expand Down

0 comments on commit b406c5b

Please sign in to comment.