Skip to content

Commit

Permalink
feat: properly handle repeated heading text
Browse files Browse the repository at this point in the history
  • Loading branch information
jar-b committed May 26, 2021
1 parent cd5c1f3 commit 79c4ddc
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Generate a table of contents for an existing markdown document. The table of con
* [Installation](#installation)
* [Usage](#usage)
* [Library](#library)
* [Usage](#usage-2)
* [Usage](#usage-1)
<!---mdtoc end--->
## CLI

Expand Down
8 changes: 8 additions & 0 deletions testdata/repeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Title
An example document with repeated heading text to verify link generation handles this properly.

## Testing
### Testing
#### Testing
### Testing
## Testing
15 changes: 15 additions & 0 deletions testdata/repeat_toc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Title
An example document with repeated heading text to verify link generation handles this properly.

<!---mdtoc begin--->
* [Testing](#testing)
* [Testing](#testing-1)
* [Testing](#testing-2)
* [Testing](#testing-3)
* [Testing](#testing-4)
<!---mdtoc end--->
## Testing
### Testing
#### Testing
### Testing
## Testing
21 changes: 21 additions & 0 deletions toc.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ func Parse(b []byte) (*Toc, error) {
return &toc, err
}

toc.updateRepeatLinks()
return &toc, nil
}

Expand All @@ -137,3 +138,23 @@ func textToLink(s string) string {
rep := strings.NewReplacer(" ", "-", "/", "", ",", "", ".", "", "+", "", ":", "", ";", "")
return strings.ToLower(rep.Replace(s))
}

// updateRepeatLinks fixes the generated link text if the generated text is repeated
// in the same contents
func (t *Toc) updateRepeatLinks() {
lookup := make(map[string]int, len(t.Bullets))

for i, b := range t.Bullets {
// if key already exists in the lookup, the link text needs to append a `-n`,
// where `n` is the number of previous occurences. if the key does not already
// exist, add a new key and set occurences to 1.
if val, ok := lookup[b.Link]; ok {
key := b.Link // preserve the original lookup key
t.Bullets[i].Link = fmt.Sprintf("%s-%d", b.Link, val)
lookup[key]++
continue
}

lookup[b.Link] = 1
}
}
3 changes: 3 additions & 0 deletions toc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ func TestAdd(t *testing.T) {
basicToc, _ := os.ReadFile("testdata/basic_toc.md")
special, _ := os.ReadFile("testdata/special.md")
specialToc, _ := os.ReadFile("testdata/special_toc.md")
repeat, _ := os.ReadFile("testdata/repeat.md")
repeatToc, _ := os.ReadFile("testdata/repeat_toc.md")

tt := []struct {
name string
Expand All @@ -22,6 +24,7 @@ func TestAdd(t *testing.T) {
}{
{"basic", basic, false, basicToc, nil},
{"special", special, false, specialToc, nil},
{"repeat", repeat, false, repeatToc, nil},
{"existing without force", basicToc, false, nil, ErrExistingToc},
{"existing with force", basicToc, true, basicToc, nil},
}
Expand Down

0 comments on commit 79c4ddc

Please sign in to comment.