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

Automatically generate anchor tags for all headings? #2690

Closed
kortina opened this issue Aug 4, 2014 · 6 comments
Closed

Automatically generate anchor tags for all headings? #2690

kortina opened this issue Aug 4, 2014 · 6 comments

Comments

@kortina
Copy link

kortina commented Aug 4, 2014

Is it possible to configure jekyll so that all headings are assigned anchor tags? Eg,

### Some Heading

would yield

<a name="some-heading"></a><h3>Some Heading</h3>

If this is not a feature, do you think this is something that jekyll users would want and be open to seeing an implementation submitted as a pull request?

@troyswanson
Copy link
Member

Some of the Markdown processors already do this. The only one I know for sure that does it is Redcarpet, but I'm sure there are other ones that do it as well.

@parkr
Copy link
Member

parkr commented Aug 4, 2014

This would have to be done in the markdown processor, or in a post-render hook that parsed the html output and inserted nodes where needed. Kramdown offers the former, nothing that I know of does the latter.

I don't think we'd want the latter for core.

The JS we use on jekyllrb.com is here http://blog.parkermoore.de/2014/08/01/header-anchor-links-in-vanilla-javascript-for-github-pages-and-jekyll/

@parkr parkr closed this as completed Aug 4, 2014
@kortina
Copy link
Author

kortina commented Aug 4, 2014

Great, thank you.

@kleinfreund
Copy link

@kortina kramdown is capable of doing so, but I disabled it (which means it’s on by default):

kramdown:
  # Enable GitHub Flavored Markdown (fenced code blocks)
  input: GFM
  # Disable auto-generated ID's for headings
  auto_ids: false

However as far as I remember correctly, this added ID’s, not name’s.

@bryanbraun
Copy link

Some markdown processors (like kramdown & redcarpet) can generate IDs but they typically don't add the anchor icon into your html. There are a few small javascript utilities that can add the anchors also, like AnchorJS (which I wrote) and the jQuery-based anchorific.js.

@reinink
Copy link

reinink commented Nov 4, 2015

Going off @parkr's suggestion, you can do something similar with some jQuery:

$('.h2,h3,h4,h5,h6').filter('[id]').each(function () {
    $(this).html('<a href="#'+$(this).attr('id')+'">' + $(this).text() + '</a>');
});

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants