diff --git a/changelog/v0.39.3/docs-anchor.yaml b/changelog/v0.39.3/docs-anchor.yaml new file mode 100644 index 000000000..154bbd575 --- /dev/null +++ b/changelog/v0.39.3/docs-anchor.yaml @@ -0,0 +1,6 @@ +changelog: +- type: FIX + issueLink: https://github.com/solo-io/doctopus/issues/124 + resolvesIssue: true + description: >- + Fix a bug where headings on the same doc page have the same anchor and hence do not work. Numbers anchors for the same headings sequentially. diff --git a/pkg/code-generator/docgen/funcs/template_funcs.go b/pkg/code-generator/docgen/funcs/template_funcs.go index 847f2cc99..a111b5344 100644 --- a/pkg/code-generator/docgen/funcs/template_funcs.go +++ b/pkg/code-generator/docgen/funcs/template_funcs.go @@ -137,10 +137,20 @@ func toHeading(docsOptions *options.DocsOptions) func(format string, p *string) if docsOptions.Output == options.Hugo { return printPointer } else { + // Track anchor names to handle duplicates + anchorCounts := make(map[string]int) //--> {{ printfptr "%v" .Name }} return func(format string, p *string) string { val := printPointer(format, p) - return "" + val + "" + name := strings.ToLower(val) + anchorCounts[name]++ + var anchorName string + if anchorCounts[name] > 1 { + anchorName = fmt.Sprintf("%s-%d", name, anchorCounts[name]-1) + } else { + anchorName = name + } + return "" + val + "" } } } @@ -149,8 +159,15 @@ func toAnchorLink(docsOptions *options.DocsOptions) func(format string, p *strin if docsOptions.Output != options.Hugo { return printPointer } else { + // Track anchor names to handle duplicates + anchorCounts := make(map[string]int) return func(format string, p *string) string { - return strings.ToLower(printPointer(format, p)) + name := strings.ToLower(printPointer(format, p)) + anchorCounts[name]++ + if anchorCounts[name] > 1 { + return fmt.Sprintf("%s-%d", name, anchorCounts[name]-1) + } + return name } } }