MyST-Parser icon indicating copy to clipboard operation
MyST-Parser copied to clipboard

Auto-generated header links ignore mis-anchor headers that start with a number

Open choldgraf opened this issue 3 years ago • 4 comments

Describe the bug

When section headers start with a number, they are labeled in a confusing way by the auto-header anchor generator. They simply have the structure id<num>.

For example, a document like this:

## v2.0

## 2.0

Will generate anchors like:

<a class="headerlink" href="#v2-0" title="Permalink to this headline">
<a class="headerlink" href="#id1" title="Permalink to this headline">

I think users would instead expect the ID of the second element to be 2-0

Since we describe the behavior as based on the GitHub sluggify, I also quickly checked and it looks like they do support IDs that start with numbers (that's why I'm labeling this as a bug since I think it breaks from expected behavior based on that criteria).

I think the relevant code for this might be here:

https://github.com/executablebooks/mdit-py-plugins/blob/master/mdit_py_plugins/anchors/index.py#L119-L120

originally reported in: https://github.com/jupyterhub/kubespawner/pull/575

Reproduce the bug

  1. Set myst_heading_anchors = 2

  2. Create a page like

    # Test
    ## v2.0
    
    ## 2.0
    
  3. Render it and note the heading IDs

List your environment

No response

choldgraf avatar Feb 24 '22 17:02 choldgraf

Just realized another problem this surfaces - if people have multiple numbered sections (e.g. in a changelog, 3.0, 3.1, 3.2) then adding a new section at the top (e.g, 4.0) will bump the id# of all subsequent labels, making links to them valid links, but not to the correct section.

choldgraf avatar Feb 25 '22 16:02 choldgraf

This may be similar to https://github.com/executablebooks/MyST-Parser/issues/195, whereby docutils/sphinx seems to treat such references differently. Should probably track down in the code exactly where/why it is doing this

chrisjsewell avatar Feb 25 '22 16:02 chrisjsewell

I think its treating them as "auto-targets", similar to autofootnote; the only fix may be to stick a prefix onto such slugs e.g. `if target.isdigit(): target = "-" + target" this would not be in keeping with github slug rules, but may be the only way

chrisjsewell avatar Feb 25 '22 16:02 chrisjsewell

maybe they could just be prefixed with num- or even n- if they started with a number?

choldgraf avatar Feb 25 '22 16:02 choldgraf