# 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, :maruku, '' assert_parsing_unsanitized_result nil, :maruku, '' end it 'maintains script tags' do md = '' assert_parsing_unsanitized_result md, :maruku, '' assert_parsing_unsanitized_result md, :github, '' end end describe '.to_html' do it 'returns empty string for nil input' do assert_maruku_result nil, '' assert_github_result nil, '' end it 'does not create s inside words' do md = 'foo_bar_baz' html = '

foo_bar_baz

' assert_maruku_result md, html assert_github_result md, html end it 'raises InvalidMarkdownError when parsing invalid markdown' do md = '[foo](bar' assert_raises DevcenterParser::InvalidMarkdownError do DevcenterParser.to_html(md, :maruku) end end it 'removes script tags and their content' do md = 'clean' html = '

clean

' assert_maruku_result md, html assert_github_result md, html end it 'allows embedding vimeo videos' do src = <<-SRC SRC assert_github_result src, src end describe 'github markdown' do it 'generates apostrophes from single quotes in plain text' do md = "That's it" html = "

That’s it

" assert_github_result(md, html) end it 'generates apostrophes from single quotes in callout|warning|note blocks' do md = <<-MARKDOWN > warning > That's it MARKDOWN html = <<-HTML

That’s it

HTML assert_github_result(md, html) end it 'supports code blocks, respecting indentation and adding a custom class attribute' do md = <<-MARKDOWN Paragraph :::term $ command indented < tag1 > tag2 Another paragraph MARKDOWN html = <<-HTML

Paragraph

$ command

  indented
< tag1
> tag2

Another paragraph

HTML assert_maruku_result md, html md = <<-MARKDOWN Paragraph ```term $ command indented < tag1 > tag2 ``` Another paragraph MARKDOWN html = <<-HTML

Paragraph

$ command

  indented
< tag1
> tag2

Another paragraph

HTML assert_github_result md, html end it 'github markdown supports regular block quotes without callout|warning|note' do md = <<-MARKDOWN Testing > not a callout > **strong** > normal And that's it. MARKDOWN html = <<-HTML

Testing

not a callout strong normal

And that’s it.

HTML assert_github_result(md, html) md = <<-MARKDOWN Testing > calloutnonono > **strong** > normal And that's it. MARKDOWN html = <<-HTML

Testing

calloutnonono strong normal

And that’s it.

HTML assert_github_result(md, html) end it 'github markdown 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 = <<-HTML

Testing

strong normal

And that’s it.

HTML mds.each do |md| assert_github_result(md, html) end end it 'github markdown 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 = <<-HTML

Testing

strong more callout

normal

And that’s it.

HTML mds.each do |md| assert_github_result(md, html) end end end it 'github markdown generates separate special blocks from blockquotes separated by empty lines' do md = <<-MARKDOWN > warning > foo > note > bar MARKDOWN html = <<-HTML

foo

bar

HTML assert_github_result md, html end it 'github markdown supports tables' do md = <<-MARKDOWN | A | B | | --- | --- | | 1 | 2 | | 3 | 4 | MARKDOWN html = <<-HTML
A B
1 2
3 4
HTML assert_github_result md, html end it "does emdashes both in all flavours" do md = "foo -- bar" html = '

foo – bar

' assert_all_flavours_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 = "

link

" assert_maruku_result md, html assert_github_result md, html end end it 'converts "articles/foo relative links with missing initial slashes to article links' do md = '[link](articles/foo)' html = '

link

' assert_maruku_result md, html assert_github_result md, html md = '[link](articles/foo#bar)' html = '

link

' assert_maruku_result md, html assert_github_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 = "

link

" assert_maruku_result md, html assert_github_result md, html end end it 'does not add href attribute to links where it does not exist' do md = '' html = '

' assert_maruku_result md, html assert_github_result md, html end it 'generates separate blockquotes from blockquotes separated by empty lines' do md = <<-MARKDOWN > foo > bar MARKDOWN html_maruku = <<-HTML

foo

bar

HTML assert_maruku_result md, html_maruku html_github = <<-HTML

foo

bar

HTML assert_github_result md, html_github end end # helpers def assert_all_flavours_result(md, expected) [:github, :maruku].each { |flavour| assert_parsing_result(md, flavour, expected) } end def assert_maruku_result(md, expected) assert_parsing_result md, :maruku, expected end def assert_github_result(md, expected) assert_parsing_result md, :github, expected end def assert_parsing_result(md, flavour, expected) result = DevcenterParser.to_html(md, flavour) assert_equal expected.strip, result.strip, "Failed when parsing\n#{md}\nwith the #{flavour} flavour.\n\nExpected:\n#{expected}\n\nActual result:\n#{result}\n\n" end def assert_parsing_unsanitized_result(md, flavour, expected) result = DevcenterParser.to_unsanitized_html(md, flavour) assert_equal expected.strip, result.strip, "Failed when parsing on unsanitized mode\n#{md}\nwith the #{flavour} flavour.\n\nExpected:\n#{expected}\n\nActual result:\n#{result}\n\n" end def assert_header_id(md, header, id) assert DevcenterParser.to_html(md, :github).include?("<#{header} id=\"#{id}\">"), "GitHub does not generate a #{header} with id #{id}" assert DevcenterParser.to_html(md, :maruku).include?("<#{header} id=\"#{id}\">"), "Maruku does not generate a #{header} with id #{id}" end end