Skip to content

The Heading Render Hook Ignores Custom Classes #2162

@boevski

Description

@boevski

Environment

  • Version of Docsy you are using: 0.11.0
  • How are you using Docsy? As a:
    • Hugo module
    • NPM module
    • Git submodule
    • Other:
  • Version of Hugo you are using (output of hugo version): 0.139.3+extended+withdeploy
  • OS:
    • Linux
    • macOS
    • Windows
    • Other:

Problem

The render hook used by Docsy to add IDs to headings and to auto-link them ignores any custom classes that can be added using Markdown Attributes, breaking a core Hugo feature this way. In contrast, it adds custom IDs just fine.

The render hook was seemingly introduced with this PR.

The bug can be fixed as follows:

<h{{ .Level }} id="{{- .Anchor | safeURL -}}">
  {{- .Text | safeHTML -}}
  {{ template "_default/_markup/_td-heading-self-link.html" . -}}
</h{{ .Level }}>

{{- define "_default/_markup/_td-heading-self-link.html" -}}
<a class="td-heading-self-link" href="#{{ .Anchor | safeURL }}" aria-label="Heading self-link"></a>
{{- end -}}
  • Improved code:
<h{{ .Level }} id="{{- .Anchor | safeURL -}}" {{- with .Attributes.class }} class="{{ . }}" {{- end }}>
  {{- .Text | safeHTML -}}
  {{ template "_default/_markup/_td-heading-self-link.html" . -}}
</h{{ .Level }}>

{{- define "_default/_markup/_td-heading-self-link.html" -}}
<a class="td-heading-self-link" href="#{{ .Anchor | safeURL }}" aria-label="Heading self-link"></a>
{{- end -}}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions