diff options
author | Kevin Gimbel <hallo@kevingimbel.com> | 2020-03-23 16:01:34 +0100 |
---|---|---|
committer | Kevin Gimbel <hallo@kevingimbel.com> | 2020-03-23 16:01:34 +0100 |
commit | 0b0b6648e409d730071654e682977f87ba4e97e0 (patch) | |
tree | de7d960b13251ac744912ece553c9c700fe642af | |
parent | 693db7cbffcab0460577e208f35c2ef5778f2e3b (diff) |
fix: Fix issue with regex mismatch
-rw-r--r-- | Cargo.lock | 2 | ||||
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | src/lib.rs | 8 |
3 files changed, 9 insertions, 3 deletions
@@ -255,7 +255,7 @@ dependencies = [ [[package]] name = "mktoc" -version = "2.1.0" +version = "2.2.0" dependencies = [ "criterion 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", "regex 1.3.4 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2,7 +2,7 @@ name = "mktoc" description = "Generate Table of Contents from Markdown files" license = "MIT" -version = "2.1.0" +version = "2.2.0" authors = ["Kevin Gimbel <hallo@kevingimbel.com>"] edition = "2018" @@ -67,8 +67,14 @@ pub fn generate_toc(original_content: String, min_depth: i32, max_depth: i32) -> if !code_block_found && !already_found_code_open { if line.starts_with("#") { - let caps = re.captures(line).unwrap(); + // Check if the regex matches, if it doesn't continue skip (continue) the loop. + let caps = match re.captures(line) { + Some(matched) => matched, + None => { continue; } + }; + let level: i32 = (caps.get(2).unwrap().as_str().chars().count() - 1) as i32; + if level < min_depth { continue; } |