Skip to content

Commit

Permalink
used PathBuf for more robust path handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Seif-Mamdouh committed Aug 2, 2024
1 parent 94e328b commit 2379087
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions server/src/http_server/pages/blog/md/html.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::unreachable;
use std::path::PathBuf;

use markdown::mdast::{
BlockQuote, Break, Code, Delete, Emphasis, Heading, Html, Image, InlineCode, Link, List,
Expand Down Expand Up @@ -300,18 +301,17 @@ impl IntoHtml for Image {
let article_path = context
.current_article_path
.as_deref()
.unwrap_or("unknown")
.split('/')
.next()
.unwrap_or("unknown")
.replace(' ', "-"); // Replace spaces with hyphens

let image_url = format!(
"{}/posts/{}/{}",
config.base_url.trim_end_matches('/'),
article_path,
self.url.trim_start_matches("../")
);
.unwrap_or("unknown");

let base_url = PathBuf::from(config.base_url.trim_end_matches('/'));
let image_path = PathBuf::from(self.url.trim_start_matches("./"));

let image_url = base_url
.join("posts")
.join(article_path)
.join(image_path)
.to_string_lossy()
.into_owned();

Ok(html! {
img src=(image_url) alt=(self.alt) title=[self.title] class="px-8 my-8" loading="lazy" {}
Expand Down

0 comments on commit 2379087

Please sign in to comment.