diff --git a/Cargo.lock b/Cargo.lock index d4ec44b..1a6bedf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -887,7 +887,7 @@ dependencies = [ [[package]] name = "mdbook-embedify" -version = "0.0.2" +version = "0.1.0" dependencies = [ "clap", "mdbook", diff --git a/Cargo.toml b/Cargo.toml index 08c38e4..26278da 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mdbook-embedify" -version = "0.0.2" +version = "0.1.0" edition = "2021" license = "MIT" readme = "README.md" diff --git a/README.md b/README.md index 56070ec..b75e895 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ This is a [mdbook](https://rust-lang.github.io/mdBook) preprocessor plugin that ## 1. Installation -First, you need to install mdbook-embedify to your computer.You can install it from crates.io using cargo. +First, you need to install mdbook-embedify to your computer. You can install it from crates.io using cargo. ```sh cargo install mdbook-embedify diff --git a/docs/index.html b/docs/index.html index da36d7e..eebaf3a 100644 --- a/docs/index.html +++ b/docs/index.html @@ -26,7 +26,7 @@

Mdbook Embedify

1. Installation

- First, you need to install mdbook-embedify to your computer.You can install it from crates.io using cargo. + First, you need to install mdbook-embedify to your computer. You can install it from crates.io using cargo.

cargo install mdbook-embedify
diff --git a/src/utils.rs b/src/utils.rs index 89473a2..3eb9f6a 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -35,10 +35,17 @@ pub fn render_template(app: &str, placeholders: &[(String, String)]) -> String { let mut result = template.to_string(); // replace the key with the value for (key, value) in placeholders { - // replace {key} or {key|default} with value - let pattern = format!(r"\{{{}\|?[^}}]*}}", key); - let re = Regex::new(&pattern).unwrap(); - result = re.replace_all(&result, value).to_string(); + // replace {key} with value + let re = Regex::new(&format!(r"\{{{}}}", key)).unwrap(); + let replaced_result = re.replace_all(&result, value).to_string(); + // replacement was successful + if replaced_result != result { + result = replaced_result; + } else { + //replacement does not success, try replace {key|default} with value + let re = Regex::new(&format!(r"\{{{}\|[^}}]*}}", key)).unwrap(); + result = re.replace_all(&result, value).to_string(); + } } // check if there are any placeholders left, this makes the process faster if result.contains('{') && result.contains('}') {