Skip to content

Commit

Permalink
Add show files by default for Trunk (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielleHuisman authored Oct 23, 2024
1 parent 321e725 commit 7a05c50
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 4 deletions.
3 changes: 3 additions & 0 deletions book/src/trunk.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
7 changes: 7 additions & 0 deletions book/theme/trunk.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions packages/mdbook-trunk/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub struct Config {
pub package: String,
pub features: Vec<String>,
pub files: Option<Vec<String>>,
pub show_files: Option<bool>,
pub url_query: Option<String>,
pub url_fragment: Option<String>,
pub attributes: Option<HashMap<String, String>>,
Expand Down
7 changes: 7 additions & 0 deletions packages/mdbook-trunk/src/theme/trunk.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 8 additions & 4 deletions packages/mdbook-trunk/src/trunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ fn files(workspace: &Workspace, config: &Config) -> Result<String> {
let mut content_elements: Vec<String> = 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!(
Expand All @@ -78,7 +78,10 @@ fn files(workspace: &Workspace, config: &Config) -> Result<String> {
let content = fs::read_to_string(&file_path)?;

header_elements.push(format!(
"<button class=\"mdbook-trunk-file\" data-file=\"{}\">{}</button>",
"<button class=\"mdbook-trunk-file{}\" data-file=\"{}\">{}</button>",
(config.show_files.unwrap_or(false) && index == 0)
.then_some(" active")
.unwrap_or_default(),
file,
file_path
.file_name()
Expand All @@ -87,7 +90,8 @@ fn files(workspace: &Workspace, config: &Config) -> Result<String> {
));

content_elements.push(format!(
"<div class=\"mdbook-trunk-file-content hidden\" data-file=\"{}\">\n\n```{}\n{}\n```\n\n</div>",
"<div class=\"mdbook-trunk-file-content{}\" data-file=\"{}\">\n\n```{}\n{}\n```\n\n</div>",
(!(config.show_files.unwrap_or(false) && index == 0)).then_some(" hidden").unwrap_or_default(),
file,
language,
content
Expand All @@ -96,7 +100,7 @@ fn files(workspace: &Workspace, config: &Config) -> Result<String> {
}

Ok(format!(
"<div class=\"mdbook-trunk-files-container\">\n<nav class=\"mdbook-trunk-files\">\n{}\n</nav>\n{}\n</div>",
"<div class=\"mdbook-trunk-files-container\">\n<nav class=\"mdbook-trunk-files\">\n<span class=\"mdbook-trunk-files-header\">Source code</span>\n{}\n</nav>\n{}\n</div>",
header_elements.join("\n"),
content_elements.join("\n")
))
Expand Down

0 comments on commit 7a05c50

Please sign in to comment.