diff --git a/.ci.gemfile b/.ci.gemfile index 83f4e2b..2754b4e 100644 --- a/.ci.gemfile +++ b/.ci.gemfile @@ -74,7 +74,8 @@ platform :ruby do gem 'rinku' # dependency for wikicloth for handling links gem 'RedCloth' - gem 'commonmarker', '< 1' + gem 'commonmarker' + gem 'rdiscount', '>= 2.1.6' gem 'redcarpet' gem 'yajl-ruby' diff --git a/lib/tilt/commonmarker.rb b/lib/tilt/commonmarker.rb index d404744..6693aa5 100644 --- a/lib/tilt/commonmarker.rb +++ b/lib/tilt/commonmarker.rb @@ -2,53 +2,92 @@ require_relative 'template' require 'commonmarker' -aliases = { - :smartypants => :SMART -}.freeze -parse_opts = [ - :FOOTNOTES, - :LIBERAL_HTML_TAG, - :SMART, - :smartypants, - :STRIKETHROUGH_DOUBLE_TILDE, - :UNSAFE, - :VALIDATE_UTF8, -].freeze -render_opts = [ - :FOOTNOTES, - :FULL_INFO_STRING, - :GITHUB_PRE_LANG, - :HARDBREAKS, - :NOBREAKS, - :SAFE, # Removed in v0.18.0 (2018-10-17) - :SOURCEPOS, - :TABLE_PREFER_STYLE_ATTRIBUTES, - :UNSAFE, -].freeze -exts = [ - :autolink, - :strikethrough, - :table, - :tagfilter, - :tasklist, -].freeze +if defined?(::Commonmarker) + aliases = { + :smartypants => :smart + }.freeze + parse_opts = [ + :smart, + :default_info_string, + ].freeze + render_opts = [ + :hardbreaks, + :github_pre_lang, + :width, + :unsafe, + :escape, + :sourcepos, + ].freeze + exts = [ + :strikethrough, + :tagfilter, + :table, + :autolink, + :tasklist, + :superscript, + :header_ids, + :footnotes, + :description_lists, + :front_matter_delimiter, + :shortcodes, + ].freeze + Tilt::CommonMarkerTemplate = Tilt::StaticTemplate.subclass do + parse_options = @options.select { |key, _| parse_opts.include?(key.downcase) }.transform_keys(&:downcase) + parse_options.merge!(@options.select { |key, _| aliases.has_key?(key) }.transform_keys { |key| aliases[key] }) + render_options = @options.select { |key, _| render_opts.include?(key.downcase) }.transform_keys(&:downcase) + extensions = @options.select { |key, _| exts.include?(key) }.transform_keys(&:downcase) -Tilt::CommonMarkerTemplate = Tilt::StaticTemplate.subclass do - extensions = exts.select do |extension| - @options[extension] + Commonmarker.to_html(@data, options: { parse: parse_options, render: render_options, extension: extensions }) end +else + aliases = { + :smartypants => :SMART + }.freeze + parse_opts = [ + :FOOTNOTES, + :LIBERAL_HTML_TAG, + :SMART, + :smartypants, + :STRIKETHROUGH_DOUBLE_TILDE, + :UNSAFE, + :VALIDATE_UTF8, + ].freeze + render_opts = [ + :FOOTNOTES, + :FULL_INFO_STRING, + :GITHUB_PRE_LANG, + :HARDBREAKS, + :NOBREAKS, + :SAFE, # Removed in v0.18.0 (2018-10-17) + :SOURCEPOS, + :TABLE_PREFER_STYLE_ATTRIBUTES, + :UNSAFE, + ].freeze + exts = [ + :autolink, + :strikethrough, + :table, + :tagfilter, + :tasklist, + ].freeze - parse_options, render_options = [parse_opts, render_opts].map do |opts| - opts = opts.select do |option| - @options[option] - end.map! do |option| - aliases[option] || option + Tilt::CommonMarkerTemplate = Tilt::StaticTemplate.subclass do + extensions = exts.select do |extension| + @options[extension] end - opts = :DEFAULT unless opts.any? - opts - end + parse_options, render_options = [parse_opts, render_opts].map do |opts| + opts = opts.select do |option| + @options[option] + end.map! do |option| + aliases[option] || option + end + + opts = :DEFAULT unless opts.any? + opts + end - CommonMarker.render_doc(@data, parse_options, extensions).to_html(render_options, extensions) + CommonMarker.render_doc(@data, parse_options, extensions).to_html(render_options, extensions) + end end diff --git a/test/tilt_commonmarkertemplate_test.rb b/test/tilt_commonmarkertemplate_test.rb index c381e81..ba1c86b 100644 --- a/test/tilt_commonmarkertemplate_test.rb +++ b/test/tilt_commonmarkertemplate_test.rb @@ -44,4 +44,46 @@ it "sets allows_script metadata set to false" do assert_equal false, Tilt::CommonMarkerTemplate.new { |t| "# Hello World!" }.metadata[:allows_script] end + + if defined?(::Commonmarker) + it "render unsafe HTML with pre version's option name" do + template = Tilt::CommonMarkerTemplate.new(UNSAFE: true) do |_t| + < +
TL;DR
+

This is an unsafe HTML block

+ + +And then some **other** Markdown +MARKDOWN + end + + expected = < +
TL;DR
+

This is an unsafe HTML block

+ +

And then some other Markdown

+EXPECTED_HTML + + assert_match(expected, template.render) + end + + it "smartypants when :smartypants is set (pre version's option name)" do + template = Tilt::CommonMarkerTemplate.new(:smartypants => true) do |t| + "OKAY -- 'Smarty Pants'" + end + assert_match('

OKAY – ‘Smarty Pants’

', template.render) + end + + it "render markdown with custom prefixed-header id" do + template = Tilt::CommonMarkerTemplate.new(header_ids: "prefix-") do |t| + "# Foo" + end + expected = <Foo +HTML + assert_match(expected, template.render) + end + end end