Fix mungedocs TOC generation

Fix TOC links in the presence duplicate headers.
This commit is contained in:
Tim St. Clair 2016-07-06 12:27:51 -07:00
parent 61e4ba9439
commit 656ce4b623

View File

@ -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))
}