diff --git a/Cargo.lock b/Cargo.lock
index bb11001a..d2622b61 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2138,6 +2138,7 @@ dependencies = [
"minifier",
"notify-debouncer-mini",
"octolotl",
+ "pathdiff",
"reqwest",
"schemars",
"serde",
diff --git a/Cargo.toml b/Cargo.toml
index ca77c9d9..77854994 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -48,6 +48,7 @@ notify-debouncer-mini = "0.3.0"
toml_edit = "0.19.9"
schemars = { version = "0.8.12", features = ["indexmap1"] }
indexmap = { version = "1.9.3", features = ["serde-1"] }
+pathdiff = { version = "0.2.1", features = ["camino"] }
[dev-dependencies]
assert_cmd="2"
diff --git a/src/site/layout/header.rs b/src/site/layout/header.rs
index cfcc934e..1b52f219 100644
--- a/src/site/layout/header.rs
+++ b/src/site/layout/header.rs
@@ -53,11 +53,10 @@ fn nav(
Message::new(MessageType::Info, "Found additional pages...").print();
for (page_name, page_path) in additional_pages.iter() {
if page::source::is_markdown(page_path) {
- let file_path = page::source::get_filename(page_path);
+ let file_path = page::source::get_filename_with_dir(page_path)?;
if let Some(file_name) = file_path {
- let href =
- link::generate(path_prefix, &format!("{}/", file_name.to_string_lossy()));
+ let href = link::generate(path_prefix, &format!("{}/", file_name));
html.extend(html!(
{text!(page_name)}));
} else {
diff --git a/src/site/mod.rs b/src/site/mod.rs
index 8b7ada57..cd1fcc56 100644
--- a/src/site/mod.rs
+++ b/src/site/mod.rs
@@ -117,7 +117,8 @@ impl Site {
let mut pages = vec![];
for file_path in files.values() {
if page::source::is_markdown(file_path) {
- let additional_page = Page::new_from_file(file_path, layout_template, config)?;
+ let additional_page =
+ Page::new_from_file_with_dir(file_path, layout_template, config)?;
pages.push(additional_page)
} else {
let msg = format!(
diff --git a/src/site/page/mod.rs b/src/site/page/mod.rs
index 780c4fae..6b792b98 100644
--- a/src/site/page/mod.rs
+++ b/src/site/page/mod.rs
@@ -60,6 +60,22 @@ impl Page {
})
}
+ pub fn new_from_file_with_dir(source: &str, layout: &Layout, config: &Config) -> Result {
+ let body = Self::load_and_render_contents(source, &config.styles.syntax_theme)?;
+ let contents = layout.render(body, None);
+ // Try diffing with the execution directory in case the user has provided an absolute-ish
+ // path, in order to obtain the relative-to-dir path segment
+ let relpath = if let Some(path) = pathdiff::diff_paths(source, std::env::current_dir()?) {
+ path
+ } else {
+ source.into()
+ };
+ Ok(Page {
+ contents,
+ filename: relpath.display().to_string(),
+ })
+ }
+
pub fn new_from_contents(
body: String,
filename: &str,
diff --git a/src/site/page/source.rs b/src/site/page/source.rs
index adbfc8a0..62f13571 100644
--- a/src/site/page/source.rs
+++ b/src/site/page/source.rs
@@ -1,3 +1,5 @@
+use crate::errors::{OrandaError, Result};
+use camino::Utf8PathBuf;
use std::{ffi::OsStr, path::Path};
pub fn is_markdown(file: &str) -> bool {
@@ -12,3 +14,17 @@ pub fn get_filename(file: &str) -> Option<&OsStr> {
let file_path = Path::new(file);
file_path.file_stem()
}
+
+pub fn get_filename_with_dir(file: &str) -> Result