Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support subdirectories for additional pages paths #469

Merged
merged 3 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 2 additions & 3 deletions src/site/layout/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
ashleygwilliams marked this conversation as resolved.
Show resolved Hide resolved

html.extend(html!(<li><a href=href>{text!(page_name)}</a></li>));
} else {
Expand Down
3 changes: 2 additions & 1 deletion src/site/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down
16 changes: 16 additions & 0 deletions src/site/page/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,22 @@ impl Page {
})
}

pub fn new_from_file_with_dir(source: &str, layout: &Layout, config: &Config) -> Result<Self> {
ashleygwilliams marked this conversation as resolved.
Show resolved Hide resolved
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,
Expand Down
16 changes: 16 additions & 0 deletions src/site/page/source.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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<Option<Utf8PathBuf>> {
// 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("")))
}
6 changes: 2 additions & 4 deletions tests/build/fixtures/oranda_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
shadows-withal marked this conversation as resolved.
Show resolved Hide resolved
additional_pages.insert("A New Page".to_string(), "SECURITY.md".to_string());
Config {
project: ProjectConfig {
description: Some(String::from("you axolotl questions")),
Expand Down
2 changes: 1 addition & 1 deletion tests/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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#"<nav class="nav"><ul><li><a href="/">Home</a></li><li><a href="/README/">Another Page</a></li></ul></nav>"#));
assert!(page.contents.contains(r#"<nav class="nav"><ul><li><a href="/">Home</a></li><li><a href="/docs/src/cli/">Another Page</a></li><li><a href="/SECURITY/">A New Page</a></li></ul></nav>"#));
}

#[test]
Expand Down