Skip to content

Identical heading text results in duplicate anchor IDs #42

Description

@preethiramiah

Description

When multiple headings contain the same text, the Anchors plugin generates duplicate id attributes for those headings.

For example, the following content:

<h2>Link Examples</h2>
<h3>Hover and Focus</h3>
<p>...</p>

<h2>Image Examples</h2>
<h3>Hover and Focus</h3>
<p>...</p>

is rendered as:

<h2 id="link-examples">Link Examples</h2>
<h3 id="hover-and-focus">Hover and Focus</h3>
<p>...</p>

<h2 id="image-examples">Image Examples</h2>
<h3 id="hover-and-focus">Hover and Focus</h3>
<p>...</p>

This results in duplicate IDs within the same document, which produces invalid HTML and breaks anchor navigation, table-of-contents links, etc. that relies on unique IDs.

The plugin should ensure generated IDs are unique, similar to how it is done for entry slugs. For example by appending a numeric suffix (hover-and-focus, hover-and-focus-2, etc.) when duplicate heading text is encountered.

Steps to reproduce

  1. Create content containing two or more headings with identical text.
  2. Render the content using the Anchors plugin.
  3. Inspect the generated HTML.
  4. Observe that each identical heading receives the same id attribute.

Additional info

  • Craft version: N/A
  • PHP version: N/A
  • Database driver & version: N/A
  • Plugins & versions: Anchors

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions