Skip to content

Commit

Permalink
Add file replacements for Trunk plugin (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielleHuisman authored Oct 23, 2024
1 parent eee9307 commit 4a6ae3b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
5 changes: 5 additions & 0 deletions book/src/trunk.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ url_fragment = "#header"
# HTML attributes for the iframe (optional).
[attributes]
allow = "fullscreen"

# Replacements to apply on files (optional).
[[file_replacements]]
find = "Some sentence"
replace = "Another sentence"
```

## Building
Expand Down
7 changes: 7 additions & 0 deletions packages/mdbook-trunk/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@ use anyhow::{anyhow, Result};
use cargo::{core::Workspace, ops::Packages};
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct FileReplacement {
pub find: String,
pub replace: String,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Config {
pub package: String,
pub features: Vec<String>,
pub files: Option<Vec<String>>,
pub show_files: Option<bool>,
pub file_replacements: Option<Vec<FileReplacement>>,
pub url_query: Option<String>,
pub url_fragment: Option<String>,
pub attributes: Option<HashMap<String, String>>,
Expand Down
8 changes: 7 additions & 1 deletion packages/mdbook-trunk/src/trunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,13 @@ fn files(workspace: &Workspace, config: &Config) -> Result<String> {
);

let language = file_path.extension().and_then(|s| s.to_str()).unwrap_or("");
let content = fs::read_to_string(&file_path)?;
let mut content = fs::read_to_string(&file_path)?;

if let Some(file_replacements) = &config.file_replacements {
for replacement in file_replacements {
content = content.replace(&replacement.find, &replacement.replace);
}
}

header_elements.push(format!(
"<button class=\"mdbook-trunk-file{}\" data-file=\"{}\">{}</button>",
Expand Down

0 comments on commit 4a6ae3b

Please sign in to comment.