|
| 1 | +$LOAD_PATH.unshift File.expand_path("../lib", __dir__) |
| 2 | + |
| 3 | +require_relative "test_helper" |
| 4 | +require "github/markup" |
| 5 | +require "commonmarker" |
| 6 | +require "minitest/autorun" |
| 7 | +require "nokogiri" |
| 8 | + |
| 9 | +class MarkdownTest < Minitest::Test |
| 10 | + include ConstantStubbing |
| 11 | + |
| 12 | + def test_uses_legacy_api_when_modern_interface_is_unavailable |
| 13 | + legacy = Module.new |
| 14 | + legacy.define_singleton_method(:render_html) do |content, _options, _extensions| |
| 15 | + "<p>legacy #{content}</p>\n" |
| 16 | + end |
| 17 | + |
| 18 | + with_stub_const("Commonmarker", Module.new) do |
| 19 | + with_stub_const("CommonMarker", legacy) do |
| 20 | + actual = GitHub::Markup::Markdown::MARKDOWN_GEMS.fetch("commonmarker").call("payload") |
| 21 | + assert_equal "<p>legacy payload</p>\n", actual |
| 22 | + end |
| 23 | + end |
| 24 | + end |
| 25 | + |
| 26 | + def test_renders_through_both_public_entry_points |
| 27 | + assert_equal "<p>hello</p>\n", GitHub::Markup.render("raw.md", "hello") |
| 28 | + assert_equal "<p>hello</p>\n", GitHub::Markup.render_s(GitHub::Markups::MARKUP_MARKDOWN, "hello") |
| 29 | + end |
| 30 | + |
| 31 | + def test_preserves_code_language |
| 32 | + document = Nokogiri::HTML.fragment(GitHub::Markup.render("raw.md", "```ruby\nputs 1\n```\n")) |
| 33 | + assert_equal "ruby", document.at_css("pre")["lang"] |
| 34 | + assert_equal "puts 1\n", document.at_css("code").text |
| 35 | + end |
| 36 | + |
| 37 | + def test_preserves_unsafe_html_and_footnotes_without_mutating_frozen_options |
| 38 | + options = {commonmarker_opts: [:UNSAFE, :FOOTNOTES].freeze}.freeze |
| 39 | + html = GitHub::Markup.render("raw.md", "hello <b>world</b>[^1]\n\n[^1]: note\n", options: options) |
| 40 | + document = Nokogiri::HTML.fragment(html) |
| 41 | + |
| 42 | + assert_equal "world", document.at_css("b").text |
| 43 | + assert_equal "1", document.at_css("sup a").text |
| 44 | + assert_includes document.at_css("li p").text, "note" |
| 45 | + end |
| 46 | + |
| 47 | + def test_nobreaks_preserves_the_installed_backends_behavior |
| 48 | + actual = GitHub::Markup.render("raw.md", "first\nsecond\n", options: {commonmarker_opts: [:NOBREAKS]}) |
| 49 | + expected = legacy_commonmarker? ? "<p>first second</p>\n" : "<p>first\nsecond</p>\n" |
| 50 | + assert_equal expected, actual |
| 51 | + end |
| 52 | + |
| 53 | + def test_conflicting_break_flags_preserve_the_installed_backends_precedence |
| 54 | + actual = GitHub::Markup.render("raw.md", "first\nsecond\n", options: {commonmarker_opts: [:HARDBREAKS, :NOBREAKS]}) |
| 55 | + expected = legacy_commonmarker? ? "<p>first<br />\nsecond</p>\n" : "<p>first\nsecond</p>\n" |
| 56 | + assert_equal expected, actual |
| 57 | + |
| 58 | + reverse = GitHub::Markup.render("raw.md", "first\nsecond\n", options: {commonmarker_opts: [:NOBREAKS, :HARDBREAKS]}) |
| 59 | + assert_equal "<p>first<br />\nsecond</p>\n", reverse |
| 60 | + end |
| 61 | + |
| 62 | + def test_table_style_option_and_frozen_extension_list |
| 63 | + content = "| left | right |\n| :--- | ---: |\n| a | b |\n" |
| 64 | + options = {commonmarker_opts: [:TABLE_PREFER_STYLE_ATTRIBUTES], commonmarker_exts: [:table].freeze} |
| 65 | + document = Nokogiri::HTML.fragment(GitHub::Markup.render("raw.md", content, options: options)) |
| 66 | + |
| 67 | + assert_equal ["left", "right"], document.css("th").map(&:text) |
| 68 | + attribute = legacy_commonmarker? ? "style" : "align" |
| 69 | + expected = legacy_commonmarker? ? "text-align: left" : "left" |
| 70 | + assert_equal expected, document.at_css("th")[attribute] |
| 71 | + end |
| 72 | + |
| 73 | + def test_double_tilde_option_preserves_the_installed_backends_behavior |
| 74 | + options = {commonmarker_opts: [:STRIKETHROUGH_DOUBLE_TILDE], commonmarker_exts: [:strikethrough]} |
| 75 | + actual = GitHub::Markup.render("raw.md", "~one~ ~~two~~\n", options: options) |
| 76 | + expected = legacy_commonmarker? ? "<p>~one~ <del>two</del></p>\n" : "<p><del>one</del> <del>two</del></p>\n" |
| 77 | + assert_equal expected, actual |
| 78 | + end |
| 79 | + |
| 80 | + def test_extensions_follow_the_installed_parsers_capabilities |
| 81 | + options = {commonmarker_exts: [:header_ids]} |
| 82 | + if legacy_commonmarker? |
| 83 | + error = assert_raises(ArgumentError) { GitHub::Markup.render("raw.md", "# Title", options: options) } |
| 84 | + assert_match(/extension header_ids not found/, error.message) |
| 85 | + else |
| 86 | + document = Nokogiri::HTML.fragment(GitHub::Markup.render("raw.md", "# Title", options: options)) |
| 87 | + assert_equal "title", document.at_css("a[id]")["id"] |
| 88 | + end |
| 89 | + end |
| 90 | + |
| 91 | + private |
| 92 | + |
| 93 | + def legacy_commonmarker? |
| 94 | + Gem.loaded_specs.fetch("commonmarker").version < Gem::Version.new("1") |
| 95 | + end |
| 96 | +end |
0 commit comments