diff --git a/Cargo.lock b/Cargo.lock index 57afe21..ba10809 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -167,7 +167,7 @@ checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89" [[package]] name = "mtoc" -version = "0.2.1" +version = "0.3.0" dependencies = [ "clap", "colored", diff --git a/Cargo.toml b/Cargo.toml index 181de80..b25b2ae 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mtoc" -version = "0.2.1" +version = "0.3.0" edition = "2021" authors = ["containerscrew info@containerscrew.com"] repository = "https://github.com/containerscrew/mtoc" diff --git a/README.md b/README.md index a1b3bfd..6bcd983 100644 --- a/README.md +++ b/README.md @@ -17,13 +17,9 @@ - [Using pre-commit](#using-pre-commit) - [Example](#example) - [Local development](#local-development) -- [TODO](#todo) +- [IN PROGRESS](#in-progress) - [License](#license) - -> [!WARNING] -> v0.2.1 is working fine unless will not respect content above the TOC. I'm working on it. Visit the [TODO](#todo) section for more information. -

logo

mtoc 📄

@@ -197,12 +193,10 @@ $ pre-commit install $ pre-commit run -a ``` -# TODO +# IN PROGRESS -* When generate the TOC, respect the content above the TOC. -* Push to homebrew or other package managers. -* When the toc is inside a code block, do not generate the TOC. +[IN PROGRESS](./docs/todo.md) # License -[License](./LICENSE) +`mtoc` is distributed under the terms of the [`GNU AFFERO GENERAL PUBLIC LICENSE`](./LICENSE). diff --git a/docs/test.md b/docs/test.md index 3ef5c6e..8c04b38 100644 --- a/docs/test.md +++ b/docs/test.md @@ -1,12 +1,11 @@ +DO NOT REMOVE THIS! + **Table of Contents** *generated with [mtoc](https://github.com/containerscrew/mtoc)* - [Test 1](#test-1) - [Test 2](#test-2) - [Test 3](#test-3) -DO NOT REMOVE THIS! - - # Test 1 diff --git a/docs/todo.md b/docs/todo.md new file mode 100644 index 0000000..d6f01a5 --- /dev/null +++ b/docs/todo.md @@ -0,0 +1,7 @@ + +**Table of Contents** *generated with [mtoc](https://github.com/containerscrew/mtoc)* +- [TODO](#todo) + +# TODO + +- Push to homebrew or other package managers. diff --git a/src/delete.rs b/src/delete.rs index 89a0311..7122a34 100644 --- a/src/delete.rs +++ b/src/delete.rs @@ -1,10 +1,11 @@ // Remove any existing TOC marked by comments -pub fn remove_existing_toc(content: &str) -> String { +pub fn remove_existing_toc(content: &str) -> (String, usize) { let mut new_content = String::new(); let mut in_toc_section = false; let mut inside_code_block = false; + let mut toc_start_index = None; - for line in content.lines() { + for (i, line) in content.lines().enumerate() { if line.trim_start().starts_with("```") { inside_code_block = !inside_code_block; } @@ -12,6 +13,9 @@ pub fn remove_existing_toc(content: &str) -> String { if !inside_code_block { if line.contains("") { in_toc_section = true; + if toc_start_index.is_none() { + toc_start_index = Some(i); + } continue; } if in_toc_section && line.contains("") { @@ -25,5 +29,5 @@ pub fn remove_existing_toc(content: &str) -> String { } } - new_content + (new_content, toc_start_index.unwrap_or(0)) } diff --git a/src/main.rs b/src/main.rs index 6a33cb6..5653a14 100644 --- a/src/main.rs +++ b/src/main.rs @@ -46,10 +46,10 @@ fn main() -> io::Result<()> { let toc = generate_toc(headers); // Remove any existing TOC from the original content - let content_without_toc = remove_existing_toc(&contents); + let (content_without_toc, toc_start_index) = remove_existing_toc(&contents); // Insert the new TOC at the beginning of the content - let updated_content = insert_toc(&content_without_toc, &toc); + let updated_content = insert_toc(&content_without_toc, &toc, toc_start_index); // Write the updated content back to the file let mut file = File::create(markdown_file.as_str())?; diff --git a/src/write.rs b/src/write.rs index fbb6d03..2468623 100644 --- a/src/write.rs +++ b/src/write.rs @@ -15,15 +15,8 @@ pub fn generate_toc(headers: Vec<(usize, String)>) -> String { } // Insert the TOC at the beginning of the file content -pub fn insert_toc(original_content: &str, toc: &str) -> String { - let mut new_content = String::new(); - - // Insert the new TOC - new_content.push_str(toc); - new_content.push('\n'); - - // Add the remaining content - new_content.push_str(original_content); - - new_content +pub fn insert_toc(content: &str, toc: &str, index: usize) -> String { + let mut lines: Vec<&str> = content.lines().collect(); + lines.insert(index, toc); + lines.join("\n") }