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> { + // 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 cur_dir = Utf8PathBuf::from_path_buf(std::env::current_dir()?) + .map_err(|_| OrandaError::Other("Unable to read your current working directory.".into()))?; + let path = if let Some(path) = pathdiff::diff_utf8_paths(file, cur_dir) { + path + } else { + Utf8PathBuf::from(file) + }; + + Ok(Some(path.with_extension(""))) +} diff --git a/tests/build/fixtures/oranda_config.rs b/tests/build/fixtures/oranda_config.rs index 844e732f..671fe7bf 100644 --- a/tests/build/fixtures/oranda_config.rs +++ b/tests/build/fixtures/oranda_config.rs @@ -8,10 +8,8 @@ use oranda::site::javascript::analytics::Plausible; pub fn no_artifacts(temp_dir: String) -> Config { let mut additional_pages = IndexMap::new(); - additional_pages.insert( - "Another Page".to_string(), - "https://raw.githubusercontent.com/axodotdev/oranda/main/README.md".to_string(), - ); + additional_pages.insert("Another Page".to_string(), "docs/src/cli.md".to_string()); + additional_pages.insert("A New Page".to_string(), "SECURITY.md".to_string()); Config { project: ProjectConfig { description: Some(String::from("you axolotl questions")), diff --git a/tests/build/mod.rs b/tests/build/mod.rs index 5b611de9..ec6471f2 100644 --- a/tests/build/mod.rs +++ b/tests/build/mod.rs @@ -107,7 +107,7 @@ fn creates_nav() { let layout = Layout::new(&config).unwrap(); let page = page::index(&config, &layout); eprintln!("{}", page.contents); - assert!(page.contents.contains(r#""#)); + assert!(page.contents.contains(r#""#)); } #[test]