Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ID's are not generated on header elements #186

Open
Phamiliarize opened this issue Mar 27, 2020 · 4 comments
Open

ID's are not generated on header elements #186

Phamiliarize opened this issue Mar 27, 2020 · 4 comments

Comments

@Phamiliarize
Copy link

Phamiliarize commented Mar 27, 2020

I noticed a peculiarity in that headers are not generated on H elements.

# My Title should evaluate to <h1 id="my-title">My Title</h1>

Instead it evaluates to <h1>My Title</h1>.

This makes internal links not possible, which is a rather large issue since MD is often used for documentation.

@Phamiliarize
Copy link
Author

Phamiliarize commented Mar 31, 2020

https://github.com/jch/html-pipeline/blob/master/lib/html/pipeline/toc_filter.rb

Here is a ruby example of the implementation I believe.

 text = node.text
          id = ascii_downcase(text)
          id.gsub!(PUNCTUATION_REGEXP, '') # remove punctuation
          id.tr!(' ', '-') # replace spaces with dash

          uniq = headers[id] > 0 ? "-#{headers[id]}" : ''
          headers[id] += 1
          if header_content = node.children.first
            result[:toc] << %(<li><a href="##{id}#{uniq}">#{EscapeUtils.escape_html(text)}</a></li>\n)
            header_content.add_previous_sibling(%(<a id="#{id}#{uniq}" class="anchor" href="##{id}#{uniq}" aria-hidden="true">#{anchor_icon}</a>))
          end

@RichardDale
Copy link

RichardDale commented Aug 11, 2020

For a real-world example of this issue, see:
https://pypi.org/project/norgatedata/

@andrewtavis
Copy link

andrewtavis commented Mar 17, 2021

Explicitly adding HTML links with ids after headers is one way of working around all this. The following creates a fully linked readme that will also work on other platforms (such as PyPI for the above comment):

Contents:<a id="contents"></a> [Section Header](#section-header)

# Section Header [``](#contents) <a id="section-header"></a>

See any of my projects for an example of how it functions.

@xplosionmind
Copy link

Generating header IDs is a crucial feature, in my opinion. Doing how @andrewtavis suggested is feasible as a temporary solution to adopt only when it is fundamental to have a reference to the header; nevertheless, writing that much markup only to get what can easily be automated seems very cumbersome and useless.

garazdawi added a commit to garazdawi/jekyll-anchor-headings that referenced this issue Sep 10, 2021
As commonmark does not generate ids for its headers see,

github/cmark-gfm#186

we need the possibility to generate them when creating anchor headings.
garazdawi added a commit to garazdawi/jekyll-anchor-headings that referenced this issue Sep 10, 2021
As commonmark does not generate ids for its headers see,

github/cmark-gfm#186

we need the possibility to generate them when creating anchor headings.
garazdawi added a commit to garazdawi/jekyll-anchor-headings that referenced this issue Sep 10, 2021
As commonmark does not generate ids for its headers see,

github/cmark-gfm#186

we need the possibility to generate them when creating anchor headings.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants