diff --git a/Cargo.toml b/Cargo.toml index 0c80d200..ce53a21c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,3 +15,4 @@ serde = "1.0" serde_json = "1.0" yaml-front-matter = "0.1.0" wasm-bindgen = "0.2" +web-sys = "0.3" diff --git a/crates/www/Cargo.toml b/crates/www/Cargo.toml index d2b41210..19cd63f9 100644 --- a/crates/www/Cargo.toml +++ b/crates/www/Cargo.toml @@ -23,7 +23,7 @@ serde = { workspace = true } serde_json = { workspace = true } yaml-front-matter = { workspace = true } wasm-bindgen = { workspace = true } +web-sys = { workspace = true, features = ["Window"] } [dev-dependencies] wasm-bindgen-test = "0.3" -web-sys = "0.3" diff --git a/crates/www/src/lib.rs b/crates/www/src/lib.rs index 46d4308b..8c3488c3 100644 --- a/crates/www/src/lib.rs +++ b/crates/www/src/lib.rs @@ -1,4 +1,5 @@ mod routes; +mod utils; use leptos::{component, view, IntoView}; use leptos_meta::provide_meta_context; diff --git a/crates/www/src/routes/notes/slug.rs b/crates/www/src/routes/notes/slug.rs index 3f342e2d..fb9f65c5 100644 --- a/crates/www/src/routes/notes/slug.rs +++ b/crates/www/src/routes/notes/slug.rs @@ -10,6 +10,8 @@ use serde::Deserialize; use wasm_bindgen::prelude::wasm_bindgen; use yaml_front_matter::YamlFrontMatter; +use crate::utils::hostname; + #[derive(Clone, Deserialize)] struct Metadata { title: String, @@ -39,7 +41,12 @@ pub fn Note() -> impl IntoView { return; } - let url = format!("http://127.0.0.1:8080/assets/notes/{slug}.md"); + let Ok(mut url) = hostname() else { + note_md.set(Some("Error: Failed to retrieve hostname".to_string())); + return; + }; + + url.set_path(&format!("/assets/notes/{slug}.md")); match get(url).await { Ok(response) => { diff --git a/crates/www/src/utils.rs b/crates/www/src/utils.rs new file mode 100644 index 00000000..e44d7f87 --- /dev/null +++ b/crates/www/src/utils.rs @@ -0,0 +1,16 @@ +use anyhow::{bail, Context, Result}; +use reqwest::Url; +use web_sys::window; + +pub fn hostname() -> Result { + if let Some(win) = window() { + let origin_str = win + .location() + .origin() + .map_err(|_| anyhow::anyhow!("Failed to retrieve Origin from Location"))?; + + return Url::parse(&origin_str).context("Failed to parse Origin into URL"); + } + + bail!("Failed to get Window from context") +}