summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKevin Gimbel <hallo@kevingimbel.com>2020-03-23 16:01:34 +0100
committerKevin Gimbel <hallo@kevingimbel.com>2020-03-23 16:01:34 +0100
commit0b0b6648e409d730071654e682977f87ba4e97e0 (patch)
treede7d960b13251ac744912ece553c9c700fe642af
parent693db7cbffcab0460577e208f35c2ef5778f2e3b (diff)
fix: Fix issue with regex mismatch
-rw-r--r--Cargo.lock2
-rw-r--r--Cargo.toml2
-rw-r--r--src/lib.rs8
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 <hallo@kevingimbel.com>"]
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;
}