2017-07-22 01:20:45 +08:00
|
|
|
# frozen_string_literal: true
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe "CommonMark" do
|
2017-07-22 01:20:45 +08:00
|
|
|
it "passes spec" do
|
|
|
|
SiteSetting.traditional_markdown_linebreaks = true
|
|
|
|
SiteSetting.enable_markdown_typographer = false
|
2019-10-08 19:00:22 +08:00
|
|
|
SiteSetting.highlighted_languages = "ruby|aa"
|
2017-07-22 01:20:45 +08:00
|
|
|
|
|
|
|
html, state, md = nil
|
|
|
|
failed = 0
|
|
|
|
|
|
|
|
File
|
|
|
|
.readlines(Rails.root + "spec/fixtures/md/spec.txt")
|
|
|
|
.each do |line|
|
|
|
|
if line == "```````````````````````````````` example\n"
|
|
|
|
state = :example
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
if line == "````````````````````````````````\n"
|
|
|
|
md.gsub!("→", "\t")
|
|
|
|
html ||= String.new
|
|
|
|
html.gsub!("→", "\t")
|
|
|
|
html.strip!
|
|
|
|
|
|
|
|
# normalize brs
|
|
|
|
html.gsub!("<br />", "<br>")
|
|
|
|
html.gsub!("<hr />", "<hr>")
|
|
|
|
html.gsub!(%r{<img([^>]+) />}, "<img\\1>")
|
|
|
|
|
2018-02-01 10:22:38 +08:00
|
|
|
SiteSetting.enable_markdown_linkify = false
|
|
|
|
cooked = PrettyText.markdown(md, sanitize: false)
|
2017-07-22 01:20:45 +08:00
|
|
|
cooked.strip!
|
|
|
|
cooked.gsub!(" class=\"lang-auto\"", "")
|
2023-08-08 09:18:55 +08:00
|
|
|
cooked.gsub!(%r{<span class="hashtag-raw">(.*)</span>}, "\\1")
|
2021-03-23 16:45:06 +08:00
|
|
|
cooked.gsub!(%r{<a name="(.*)" class="anchor" href="#\1*"></a>}, "")
|
2022-02-09 18:23:44 +08:00
|
|
|
# we support data-attributes which is not in the spec
|
2023-11-11 04:39:48 +08:00
|
|
|
cooked.gsub!(" data-code-startline=\"3\"", "")
|
|
|
|
cooked.gsub!(%r{ data-code-wrap="[^"]+"}, "")
|
2017-07-22 01:20:45 +08:00
|
|
|
# we don't care about this
|
|
|
|
cooked.gsub!("<blockquote>\n</blockquote>", "<blockquote></blockquote>")
|
|
|
|
html.gsub!("<blockquote>\n</blockquote>", "<blockquote></blockquote>")
|
|
|
|
html.gsub!("language-ruby", "lang-ruby")
|
2019-10-08 19:00:22 +08:00
|
|
|
html.gsub!("language-aa", "lang-aa")
|
2017-07-22 01:20:45 +08:00
|
|
|
# strip out unsupported languages
|
|
|
|
html.gsub!(%r{ class="language-[;f].*"}, "")
|
|
|
|
|
|
|
|
unless cooked == html
|
|
|
|
failed += 1
|
|
|
|
puts "FAILED SPEC"
|
|
|
|
puts "Expected: "
|
|
|
|
puts html
|
|
|
|
puts "Got: "
|
|
|
|
puts cooked
|
|
|
|
puts "Markdown: "
|
|
|
|
puts md
|
|
|
|
puts
|
2023-01-09 19:18:21 +08:00
|
|
|
end
|
2017-07-22 01:20:45 +08:00
|
|
|
html, state, md = nil
|
2023-01-09 19:18:21 +08:00
|
|
|
next
|
2017-07-22 01:20:45 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
if state == :example && line == ".\n"
|
|
|
|
state = :html
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
md = (md || String.new) << line if state == :example
|
|
|
|
|
|
|
|
html = (html || String.new) << line if state == :html
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(failed).to eq(0)
|
|
|
|
end
|
|
|
|
end
|