Skip to content

Commit

Permalink
fix(www): use hostname to load assets for markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoBorai committed Dec 13, 2024
1 parent b98bf82 commit 9017c0b
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
2 changes: 1 addition & 1 deletion crates/www/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
1 change: 1 addition & 0 deletions crates/www/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod routes;
mod utils;

use leptos::{component, view, IntoView};
use leptos_meta::provide_meta_context;
Expand Down
9 changes: 8 additions & 1 deletion crates/www/src/routes/notes/slug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) => {
Expand Down
16 changes: 16 additions & 0 deletions crates/www/src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use anyhow::{bail, Context, Result};
use reqwest::Url;
use web_sys::window;

pub fn hostname() -> Result<Url> {
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")
}

0 comments on commit 9017c0b

Please sign in to comment.