summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKevin Gimbel <hallo@kevingimbel.com>2020-02-28 13:35:38 +0100
committerKevin Gimbel <hallo@kevingimbel.com>2020-02-28 13:35:38 +0100
commitd43175cb0dcf22633f76ef57bf56c47c4e6a2284 (patch)
tree15a08f302c01613d09aabe4c9f89457da01b95bc
parent40d3df4d2564b967e703615b1483c2cd05a30859 (diff)
fix: fix replace error
The Regex was too greedy, resulting in the replacement of all content between openening and closing comments when multiple comments were present. For now, only 1 (the first) match is replaced and the regex is non-greedy so it matches the first end tag.
-rw-r--r--src/lib.rs5
1 files changed, 3 insertions, 2 deletions
diff --git a/src/lib.rs b/src/lib.rs
index af9ac20..f691ea3 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -120,9 +120,10 @@ pub fn make_toc(
) -> Result<String, ::std::io::Error> {
let content = read_file(file_path_in)?;
let new_toc = generate_toc(content.to_owned(), min_depth, max_depth);
- let re_toc = regex::Regex::new(r"(?ms)^(<!-- BEGIN mktoc).*(END mktoc -->)").unwrap();
+ let re_toc =
+ regex::Regex::new(r"(?ms)^(<!-- BEGIN mktoc -->)(.*?)(<!-- END mktoc -->)").unwrap();
let res: String = re_toc
- .replace_all(content.as_str(), new_toc.as_str())
+ .replacen(content.as_str(), 1, new_toc.as_str())
.into_owned();
Ok(res)