From 0b0b6648e409d730071654e682977f87ba4e97e0 Mon Sep 17 00:00:00 2001 From: Kevin Gimbel Date: Mon, 23 Mar 2020 16:01:34 +0100 Subject: fix: Fix issue with regex mismatch --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/lib.rs | 8 +++++++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 62aeabd..9e5092b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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)", diff --git a/Cargo.toml b/Cargo.toml index 0201e99..57b418a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 "] edition = "2018" diff --git a/src/lib.rs b/src/lib.rs index f691ea3..9e28e3c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; } -- cgit v1.2.3