From 2dc33d578c6f0e6607ba3057f84234e312a9b169 Mon Sep 17 00:00:00 2001 From: Shuanglei Tao Date: Tue, 6 Sep 2016 14:15:36 +0800 Subject: [PATCH] GFM: be more clever on parsing fenced codeblock --- lib/kramdown/parser/gfm.rb | 4 ++-- test/testcases_gfm/codeblock_fenced.html | 20 ++++++++++++++++++++ test/testcases_gfm/codeblock_fenced.options | 1 + test/testcases_gfm/codeblock_fenced.text | 21 +++++++++++++++++++++ 4 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 test/testcases_gfm/codeblock_fenced.html create mode 100644 test/testcases_gfm/codeblock_fenced.options create mode 100644 test/testcases_gfm/codeblock_fenced.text diff --git a/lib/kramdown/parser/gfm.rb b/lib/kramdown/parser/gfm.rb index 743c5ddd..1b0ab9f1 100644 --- a/lib/kramdown/parser/gfm.rb +++ b/lib/kramdown/parser/gfm.rb @@ -85,8 +85,8 @@ def parse_atx_header_gfm_quirk true end - FENCED_CODEBLOCK_START = /^[~`]{3,}/ - FENCED_CODEBLOCK_MATCH = /^(([~`]){3,})\s*?((\S+?)(?:\?\S*)?)?\s*?\n(.*?)^\1\2*\s*?\n/m + FENCED_CODEBLOCK_START = /^[ ]{0,3}[~`]{3,}/ + FENCED_CODEBLOCK_MATCH = /^[ ]{0,3}(([~`]){3,})\s*?((\S+?)(?:\?\S*)?)?\s*?\n(.*?)^[ ]{0,3}\1\2*\s*?\n/m define_parser(:codeblock_fenced_gfm, FENCED_CODEBLOCK_START, nil, 'parse_codeblock_fenced') STRIKETHROUGH_DELIM = /~~/ diff --git a/test/testcases_gfm/codeblock_fenced.html b/test/testcases_gfm/codeblock_fenced.html new file mode 100644 index 00000000..0bdefdc3 --- /dev/null +++ b/test/testcases_gfm/codeblock_fenced.html @@ -0,0 +1,20 @@ +

normal

+ +
require 'kramdown'
+
+Kramdown::Document.new(text).to_html
+
+ +

indent with tab

+ +
```ruby
+require 'kramdown'
+
+Kramdown::Document.new(text).to_html
+```
+
+ +

indent with 2 spaces

+ +
  console.log("hello");
+
diff --git a/test/testcases_gfm/codeblock_fenced.options b/test/testcases_gfm/codeblock_fenced.options new file mode 100644 index 00000000..72e9bc1e --- /dev/null +++ b/test/testcases_gfm/codeblock_fenced.options @@ -0,0 +1 @@ +:enable_coderay: false diff --git a/test/testcases_gfm/codeblock_fenced.text b/test/testcases_gfm/codeblock_fenced.text new file mode 100644 index 00000000..5e3e192c --- /dev/null +++ b/test/testcases_gfm/codeblock_fenced.text @@ -0,0 +1,21 @@ +normal + +```ruby +require 'kramdown' + +Kramdown::Document.new(text).to_html +``` + +indent with tab + + ```ruby + require 'kramdown' + + Kramdown::Document.new(text).to_html + ``` + +indent with 2 spaces + + ```js + console.log("hello"); + ```