diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 0e7831d..06bf065 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -68,7 +68,6 @@ jobs: strip: true - name: Package zip - # if: startsWith(github.ref, 'refs/tags/') run: | make package PLATFORM=${{ matrix.platform.release_for }} TARGET=${{ matrix.platform.target }} diff --git a/Cargo.lock b/Cargo.lock index 945bc0b..8b7dcb4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,15 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + [[package]] name = "anstream" version = "0.6.15" @@ -165,12 +174,19 @@ version = "0.4.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89" +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + [[package]] name = "mtoc" -version = "0.4.0" +version = "0.4.1" dependencies = [ "clap", "colored", + "regex", "tempfile", "walkdir", ] @@ -199,6 +215,35 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "regex" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38200e5ee88914975b69f657f0801b6f6dccafd44fd9326302a4aaeecfacb1d8" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "368758f23274712b504848e9d5a6f010445cc8b87a7cdb4d7cbee666c1288da3" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" + [[package]] name = "rustix" version = "0.38.37" diff --git a/Cargo.toml b/Cargo.toml index f380b95..132dd75 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mtoc" -version = "0.4.0" +version = "0.4.1" edition = "2021" authors = ["containerscrew info@containerscrew.com"] repository = "https://github.com/containerscrew/mtoc" @@ -13,3 +13,4 @@ clap = { version = "4.5.20", features = ["derive"] } tempfile = "3.13.0" colored = "2.1.0" walkdir = "2.5.0" +regex = "1.11.0" diff --git a/docs/test.md b/docs/test.md index 8c04b38..f0483b2 100644 --- a/docs/test.md +++ b/docs/test.md @@ -5,6 +5,7 @@ DO NOT REMOVE THIS! - [Test 1](#test-1) - [Test 2](#test-2) - [Test 3](#test-3) + - [Test: testing testing](#test:-testing-testing) # Test 1 @@ -13,6 +14,9 @@ DO NOT REMOVE THIS! ### Test 3 +#### Test: testing testing + + ```markdown # Hello ## World diff --git a/src/write.rs b/src/write.rs index e6b11da..1048a4a 100644 --- a/src/write.rs +++ b/src/write.rs @@ -1,3 +1,5 @@ +use regex::Regex; + // Generate a table of contents from the headers pub fn generate_toc(headers: Vec<(usize, String)>) -> String { let mut toc = String::new(); @@ -5,11 +7,17 @@ pub fn generate_toc(headers: Vec<(usize, String)>) -> String { toc.push_str( "**Table of Contents** *generated with [mtoc](https://github.com/containerscrew/mtoc)*\n", ); + + // Regex to match and remove or replace unwanted characters + let re = Regex::new(r"[^a-zA-Z0-9-_ ]").unwrap(); + for (level, header) in headers { let indent = " ".repeat(level - 1); - let anchor = header.to_lowercase().replace(" ", "-"); + let sanitized_header = re.replace_all(&header.to_lowercase(), "").to_string(); + let anchor = sanitized_header.replace(" ", "-"); toc.push_str(&format!("{}- [{}](#{})\n", indent, header, anchor)); } + toc.push_str(""); toc }