Skip to content

Commit

Permalink
fix: newline spacing after toc insert
Browse files Browse the repository at this point in the history
  • Loading branch information
jar-b committed Jul 9, 2023
1 parent 4c3189d commit f182b25
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Changelog


## v0.4.2

### Fixed
- Preserve spacing after TOC on overwrite


## v0.4.1

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.4.1
0.4.2
1 change: 1 addition & 0 deletions testdata/basic_toc.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ An example document with no existing table of contents, but headings of varying
* [Nest again](#nest-again)
* [Nest a third time](#nest-a-third-time)
<!--mdtoc: end-->

## Heading 1

### A sub-bullet
Expand Down
1 change: 1 addition & 0 deletions testdata/codeblock_toc.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ An example document with code blocks containing `#` characters to verify these a
* [Heading 2](#heading-2)
* [Heading 3](#heading-3)
<!--mdtoc: end-->

## Heading 1

```
Expand Down
1 change: 1 addition & 0 deletions testdata/custom_heading_toc.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ An example document where a custom table of contents heading will be included.
* [Heading 2](#heading-2)
* [Heading 3](#heading-3)
<!--mdtoc: end-->

## Heading 1

### A sub-bullet
Expand Down
1 change: 1 addition & 0 deletions testdata/ignore_toc.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ An example document with some headings excluded by using the `<!--mdtoc: ignore-
* [Another](#another)
* [Heading 2](#heading-2)
<!--mdtoc: end-->

## Heading 1

### A sub-bullet <!--mdtoc: ignore-->
Expand Down
1 change: 1 addition & 0 deletions testdata/repeat_toc.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ An example document with repeated heading text to verify link generation handles
* [Testing](#testing-3)
* [Testing](#testing-4)
<!--mdtoc: end-->

## Testing
### Testing
#### Testing
Expand Down
1 change: 1 addition & 0 deletions testdata/special_toc.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ An example document with headings including special/uncommon characters to test
* [{With braces}](#with-braces)
* [(With parenthesis)](#with-parenthesis)
<!--mdtoc: end-->

## With/Slash

## With_Underscore
Expand Down
9 changes: 5 additions & 4 deletions toc.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,10 @@ func Insert(b []byte, cfg *Config) ([]byte, error) {
inOld = true
continue
}
// end comment, reset flag and skip
// end comment, write toc in same location and reset flag
if inOld && strings.EqualFold(line, tocEnd) {
buf.Write(toc.Bytes())
newAdded = true
inOld = false
continue
}
Expand All @@ -123,12 +125,11 @@ func Insert(b []byte, cfg *Config) ([]byte, error) {

// when the first non-title heading is encoutered, insert new toc just before it
if !newAdded && headingRegex.FindStringSubmatch(line) != nil {
buf.Write(toc.Bytes())
buf.Write(append(toc.Bytes(), []byte("\n")...))
newAdded = true
}

buf.Write(scanner.Bytes())
buf.Write([]byte("\n"))
buf.Write(append(scanner.Bytes(), []byte("\n")...))
}

if err := scanner.Err(); err != nil {
Expand Down

0 comments on commit f182b25

Please sign in to comment.