# encoding: utf-8
require 'minitest/autorun'
require_relative '../lib/devcenter-parser'
describe 'DevcenterParser' do
describe '.to_unsanitized_html' do
it 'returns empty string for nil input' do
assert_parsing_unsanitized_result nil, ''
end
it 'maintains script tags' do
assert_parsing_unsanitized_result '', ''
end
it 'maintains toolbelt custom element' do
assert_parsing_unsanitized_result '
foo_bar_baz
' end it 'removes script tags and their content' do md = 'clean' html = 'clean
' assert_parsing_result md, html end it 'allows embedding vimeo videos' do src = <<-SRC SRC assert_parsing_result src, src end describe 'github markdown' do it 'removes markdown links or nested tags inside HTML comments' do md = <<-MD 1 2 34 One 5 6 78 Two 9 10 1112 Three MD html = <<-HTML1 2 34
One
5 6 78
Two
9 10 1112
Three
HTML assert_parsing_result md, html end it 'generates apostrophes from single quotes in plain text' do md = "That's it" html = "That’s it
" assert_parsing_result(md, html) end it 'generates apostrophes from single quotes in callout|warning|note blocks' do md = <<-MARKDOWN > warning > That's it MARKDOWN html = <<-HTMLThat’s it
Paragraph
$ command
indented
< tag1
> tag2
Another paragraph
HTML assert_parsing_result md, html end it 'supports regular block quotes without callout|warning|note' do md = <<-MARKDOWN Testing > not a callout > **strong** > normal And that's it. MARKDOWN html = <<-HTMLTesting
not a callout strong normal
And that’s it.
HTML assert_parsing_result(md, html) md = <<-MARKDOWN Testing > calloutnonono > **strong** > normal And that's it. MARKDOWN html = <<-HTMLTesting
calloutnonono strong normal
And that’s it.
HTML assert_parsing_result(md, html) end it 'supports "> callout" and ">callout" and parses inner markdown' do mds = [] mds << <<-MARKDOWN Testing > callout > **strong** > normal And that's it. MARKDOWN mds << <<-MARKDOWN Testing >callout >**strong** >normal And that's it. MARKDOWN html = <<-HTMLTesting
strong normal
And that’s it.
HTML mds.each do |md| assert_parsing_result(md, html) end end it 'supports "> callout" and ">callout", parses inner markdown and allows paragraphs' do mds = [] mds << <<-MARKDOWN Testing > callout > **strong** > more callout > normal And that's it. MARKDOWN mds << <<-MARKDOWN Testing >callout >**strong** >more callout >normal And that's it. MARKDOWN html = <<-HTMLTesting
strong more callout
normal
And that’s it.
HTML mds.each do |md| assert_parsing_result(md, html) end end end it 'generates separate special blocks from blockquotes separated by empty lines' do md = <<-MARKDOWN > warning > foo > note > bar MARKDOWN html = <<-HTMLfoo
bar
A | B |
---|---|
1 | 2 |
3 | 4 |
foo – bar
' assert_parsing_result md, html end it 'converts relative links with missing initial slashes to article links' do ['foo', 'foo/bar', 'foo#bar', '123'].each do |href| md = "[link](#{href})" html = "" assert_parsing_result md, html end end it 'converts "articles/foo relative links with missing initial slashes to article links' do md = '[link](articles/foo)' html = '' assert_parsing_result md, html md = '[link](articles/foo#bar)' html = '' assert_parsing_result md, html end it 'does not alter relative links with initial slashes nor absolute links nor anchor links to the same doc' do ['http://foo.com', 'https://foo.com', '/foo', '/foo/bar', '/foo#bar', '#foo', '/123', 'mailto:foo@foobar.com'].each do |href| md = "[link](#{href})" html = "" assert_parsing_result md, html end end it 'does not add href attribute to links where it does not exist' do md = '' html = '' assert_parsing_result md, html end it 'generates separate blockquotes from blockquotes separated by empty lines' do md = <<-MARKDOWN > foo > bar MARKDOWN html = <<-HTMLfoo
HTML assert_parsing_result md, html end it 'allows id data-next-message and data-step-title attributes, used in dynamic tutorials' do md = <<-MARKDOWNbar
Text for first step
HTML assert_parsing_result md, html end it 'removes elements with src="javascript:"' do md = <<-MARKDOWN MARKDOWN assert_parsing_result md, "" end it 'removes elements with src="jaVasCript:"' do md = <<-MARKDOWN MARKDOWN assert_parsing_result md, "" end end # helpers def assert_parsing_result(md, expected) result = DevcenterParser.to_html(md) assert_equal expected.strip, result.strip, "Failed when parsing\n#{md}\n.\n\nExpected:\n#{expected}\n\nActual result:\n#{result}\n\n" end def assert_parsing_unsanitized_result(md, expected) result = DevcenterParser.to_unsanitized_html(md) assert_equal expected.strip, result.strip, "Failed when parsing on unsanitized mode\n#{md}\n.\n\nExpected:\n#{expected}\n\nActual result:\n#{result}\n\n" end def assert_header_id(md, header, id) assert DevcenterParser.to_html(md).include?("<#{header} id=\"#{id}\">"), "GitHub does not generate a #{header} with id #{id}" end end