diff --git a/CHANGELOG.md b/CHANGELOG.md index 972af39..d2988a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,12 @@ # Changelog +## v0.4.2 + +### Fixed +- Preserve spacing after TOC on overwrite + + ## v0.4.1 ### Fixed diff --git a/VERSION b/VERSION index 267577d..2b7c5ae 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.4.1 +0.4.2 diff --git a/testdata/basic_toc.md b/testdata/basic_toc.md index 3b08f45..4507ed3 100644 --- a/testdata/basic_toc.md +++ b/testdata/basic_toc.md @@ -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) + ## Heading 1 ### A sub-bullet diff --git a/testdata/codeblock_toc.md b/testdata/codeblock_toc.md index 03f37f8..b2e7c59 100644 --- a/testdata/codeblock_toc.md +++ b/testdata/codeblock_toc.md @@ -6,6 +6,7 @@ An example document with code blocks containing `#` characters to verify these a * [Heading 2](#heading-2) * [Heading 3](#heading-3) + ## Heading 1 ``` diff --git a/testdata/custom_heading_toc.md b/testdata/custom_heading_toc.md index 2fafb70..0617a95 100644 --- a/testdata/custom_heading_toc.md +++ b/testdata/custom_heading_toc.md @@ -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) + ## Heading 1 ### A sub-bullet diff --git a/testdata/ignore_toc.md b/testdata/ignore_toc.md index fae7b85..9c649e8 100644 --- a/testdata/ignore_toc.md +++ b/testdata/ignore_toc.md @@ -6,6 +6,7 @@ An example document with some headings excluded by using the ` + ## Heading 1 ### A sub-bullet diff --git a/testdata/repeat_toc.md b/testdata/repeat_toc.md index e3804de..b2adc34 100644 --- a/testdata/repeat_toc.md +++ b/testdata/repeat_toc.md @@ -8,6 +8,7 @@ An example document with repeated heading text to verify link generation handles * [Testing](#testing-3) * [Testing](#testing-4) + ## Testing ### Testing #### Testing diff --git a/testdata/special_toc.md b/testdata/special_toc.md index e719b01..7dc21a6 100644 --- a/testdata/special_toc.md +++ b/testdata/special_toc.md @@ -14,6 +14,7 @@ An example document with headings including special/uncommon characters to test * [{With braces}](#with-braces) * [(With parenthesis)](#with-parenthesis) + ## With/Slash ## With_Underscore diff --git a/toc.go b/toc.go index ec25408..afa77c3 100644 --- a/toc.go +++ b/toc.go @@ -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 } @@ -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 {