diff --git a/changelog/v0.39.4/docs-anchor2.yaml b/changelog/v0.39.4/docs-anchor2.yaml new file mode 100644 index 000000000..14a22e953 --- /dev/null +++ b/changelog/v0.39.4/docs-anchor2.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. The headings were not getting numbered from the previous PR. diff --git a/pkg/code-generator/docgen/funcs/template_funcs.go b/pkg/code-generator/docgen/funcs/template_funcs.go index a111b5344..778670cee 100644 --- a/pkg/code-generator/docgen/funcs/template_funcs.go +++ b/pkg/code-generator/docgen/funcs/template_funcs.go @@ -135,7 +135,18 @@ func TemplateFuncs(project *model.Project, docsOptions *options.DocsOptions) tem func toHeading(docsOptions *options.DocsOptions) func(format string, p *string) string { if docsOptions.Output == options.Hugo { - return printPointer + // For Hugo, we need to generate the same numbered anchors as toAnchorLink + // Track anchor names to handle duplicates + anchorCounts := make(map[string]int) + return func(format string, p *string) string { + val := printPointer(format, p) + name := strings.ToLower(val) + anchorCounts[name]++ + if anchorCounts[name] > 1 { + return fmt.Sprintf("%s {#%s-%d}", val, name, anchorCounts[name]-1) + } + return fmt.Sprintf("%s {#%s}", val, name) + } } else { // Track anchor names to handle duplicates anchorCounts := make(map[string]int)