From 5e1a6aef5590104e093dc6126f98a194271ed739 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABlle=20Huisman?= Date: Wed, 23 Oct 2024 13:09:23 +0200 Subject: [PATCH] Add show files by default for Trunk --- book/src/trunk.md | 3 +++ book/theme/trunk.css | 7 +++++++ packages/mdbook-trunk/src/config.rs | 1 + packages/mdbook-trunk/src/theme/trunk.css | 7 +++++++ packages/mdbook-trunk/src/trunk.rs | 12 ++++++++---- 5 files changed, 26 insertions(+), 4 deletions(-) diff --git a/book/src/trunk.md b/book/src/trunk.md index aca838d..4b39545 100644 --- a/book/src/trunk.md +++ b/book/src/trunk.md @@ -72,6 +72,9 @@ features = ["button"] # Relative to the package root. files = ["src/button.rs"] +# Whether to show files by default (optional). +show_files = true + # URL query for the iframe URL (optional). # The leading question mark is optional. url_query = "?key=value" diff --git a/book/theme/trunk.css b/book/theme/trunk.css index ed75230..41dc038 100644 --- a/book/theme/trunk.css +++ b/book/theme/trunk.css @@ -15,6 +15,13 @@ display: flex; } +.mdbook-trunk-files-header { + padding: 0.5rem 1rem; + font-weight: bold; + font-size: 1.6rem; + line-height: 1.45em; +} + .mdbook-trunk-file { background-color: var(--table-alternate-bg); padding: 0.5rem 1rem; diff --git a/packages/mdbook-trunk/src/config.rs b/packages/mdbook-trunk/src/config.rs index 3f97d88..170074e 100644 --- a/packages/mdbook-trunk/src/config.rs +++ b/packages/mdbook-trunk/src/config.rs @@ -9,6 +9,7 @@ pub struct Config { pub package: String, pub features: Vec, pub files: Option>, + pub show_files: Option, pub url_query: Option, pub url_fragment: Option, pub attributes: Option>, diff --git a/packages/mdbook-trunk/src/theme/trunk.css b/packages/mdbook-trunk/src/theme/trunk.css index ed75230..41dc038 100644 --- a/packages/mdbook-trunk/src/theme/trunk.css +++ b/packages/mdbook-trunk/src/theme/trunk.css @@ -15,6 +15,13 @@ display: flex; } +.mdbook-trunk-files-header { + padding: 0.5rem 1rem; + font-weight: bold; + font-size: 1.6rem; + line-height: 1.45em; +} + .mdbook-trunk-file { background-color: var(--table-alternate-bg); padding: 0.5rem 1rem; diff --git a/packages/mdbook-trunk/src/trunk.rs b/packages/mdbook-trunk/src/trunk.rs index d9c4d45..74e4d89 100644 --- a/packages/mdbook-trunk/src/trunk.rs +++ b/packages/mdbook-trunk/src/trunk.rs @@ -66,7 +66,7 @@ fn files(workspace: &Workspace, config: &Config) -> Result { let mut content_elements: Vec = vec![]; if let Some(files) = config.files.as_ref() { - for file in files { + for (index, file) in files.iter().enumerate() { let file_path = package_root.join(file); info!( @@ -78,7 +78,10 @@ fn files(workspace: &Workspace, config: &Config) -> Result { let content = fs::read_to_string(&file_path)?; header_elements.push(format!( - "", + "", + (config.show_files.unwrap_or(false) && index == 0) + .then_some(" active") + .unwrap_or_default(), file, file_path .file_name() @@ -87,7 +90,8 @@ fn files(workspace: &Workspace, config: &Config) -> Result { )); content_elements.push(format!( - "
\n\n```{}\n{}\n```\n\n
", + "
\n\n```{}\n{}\n```\n\n
", + (!(config.show_files.unwrap_or(false) && index == 0)).then_some(" hidden").unwrap_or_default(), file, language, content @@ -96,7 +100,7 @@ fn files(workspace: &Workspace, config: &Config) -> Result { } Ok(format!( - "
\n\n{}\n
", + "
\n\n{}\n
", header_elements.join("\n"), content_elements.join("\n") ))