-
Notifications
You must be signed in to change notification settings - Fork 524
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
Comments
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! |
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 |
Hey, the 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 |
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! |
It's currently broken, but something like this would be great: https://github.com/brady8/redcarpet/commit/b573f148c7205b3f773fefb9379f1100cfaa1956 |
can we please re-open this now that the gem seems to be maintained again? |
Any chance of RedCarpet switching to using (or providing a flag for) semantic anchor tags, rather than "toc_0", etc.?
A.k.a.
I'm happy to do some hacking, but C is definitely not my strength...
The text was updated successfully, but these errors were encountered: