Skip to content

Commit

Permalink
feat!: rename root module, do not generate docs for empty modules (#21)
Browse files Browse the repository at this point in the history
* feat: enable renaming the root module

* fix: module name instead of slug

* fix: do not generate docs for empty modules

* chore: update tests
  • Loading branch information
ltabis committed May 2, 2024
1 parent 75a94ea commit 22e4a12
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 19 deletions.
52 changes: 39 additions & 13 deletions src/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub const GLOSSARY_COLOR_INDEX: &str = "#25c2a0";
#[derive(Default)]
pub struct DocusaurusOptions {
slug: Option<String>,
module_name: Option<String>,
}

impl DocusaurusOptions {
Expand All @@ -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
Expand All @@ -48,6 +66,11 @@ impl DocusaurusOptions {
module: &Documentation,
) -> Result<std::collections::HashMap<String, String>, 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(
Expand All @@ -62,7 +85,7 @@ impl DocusaurusOptions {
.expect("partial is valid");

generate(
module,
&module,
"docusaurus-module",
self.slug.as_deref(),
&hbs_registry,
Expand Down Expand Up @@ -227,18 +250,21 @@ fn generate(
hbs_registry: &handlebars::Handlebars<'_>,
) -> Result<std::collections::HashMap<String, String>, 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)?);
Expand Down
8 changes: 2 additions & 6 deletions src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(),
Expand Down

0 comments on commit 22e4a12

Please sign in to comment.