Skip to content

Commit

Permalink
Change file processor. Ignore code inside a block
Browse files Browse the repository at this point in the history
  • Loading branch information
containerscrew committed Aug 29, 2024
1 parent 518aca5 commit 3a4a2b2
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 10 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
<!-- START OF TOC !DO NOT EDIT THIS CONTENT MANUALLY-->
**Table of Contents** *generated with [mtoc](https://github.com/containerscrew/mtoc)*
- [Changelog](#changelog)
- [[unreleased]](#[unreleased])
- [[0.2.0] - 2024-08-29](#[0.2.0]---2024-08-29)
- [[0.1.0] - 2024-08-28](#[0.1.0]---2024-08-28)
<!-- END OF TOC -->
# Changelog

All notable changes to this project will be documented in this file.
Expand Down
19 changes: 19 additions & 0 deletions docs/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,27 @@
- [Test 2](#test-2)
- [Test 3](#test-3)
<!-- END OF TOC -->
DO NOT REMOVE THIS!



# Test 1

## Test 2

### Test 3

```markdown
# Hello
## World
### How are you?
```

```markdown
<!-- START OF TOC !DO NOT EDIT THIS CONTENT MANUALLY-->
**Table of Contents** *generated with [mtoc](https://github.com/containerscrew/mtoc)*
- [Hello](#hello)
- [World](#world)
- [How are you?](#how-are-you?)
<!-- END OF TOC -->
```
24 changes: 15 additions & 9 deletions src/delete.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
// Remove any existing TOC marked by comments
pub fn remove_existing_toc(content: &str) -> String {
let mut lines = content.lines();
let mut new_content = String::new();
let mut in_toc_section = false;
let mut inside_code_block = false;

// Detect and remove existing TOC section marked by comments
for line in lines.by_ref() {
if line.contains("<!-- START OF TOC !DO NOT EDIT THIS CONTENT MANUALLY-->") {
in_toc_section = true;
continue;
for line in content.lines() {
if line.trim_start().starts_with("```") {
inside_code_block = !inside_code_block;
}
if in_toc_section && line.contains("<!-- END OF TOC -->") {
in_toc_section = false;
continue;

if !inside_code_block {
if line.contains("<!-- START OF TOC !DO NOT EDIT THIS CONTENT MANUALLY-->") {
in_toc_section = true;
continue;
}
if in_toc_section && line.contains("<!-- END OF TOC -->") {
in_toc_section = false;
continue;
}
}

if !in_toc_section {
new_content.push_str(&format!("{}\n", line));
}
Expand Down
9 changes: 8 additions & 1 deletion src/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,15 @@ pub fn read_markdown_file(file_path: &str) -> io::Result<String> {
// TODO: add tests
pub fn extract_headers(contents: &str) -> Vec<(usize, String)> {
let mut headers = Vec::new();
let mut inside_code_block = false;

for line in contents.lines() {
if line.starts_with('#') {
if line.trim_start().starts_with("```") {
inside_code_block = !inside_code_block;
continue;
}

if !inside_code_block && line.starts_with('#') {
let level = line.chars().take_while(|&c| c == '#').count();
let header = line[level..].trim().to_string();
headers.push((level, header));
Expand Down

0 comments on commit 3a4a2b2

Please sign in to comment.