From 22e4a129bb80ffafdc7b763c0659d55cdb4bea81 Mon Sep 17 00:00:00 2001 From: Lucas Tabis Date: Thu, 2 May 2024 12:11:32 +0200 Subject: [PATCH] feat!: rename root module, do not generate docs for empty modules (#21) * feat: enable renaming the root module * fix: module name instead of slug * fix: do not generate docs for empty modules * chore: update tests --- src/generate.rs | 52 ++++++++++++++++++++++++++++++++++++------------- src/module.rs | 8 ++------ 2 files changed, 41 insertions(+), 19 deletions(-) diff --git a/src/generate.rs b/src/generate.rs index 2f52087..ca6e354 100644 --- a/src/generate.rs +++ b/src/generate.rs @@ -17,6 +17,7 @@ pub const GLOSSARY_COLOR_INDEX: &str = "#25c2a0"; #[derive(Default)] pub struct DocusaurusOptions { slug: Option, + module_name: Option, } impl DocusaurusOptions { @@ -33,6 +34,23 @@ impl DocusaurusOptions { self } + /// When registering stuff into your engine, some items will be exported in the "global" module, a module + /// that is accessible without the need to specify it's name. For documentation sake, you can use this method + /// to rename the global module so that you can split multiple items groups into multiple global modules without + /// having the "global" slug everywhere. + /// + /// For example, if the documentation exports items under the global namespace with + /// the slug `/docs/api/` and the module renamed as `my_module`, the slug set in the document will be + /// `/docs/api/my_module` instead of `/docs/api/global`. + /// + /// By default the root `global` module name is used. + #[must_use] + pub fn rename_root_module(mut self, name: &str) -> Self { + self.module_name = Some(name.to_string()); + + self + } + /// Build MDX documentation for docusaurus from the given module documentation struct. /// /// # Return @@ -48,6 +66,11 @@ impl DocusaurusOptions { module: &Documentation, ) -> Result, handlebars::RenderError> { let mut hbs_registry = handlebars::Handlebars::new(); + let mut module = module.clone(); + + if let Some(module_name) = self.module_name { + module.name = module_name; + } hbs_registry .register_template_string( @@ -62,7 +85,7 @@ impl DocusaurusOptions { .expect("partial is valid"); generate( - module, + &module, "docusaurus-module", self.slug.as_deref(), &hbs_registry, @@ -227,18 +250,21 @@ fn generate( hbs_registry: &handlebars::Handlebars<'_>, ) -> Result, handlebars::RenderError> { let mut documentation = std::collections::HashMap::default(); - let data = json!({ - "title": module.name, - "slug": slug.map_or(format!("/{}", module.name), |slug| format!("{}/{}", slug, module.name)), - "description": module.documentation, - "namespace": module.namespace, - "items": module.items, - }); - - documentation.insert( - module.name.to_string(), - hbs_registry.render(template, &data)?, - ); + + if !module.items.is_empty() { + let data = json!({ + "title": module.name, + "slug": slug.map_or(format!("/{}", module.name), |slug| format!("{}/{}", slug, module.name)), + "description": module.documentation, + "namespace": module.namespace, + "items": module.items, + }); + + documentation.insert( + module.name.to_string(), + hbs_registry.render(template, &data)?, + ); + } for sub in &module.sub_modules { documentation.extend(generate(sub, template, slug, hbs_registry)?); diff --git a/src/module.rs b/src/module.rs index f2a749e..c427353 100644 --- a/src/module.rs +++ b/src/module.rs @@ -30,7 +30,7 @@ impl std::fmt::Display for Error { } /// Rhai module documentation parsed from a definitions exported by a rhai engine. -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct Documentation { /// Complete path to the module. pub namespace: String, @@ -212,11 +212,7 @@ mod test { let docs = crate::generate::docusaurus().generate(&docs).unwrap(); - pretty_assertions::assert_eq!( - docs.get("global") - .unwrap(), - "---\ntitle: global\nslug: /global\n---\n\nimport Tabs from '@theme/Tabs';\nimport TabItem from '@theme/TabItem';\n\n```Namespace: global```\n\n\n\n" - ); + pretty_assertions::assert_eq!(docs.get("global"), None); pretty_assertions::assert_eq!( docs.get("my_module").unwrap(),