Skip to content

ID of the headings are not unique #12

@0xMax42

Description

@0xMax42

The IDs that are automatically assigned to the headings, indirectly via the “slugify” function, are not unique.
This means that these headings cannot be accessed through the table of contents, but only the first heading with the same ID can be reached.

For example, if you have several headings with the name “Summary”, the same ID “summary” is generated for each of them.

To solve this problem, I have changed the code of the “getList” function in one place:

for ($index; $index < $headers->length; $index++) {
            $curr_header = $headers[$index];
            if (isset($curr_header->tagName) && $curr_header->tagName !== '') {
                // Add missing id's to the h tags
                $id = $curr_header->getAttribute('id');
                if ($id === "") {
                    $id = $this->slugify($curr_header->nodeValue) . '-' . $index; // Add the index to the ID
                    $curr_header->setAttribute('id', $id);
                }

This turns several “summary” IDs into “summary-1”, “summary-4”, “summary-n”…

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions