diff --git a/cmd/mungedocs/toc.go b/cmd/mungedocs/toc.go index 291d6883585..649a954bf5a 100644 --- a/cmd/mungedocs/toc.go +++ b/cmd/mungedocs/toc.go @@ -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 { @@ -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)) }