diff --git a/Cargo.lock b/Cargo.lock index f09b370..89c67a3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -295,7 +295,7 @@ dependencies = [ [[package]] name = "doctave" -version = "0.4.0" +version = "0.4.1" dependencies = [ "alphanumeric-sort", "ascii", diff --git a/docs/features/checks.md b/docs/features/checks.md index 5d16696..0e95736 100644 --- a/docs/features/checks.md +++ b/docs/features/checks.md @@ -51,4 +51,4 @@ The following links point to pages that do not exist: ### Limitations * Only interal links within a Doctave project are checked -* Anchor tags are not verified \ No newline at end of file +* Anchor tags are not verified diff --git a/src/lib.rs b/src/lib.rs index f6092b9..2364d5a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -245,10 +245,19 @@ impl Document { &self.markdown.as_html } - fn title(&self) -> &str { - self.frontmatter - .get("title") - .map(|t| t.as_ref()) - .unwrap_or_else(|| self.path.file_stem().unwrap().to_str().unwrap()) + fn title(&self) -> String { + if let Some(title) = self.frontmatter.get("title") { + return title.to_string(); + } + + capitalize(self.path.file_stem().unwrap().to_str().unwrap()) + } +} + +fn capitalize(s: &str) -> String { + let mut c = s.chars(); + match c.next() { + None => String::new(), + Some(f) => f.to_uppercase().collect::() + c.as_str(), } } diff --git a/src/navigation.rs b/src/navigation.rs index 960c767..56af8ce 100644 --- a/src/navigation.rs +++ b/src/navigation.rs @@ -243,6 +243,59 @@ mod test { ) } + #[test] + fn auto_title() { + let auto_titled_page = |path: &str, base_path: Option<&str>| -> Document { + let frontmatter = BTreeMap::new(); + + Document::new( + Path::new(path), + "Not important".to_string(), + frontmatter, + base_path.unwrap_or("/"), + ) + }; + + let config = config(None); + let root = Directory { + path: config.docs_dir().to_path_buf(), + docs: vec![ + page("README.md", "Getting Started", None), + auto_titled_page("one.md", None), + ], + dirs: vec![Directory { + path: config.docs_dir().join("child"), + docs: vec![ + page("child/README.md", "Nested Root", None), + auto_titled_page("child/three.md", None), + ], + dirs: vec![], + }], + }; + + let navigation = Navigation::new(&config); + + assert_eq!( + navigation.build_for(&root), + vec![ + Link { + path: String::from("/child"), + title: String::from("Nested Root"), + children: vec![Link { + path: String::from("/child/three"), + title: String::from("Three"), + children: vec![] + }] + }, + Link { + path: String::from("/one"), + title: String::from("One"), + children: vec![] + }, + ] + ) + } + #[test] fn sorting_alphanumerically() { let config = config(None); diff --git a/src/site_generator.rs b/src/site_generator.rs index 754cde8..25f1596 100644 --- a/src/site_generator.rs +++ b/src/site_generator.rs @@ -312,11 +312,7 @@ impl<'a, T: SiteBackend> SiteGenerator<'a, T> { for doc in &root.docs { index.add_doc( &doc.id.to_string(), - &[ - &doc.title(), - &doc.uri_path().as_str(), - doc.markdown_section(), - ], + &[&doc.title(), &doc.uri_path(), doc.markdown_section()], ); } for dir in &root.dirs {