# frozen_string_literal: true require "helper" require "rouge" class TestKramdown < JekyllUnitTest def fixture_converter(config) site = fixture_site( Utils.deep_merge_hashes( { "markdown" => "kramdown", }, config ) ) Jekyll::Cache.clear site.find_converter_instance( Jekyll::Converters::Markdown ) end context "kramdown" do setup do @config = { "kramdown" => { "smart_quotes" => "lsquo,rsquo,ldquo,rdquo", "entity_output" => "as_char", "toc_levels" => "1..6", "auto_ids" => false, "footnote_nr" => 1, "show_warnings" => true, "syntax_highlighter" => "rouge", "syntax_highlighter_opts" => { "bold_every" => 8, "css" => :class, "css_class" => "highlight", "formatter" => ::Rouge::Formatters::HTMLLegacy, "foobar" => "lipsum", }, }, } @kramdown_config_keys = @config["kramdown"].keys @syntax_highlighter_opts_config_keys = \ @config["kramdown"]["syntax_highlighter_opts"].keys @config = Jekyll.configuration(@config) @converter = fixture_converter(@config) end should "not break kramdown" do kramdown_doc = Kramdown::Document.new("# Some Header #", @config["kramdown"]) assert_equal :class, kramdown_doc.options[:syntax_highlighter_opts][:css] assert_equal "lipsum", kramdown_doc.options[:syntax_highlighter_opts][:foobar] end should "run Kramdown" do assert_equal "
(«|«)Pit(›|›)hy(»|»)
!, \ converter.convert(%("Pit'hy")).strip end end context "when a custom highlighter is chosen" do should "use the chosen highlighter if it's available" do override = { "highlighter" => nil, "kramdown" => { "syntax_highlighter" => "coderay", }, } converter = fixture_converter(Utils.deep_merge_hashes(@config, override)) result = nokogiri_fragment(converter.convert(<<~MARKDOWN)) ~~~ruby puts "Hello World" ~~~ MARKDOWN selector = "div.highlighter-coderay>div.CodeRay>div.code>pre" refute_empty result.css(selector) end should "support legacy enable_coderay... for now" do override = { "kramdown" => { "enable_coderay" => true, }, } @config.delete("highlighter") @config["kramdown"].delete("syntax_highlighter") converter = fixture_converter(Utils.deep_merge_hashes(@config, override)) result = nokogiri_fragment(converter.convert(<<~MARKDOWN)) ~~~ruby puts "Hello World" ~~~ MARKDOWN selector = "div.highlighter-coderay>div.CodeRay>div.code>pre" refute_empty result.css(selector), "pre tag should exist" end end should "move coderay to syntax_highlighter_opts" do override = { "highlighter" => nil, "kramdown" => { "syntax_highlighter" => "coderay", "coderay" => { "hello" => "world", }, }, } original = Kramdown::Document.method(:new) converter = fixture_converter( Utils.deep_merge_hashes(@config, override) ) expect(Kramdown::Document).to receive(:new) do |arg1, hash| assert_equal "world", hash["syntax_highlighter_opts"]["hello"] original.call(arg1, hash) end converter.convert("hello world") end end end