-
Notifications
You must be signed in to change notification settings - Fork 527
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
TOC header anchor generator doesn't replace dots #385
Comments
I spent some time looking into this today, and it's a bit more complicated than it seems. First off, char *header_anchor(struct buf *text)
{
VALUE str = rb_str_new2(bufcstr(text));
VALUE tags_regex = rb_reg_new("<\\/?[^>]*>", 10 /* length */, 0);
VALUE unsafe_char_regex = rb_reg_new("[ $&+,\\/:;=?@\"<>#%{}|\\^~\\[\\]`]+", 31, 0);
VALUE trailing_dash_regex = rb_reg_new("-\\Z", 3, 0);
VALUE heading = rb_funcall(str, rb_intern("gsub"), 2, tags_regex, rb_str_new2(""));
heading = rb_funcall(heading, rb_intern("gsub"), 2, unsafe_char_regex, rb_str_new2("-"));
heading = rb_funcall(heading, rb_intern("gsub"), 2, trailing_dash_regex, rb_str_new2(""));
heading = rb_funcall(heading, rb_intern("downcase"), 0);
return StringValueCStr(heading);
} With this code, the TOC links render just fine. This markdown: # How does "this" work? Becomes this TOS link: <ul>
<li>
<a href="#how-does-this-work">How does "this" work?</a>
</li>
</ul> The problem is in the regular header anchors. That same markdown renders as this header (with <h1 id="how-does-quot-this-quot-work">How does "this" work?</h1> The reason for this is that the |
Hello there, Thanks for your inputs on this! I agree that the TOC headers are a hot-spot that we should improve for the coming release. At least, you mentioned these different issues:
However, we'd love to rewrite this area of the code without the Ruby C API relying on regular |
In order to generate valid anchors, let's remove non-alphanumeric chars from the output string. The generated strings are along the lines of strings returned by Active Support's `#parameterize`. Fixes #385.
Ok guys this should has been resolved by the aforementioned pull request ; thanks again for reporting ! :-) |
@robin850 Oh that's great, thanks! ❤️ |
Nice work! My only comment is that maybe the tests should include a double quote in one of the strings, but I have every confidence that it works regardless. |
@timcheadle : Whoah, thanks for spotting this ! 👍 Actually no, it didn't work but this is now fixed ! |
❇️ |
This is sort of a minor aesthetic issue, but generally I feel like any heading text being processed for a TOC header should follow the kind of conversion that
ActiveSupport::Inflector#parameterize
does since it's going to end up in a URL:However this is how Redcarpet currently handles the same string:
Seems like a simple regex update inside of header_anchor would do the trick. It currently uses:
<\\/?[^>]*>
This is the regex that
parameterize
uses:/[^a-z0-9\-_]+/i
C isn't my strong suit, but I can try to provide a PR for this if it seems sane.
The text was updated successfully, but these errors were encountered: