# coding: UTF-8
require 'test_helper'
class HTMLRenderTest < Redcarpet::TestCase
def setup
@markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML)
@rndr = {
:no_html => Redcarpet::Render::HTML.new(:filter_html => true),
:no_images => Redcarpet::Render::HTML.new(:no_images => true),
:no_links => Redcarpet::Render::HTML.new(:no_links => true),
:safe_links => Redcarpet::Render::HTML.new(:safe_links_only => true),
:escape_html => Redcarpet::Render::HTML.new(:escape_html => true),
:hard_wrap => Redcarpet::Render::HTML.new(:hard_wrap => true),
:toc_data => Redcarpet::Render::HTML.new(:with_toc_data => true),
:prettify => Redcarpet::Render::HTML.new(:prettify => true)
}
end
def render_with(rndr, text)
Redcarpet::Markdown.new(rndr).render(text)
end
# Hint: overrides filter_html, no_images and no_links
def test_that_escape_html_works
source = <
EOS
expected = <
<script>BAD</script>
<img src="/favicon.ico" />
EOE markdown = render_with(@rndr[:escape_html], source) html_equal expected, markdown end def test_that_filter_html_works markdown = render_with(@rndr[:no_html], 'Through NO ') html_equal "Through NO DOUBLE NO
\n", markdown end def test_filter_html_doesnt_break_two_space_hard_break markdown = render_with(@rndr[:no_html], "Lorem, \nipsum\n") html_equal "Lorem,
\nipsum
a bold codespan
However, this should be an emphasised codespan
ABC
or DEF
Hey have a look at the bash man page!
\n" output = render_with(Redcarpet::Render::HTML.new, markdown) assert_equal html, output end def test_autolinking_works_as_expected markdown = "Example of uri ftp://user:pass@example.com/. Email foo@bar.com and link http://bar.com" renderer = Redcarpet::Markdown.new(Redcarpet::Render::HTML, :autolink => true) output = renderer.render(markdown) assert output.include? 'ftp://user:pass@example.com/' assert output.include? 'mailto:foo@bar.com' assert output.include? '' end def test_that_footnotes_work markdown = <<-MD This is a footnote.[^1] [^1]: It provides additional information. MD html = <This is a footnote.1It provides additional information. ↩
[^1] And a trailing definition
HTML renderer = Redcarpet::Markdown.new(Redcarpet::Render::HTML, :footnotes => true) output = renderer.render(markdown) assert_equal html, output end def test_footnotes_enabled_but_missing_definition markdown = "Some text with a marker[^1] but no definition." html = "Some text with a marker[^1] but no definition.
\n" renderer = Redcarpet::Markdown.new(Redcarpet::Render::HTML, :footnotes => true) output = renderer.render(markdown) assert_equal html, output end def test_autolink_short_domains markdown = "Example of uri ftp://auto/short/domains. Email auto@l.n and link http://a/u/t/o/s/h/o/r/t" renderer = Redcarpet::Markdown.new(Redcarpet::Render::HTML, :autolink => true) output = renderer.render(markdown) assert output.include? 'ftp://auto/short/domains' assert output.include? 'mailto:auto@l.n' assert output.include? 'http://a/u/t/o/s/h/o/r/t' end def test_toc_heading_id markdown = "# First level heading\n## Second level heading" output = render_with(@rndr[:toc_data], markdown) assert_match /")
end
def test_safe_links_only_with_anchors
markdown = "An [anchor link](#anchor) on a page."
renderer = Redcarpet::Markdown.new(@rndr[:safe_links])
output = renderer.render(markdown)
assert_match %r{anchor link}, output
end
def test_autolink_with_link_attributes
render = Redcarpet::Render::HTML.new(link_attributes: {rel: "nofollow"})
parser = Redcarpet::Markdown.new(render, autolink: true)
output = parser.render("https://github.com/")
assert_match %r{rel="nofollow"}, output
end
end