Skip to content

Commit

Permalink
Fix mungedocs TOC generation
Browse files Browse the repository at this point in the history
Fix TOC links in the presence duplicate headers.
  • Loading branch information
tallclair committed Jul 7, 2016
1 parent 61e4ba9 commit 656ce4b
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions cmd/mungedocs/toc.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func updateTOC(filePath string, mlines mungeLines) (mungeLines, error) {

func buildTOC(mlines mungeLines) mungeLines {
var out mungeLines
bookmarks := map[string]int{}

for _, mline := range mlines {
if mline.preformatted || !mline.header {
Expand All @@ -69,6 +70,12 @@ func buildTOC(mlines mungeLines) mungeLines {
bookmark := strings.Replace(strings.ToLower(heading), " ", "-", -1)
// remove symbols (except for -) in bookmarks
bookmark = r.ReplaceAllString(bookmark, "")
// Incremental counter for duplicate bookmarks
next := bookmarks[bookmark]
bookmarks[bookmark] = next + 1
if next > 0 {
bookmark = fmt.Sprintf("%s-%d", bookmark, next)
}
tocLine := fmt.Sprintf("%s- [%s](#%s)", indent, heading, bookmark)
out = append(out, newMungeLine(tocLine))
}
Expand Down

0 comments on commit 656ce4b

Please sign in to comment.