Skip to content

Commit

Permalink
Feature: do not remove content above TOC
Browse files Browse the repository at this point in the history
  • Loading branch information
containerscrew committed Aug 29, 2024
1 parent cb66462 commit c9e2320
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 31 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mtoc"
version = "0.2.1"
version = "0.3.0"
edition = "2021"
authors = ["containerscrew [email protected]"]
repository = "https://github.com/containerscrew/mtoc"
Expand Down
14 changes: 4 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,9 @@
- [Using pre-commit](#using-pre-commit)
- [Example](#example)
- [Local development](#local-development)
- [TODO](#todo)
- [IN PROGRESS](#in-progress)
- [License](#license)
<!-- END OF TOC -->

> [!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.
<p align="center">
<img src="logo.png" alt="logo" width="250"/>
<h3 align="center">mtoc 📄</h3>
Expand Down Expand Up @@ -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).
5 changes: 2 additions & 3 deletions docs/test.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
DO NOT REMOVE THIS!

<!-- START OF TOC !DO NOT EDIT THIS CONTENT MANUALLY-->
**Table of Contents** *generated with [mtoc](https://github.com/containerscrew/mtoc)*
- [Test 1](#test-1)
- [Test 2](#test-2)
- [Test 3](#test-3)
<!-- END OF TOC -->
DO NOT REMOVE THIS!



# Test 1

Expand Down
7 changes: 7 additions & 0 deletions docs/todo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!-- START OF TOC !DO NOT EDIT THIS CONTENT MANUALLY-->
**Table of Contents** *generated with [mtoc](https://github.com/containerscrew/mtoc)*
- [TODO](#todo)
<!-- END OF TOC -->
# TODO

- Push to homebrew or other package managers.
10 changes: 7 additions & 3 deletions src/delete.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
// 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;
}

if !inside_code_block {
if line.contains("<!-- START OF TOC !DO NOT EDIT THIS CONTENT MANUALLY-->") {
in_toc_section = true;
if toc_start_index.is_none() {
toc_start_index = Some(i);
}
continue;
}
if in_toc_section && line.contains("<!-- END OF TOC -->") {
Expand All @@ -25,5 +29,5 @@ pub fn remove_existing_toc(content: &str) -> String {
}
}

new_content
(new_content, toc_start_index.unwrap_or(0))
}
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())?;
Expand Down
15 changes: 4 additions & 11 deletions src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

0 comments on commit c9e2320

Please sign in to comment.