mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-31 07:20:13 +00:00
Fix mungedocs TOC generation
Fix TOC links in the presence duplicate headers.
This commit is contained in:
parent
61e4ba9439
commit
656ce4b623
@ -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))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user