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

Semantic Anchors for Table of Contents #52

Closed
bouchard opened this issue Aug 31, 2011 · 6 comments
Closed

Semantic Anchors for Table of Contents #52

bouchard opened this issue Aug 31, 2011 · 6 comments

Comments

@bouchard
Copy link

Any chance of RedCarpet switching to using (or providing a flag for) semantic anchor tags, rather than "toc_0", etc.?

A.k.a.

# Header 1

# Header 2
<h1 id="header_1">Header 1</h1>
<h1 id="header_2">Header 2</h1>

I'm happy to do some hacking, but C is definitely not my strength...

@vmg
Copy link
Owner

vmg commented Sep 8, 2011

Hey Brady, semantic anchor tags are hard to get right (with the underscore conversions, and all that), so by default the HTML renderer uses increasing anchor IDs.

Luckily, starting with Redcarpet 2 (the head of the master branch, currently in beta), you can override any of the rendering callbacks to make them render whatever fits you:

class HTML_SemanticTOC < Redcarpet::Render::HTML
  def header(title, level)
    '<h#{level} id="#{title.make_semantic}">#{title}</h#{level}>'
  end
end

Hope that's helpful!

@vmg vmg closed this as completed Sep 8, 2011
@bouchard
Copy link
Author

bouchard commented Sep 9, 2011

Thanks a lot tanoku for getting back to me! That sounds like it will work perfectly for me, much appreciated. I had another look through the source, and in order to make it work with the built-in TOC renderer, it will also require if I'm not mistaken access to the internal variable #current_level so as to emulate the toc_header method in Ruby. Any chance of exposing that, or adding another callback, perhaps #header_link or #toc_header_link, so I can do the following?

class HTML_SemanticTOC < Redcarpet::Render::HTML_TOC
  def header_link(title, level)
    %[<a href="#{title.make_semantic}">#{title}</a>]
  end
end

@vmg
Copy link
Owner

vmg commented Sep 10, 2011

Hey, the current_level variable is internal to the HTML parser. If you need to track header increases, why don't just declare an instance variable to track them?

class HTML_SemanticTOC < Redcarpet::Render::HTML_TOC
  def initialize()
    @header_level = 0
  end
  def header_link(title, level)
    %[<a href="#{title.make_semantic}">#{title}</a>]
    @header_level = level # or something like that I don't remember the logic ;/
  end
end

@bouchard
Copy link
Author

Hey tanoku, looks like I didn't describe the issue properly. The toc_header method implements quite a lot of logic, including opening / closing lists based on the current level, so that, if I use the #header method you've suggested above, everything is outputted at the top level. If you take a look at the toc_header method in the html.c:530 file, you can see what I mean. I'm wondering if it would be possible to expose just one more level of the C code in Ruby, say #toc_header_link, so that I could override the tag only and leave the rest of the logic alone?

Re-implementing the entire method in Ruby is certainly doable, but (I'm assuming) would negate most of the speed advantage of having it in C in the first place. I'm not very familar with C, but I'm going to give this a go on my own and see if I can get most of the way there!

@bouchard
Copy link
Author

It's currently broken, but something like this would be great:

https://github.com/brady8/redcarpet/commit/b573f148c7205b3f773fefb9379f1100cfaa1956

@rubiii
Copy link

rubiii commented Apr 14, 2013

can we please re-open this now that the gem seems to be maintained again?
i would really love to have the header method call a new header_link method to just change to links.
unfortunately, i'm not good with c at all, so i can't help you here.

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

3 participants