From 26b90a8a5dc1af64a19c1b3ca30ccdefbc145519 Mon Sep 17 00:00:00 2001 From: Weslei Juan Moser Pereira Date: Tue, 11 Jan 2022 18:31:58 -0300 Subject: [PATCH] feat: optional table of contents --- Cargo.lock | 2 +- docs/configuration.md | 11 +++++++++++ src/config.rs | 7 +++++++ src/site_generator.rs | 2 ++ templates/page.html | 4 +++- tests/build_cmd.rs | 24 ++++++++++++++++++++++++ 6 files changed, 48 insertions(+), 2 deletions(-) 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/configuration.md b/docs/configuration.md index 7cb83ff..6a80c53 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -128,6 +128,17 @@ navigation: children: "*" ``` + +### table of contents + +Tells Doctave to show or not the table of contents in the right side of the main content. + +This is an optional setting. + +```yaml +table_contents: true +``` + ## All commands All commands support the following option. diff --git a/src/config.rs b/src/config.rs index 6e6ba4a..f1a0dda 100644 --- a/src/config.rs +++ b/src/config.rs @@ -19,6 +19,7 @@ struct DoctaveYaml { navigation: Option>, base_path: Option, docs_dir: Option, + table_contents: Option, } impl DoctaveYaml { @@ -257,6 +258,7 @@ pub struct Config { navigation: Option>, port: u32, build_mode: BuildMode, + table_contents: bool, } impl Config { @@ -295,6 +297,7 @@ impl Config { navigation: doctave_yaml.navigation.map(|n| NavRule::from_yaml_input(n)), port: doctave_yaml.port.unwrap_or_else(|| 4001), build_mode: BuildMode::Dev, + table_contents: doctave_yaml.table_contents.unwrap_or(true), }; Ok(config) @@ -381,6 +384,10 @@ impl Config { pub fn logo(&self) -> Option<&str> { self.logo.as_deref() } + + pub fn table_contents_enabled(&self) -> bool { + self.table_contents + } } pub fn project_root() -> Option { diff --git a/src/site_generator.rs b/src/site_generator.rs index 754cde8..84f9389 100644 --- a/src/site_generator.rs +++ b/src/site_generator.rs @@ -271,6 +271,7 @@ impl<'a, T: SiteBackend> SiteGenerator<'a, T> { timestamp: &self.timestamp, page_title, head_include, + table_contents: self.config.table_contents_enabled(), }; let mut out = Vec::new(); @@ -338,4 +339,5 @@ pub struct TemplateData<'a> { pub project_title: String, pub build_mode: String, pub timestamp: &'a str, + pub table_contents: bool, } diff --git a/templates/page.html b/templates/page.html index 97ba01b..34e5768 100644 --- a/templates/page.html +++ b/templates/page.html @@ -73,6 +73,7 @@

{{{ content }}}
@@ -129,4 +131,4 @@

{{/if}} - \ No newline at end of file + diff --git a/tests/build_cmd.rs b/tests/build_cmd.rs index 118e125..928f5e8 100644 --- a/tests/build_cmd.rs +++ b/tests/build_cmd.rs @@ -244,6 +244,30 @@ integration_test!(page_nav, |area| { area.assert_contains(&index, " End"); }); +integration_test!(no_table_contents, |area| { + area.mkdir(Path::new("docs")); + area.write_file( + Path::new("doctave.yaml"), + indoc! {" + --- + title: Custom colors + table_contents: false + "} + .as_bytes(), + ); + + area.write_file( + Path::new("docs").join("README.md"), + b"# Hi\n## Foo\n### Bar", + ); + + let result = area.cmd(&["build"]); + assert_success(&result); + + let index = Path::new("site").join("index.html"); + area.refute_contains(&index, "On this page"); +}); + integration_test!(missing_directory_index, |area| { area.create_config(); area.mkdir(Path::new("docs").join("nested"));