From 4a6ae3bc6cc43dd3627f02b9ecf005d0dfa6f6c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABlle=20Huisman?= Date: Wed, 23 Oct 2024 14:05:20 +0200 Subject: [PATCH] Add file replacements for Trunk plugin (#15) --- book/src/trunk.md | 5 +++++ packages/mdbook-trunk/src/config.rs | 7 +++++++ packages/mdbook-trunk/src/trunk.rs | 8 +++++++- 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/book/src/trunk.md b/book/src/trunk.md index 4b39545..8463a01 100644 --- a/book/src/trunk.md +++ b/book/src/trunk.md @@ -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 diff --git a/packages/mdbook-trunk/src/config.rs b/packages/mdbook-trunk/src/config.rs index d86008d..a5c5e92 100644 --- a/packages/mdbook-trunk/src/config.rs +++ b/packages/mdbook-trunk/src/config.rs @@ -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, pub files: Option>, pub show_files: Option, + pub file_replacements: Option>, pub url_query: Option, pub url_fragment: Option, pub attributes: Option>, diff --git a/packages/mdbook-trunk/src/trunk.rs b/packages/mdbook-trunk/src/trunk.rs index a268c3b..fb0cc6d 100644 --- a/packages/mdbook-trunk/src/trunk.rs +++ b/packages/mdbook-trunk/src/trunk.rs @@ -75,7 +75,13 @@ fn files(workspace: &Workspace, config: &Config) -> Result { ); 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!( "",