Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add show files by default for Trunk plugin #13

Merged
merged 1 commit into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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