Skip to content

Commit

Permalink
Fix links with special characters
Browse files Browse the repository at this point in the history
  • Loading branch information
containerscrew committed Oct 9, 2024
1 parent 169ea06 commit 42e4b41
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 4 deletions.
1 change: 0 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
47 changes: 46 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mtoc"
version = "0.4.0"
version = "0.4.1"
edition = "2021"
authors = ["containerscrew [email protected]"]
repository = "https://github.com/containerscrew/mtoc"
Expand All @@ -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"
4 changes: 4 additions & 0 deletions docs/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
<!-- END OF TOC -->

# Test 1
Expand All @@ -13,6 +14,9 @@ DO NOT REMOVE THIS!

### Test 3

#### Test: testing testing


```markdown
# Hello
## World
Expand Down
10 changes: 9 additions & 1 deletion src/write.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
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();
toc.push_str("<!-- START OF TOC !DO NOT EDIT THIS CONTENT MANUALLY-->\n");
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("<!-- END OF TOC -->");
toc
}
Expand Down

0 comments on commit 42e4b41

Please sign in to comment.