Skip to content

Commit

Permalink
Automatically enable the fenced_code_blocks option for HTML_TOC
Browse files Browse the repository at this point in the history
Since some languages rely on the sharp to comment code, the output
may be unexpected as titles begin with a sharp in Markdown.

Refs #451.
  • Loading branch information
robin850 committed Jan 28, 2017
1 parent 4105028 commit 58cca54
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,9 @@
# Changelog

* Automatically enable the `fenced_code_blocks` option passing a
`HTML_TOC` object to the `Markdown` object's constructor since
some languages rely on the sharp to comment code.

* Allow passing `Range` objects to the `nesting_level` option to have
a higher level of customization for table of contents:

Expand Down
10 changes: 10 additions & 0 deletions ext/redcarpet/rc_markdown.c
Expand Up @@ -24,6 +24,7 @@

VALUE rb_mRedcarpet;
VALUE rb_cMarkdown;
VALUE rb_cRenderHTML_TOC;

extern VALUE rb_cRenderBase;

Expand Down Expand Up @@ -101,6 +102,15 @@ static VALUE rb_redcarpet_md__new(int argc, VALUE *argv, VALUE klass)
if (!rb_obj_is_kind_of(rb_rndr, rb_cRenderBase))
rb_raise(rb_eTypeError, "Invalid Renderer instance given");

/**
* Automatically enable the `fenced_code_blocks` option if
* given a kind of `HTML_TOC` object since many languages
* like Ruby use the sharp to comment code so these comments
* would be processed as titles.
*/
if (rb_obj_is_kind_of(rb_rndr, rb_cRenderHTML_TOC))
extensions |= MKDEXT_FENCED_CODE;

Data_Get_Struct(rb_rndr, struct rb_redcarpet_rndr, rndr);

/* Merge the current options in the @options hash */
Expand Down
2 changes: 1 addition & 1 deletion ext/redcarpet/rc_render.c
Expand Up @@ -40,10 +40,10 @@
}

extern VALUE rb_mRedcarpet;
extern VALUE rb_cRenderHTML_TOC;
VALUE rb_mRender;
VALUE rb_cRenderBase;
VALUE rb_cRenderHTML;
VALUE rb_cRenderHTML_TOC;
VALUE rb_mSmartyPants;

#define buf2str(t) ((t) ? rb_enc_str_new((const char*)(t)->data, (t)->size, opt->active_enc) : Qnil)
Expand Down
15 changes: 15 additions & 0 deletions test/html_toc_render_test.rb
Expand Up @@ -93,4 +93,19 @@ def test_inline_markup_escaping
assert_match "<strong>", output
assert_no_match %r{<strong>}, output
end

def test_ignoring_fenced_code_blocks_comments
markdown = <<-Markdown.strip_heredoc
# Hello world !
~~~ruby
# This is a comment
~~~
Markdown

output = render(markdown)

assert output.match("Hello world")
refute output.match("This is a comment")
end
end

0 comments on commit 58cca54

Please sign in to comment.