From 656ce4b62355288221beb1cb18f92bdc210e0099 Mon Sep 17 00:00:00 2001 From: "Tim St. Clair" Date: Wed, 6 Jul 2016 12:27:51 -0700 Subject: [PATCH] Fix mungedocs TOC generation Fix TOC links in the presence duplicate headers. --- cmd/mungedocs/toc.go | 7 +++++++ 1 file changed, 7 insertions(+) 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)) }