From 58cca54cb6efe13b24a565f69700d2a74b06c09d Mon Sep 17 00:00:00 2001 From: Robin Dupret Date: Sat, 28 Jan 2017 12:54:53 +0100 Subject: [PATCH] Automatically enable the `fenced_code_blocks` option for HTML_TOC 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. --- CHANGELOG.md | 4 ++++ ext/redcarpet/rc_markdown.c | 10 ++++++++++ ext/redcarpet/rc_render.c | 2 +- test/html_toc_render_test.rb | 15 +++++++++++++++ 4 files changed, 30 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 401a06c8..0811b004 100644 --- a/CHANGELOG.md +++ b/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: diff --git a/ext/redcarpet/rc_markdown.c b/ext/redcarpet/rc_markdown.c index 8a3df1d7..b69e951d 100644 --- a/ext/redcarpet/rc_markdown.c +++ b/ext/redcarpet/rc_markdown.c @@ -24,6 +24,7 @@ VALUE rb_mRedcarpet; VALUE rb_cMarkdown; +VALUE rb_cRenderHTML_TOC; extern VALUE rb_cRenderBase; @@ -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 */ diff --git a/ext/redcarpet/rc_render.c b/ext/redcarpet/rc_render.c index 09756472..297f7531 100644 --- a/ext/redcarpet/rc_render.c +++ b/ext/redcarpet/rc_render.c @@ -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) diff --git a/test/html_toc_render_test.rb b/test/html_toc_render_test.rb index f06b1836..6dc5ad8b 100644 --- a/test/html_toc_render_test.rb +++ b/test/html_toc_render_test.rb @@ -93,4 +93,19 @@ def test_inline_markup_escaping assert_match "<strong>", output assert_no_match %r{}, 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