# 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 '', '

' end it 'keeps html entities' do assert_parsing_unsanitized_result '

 

', '

 

' end end describe '.to_html' do it 'returns empty string for nil input' do assert_parsing_result nil, '' end it 'keeps html entities' do assert_parsing_result '

 

', '

 

' end it 'does not create s inside words' do assert_parsing_result 'foo_bar_baz', '

foo_bar_baz

' end it 'removes iframes with script tags in the src' do md = '' html = '' assert_parsing_result md, html end it 'removes script tags and their content' do md = 'clean' html = '

clean

' assert_parsing_result md, html end it 'allows embedding vimeo videos when their do-no-track param is set to true' do src = '' assert_parsing_result src, src end it 'allows embedding youtube videos from its no-cookies domain' do src = '' assert_parsing_result src, src end it 'allows embedding youtube videos from its no-cookies domain with allow attribute' do src = '' assert_parsing_result src, src end it 'converts youtube videos embedded from the with-cookies domain to the no-cookies domain' do src = '' src_result = '' assert_parsing_result src, src_result end it 'does not allow embedding vimeo videos when their do-no-track param is set to something different than true' do src = '' assert_parsing_result src, '' end it 'does not allow embedding vimeo videos when their do-no-track param is not passed' do src = '' assert_parsing_result src, '' end it 'allows embedding vidyard videos with data attributes' do src = '

' assert_parsing_result src, src end it 'allows images' do src = '

image

' assert_parsing_result src, src end describe 'github markdown' do it 'leaves HTML comments' do md = '' html ='' assert_parsing_result(md, html) end it 'removes tag brackets < and > from HTML comments so they do not appear accidentally in generated HTML' do md = '' 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 = <<-HTML

That’s it

HTML assert_parsing_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_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 = <<-HTML

Testing

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 = <<-HTML

Testing

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 = <<-HTML

Testing

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 = <<-HTML

Testing

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 = <<-HTML

foo

bar

HTML assert_parsing_result md, html end it 'supports tables' do md = <<-MARKDOWN | A | B | | --- | --- | | 1 | 2 | | 3 | 4 | MARKDOWN html = <<-HTML
A B
1 2
3 4
HTML assert_parsing_result md, html end it "does emdashes" do md = "foo -- bar" html = '

foo – bar

' assert_parsing_result md, html end it "converts code block classes to language-" do md = <<-MARKDOWN ```lang-ruby # a comment ``` ```language-ruby # a comment ``` ```ruby # a comment ``` MARKDOWN html = <<-HTML
# a comment
# a comment
# a comment
HTML assert_parsing_result md, html end it 'converts relative links with missing initial slashes to article links' do ['foo', 'foo/bar', 'foo#bar', '123', 'categories'].each do |href| md = "[link](#{href})" html = "

link

" assert_parsing_result md, html end end it 'ignores links with missing initial slashes IFF they start with `categories/`' do ['categories/foo', 'categories/foo/bar', 'categories/foo#bar', 'categories/123'].each do |href| md = "[link](#{href})" html = "

link

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

link

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

link

' assert_parsing_result md, html end it 'adds a link prefix from options for relative dev center links' do md = '[link](foo)' expected = '

link

' result = DevcenterParser.to_html(md, { link_prefix: '/ja/articles' }) assert_equal expected.strip, result.strip, "Failed when parsing\n#{md}\n.\n\nExpected:\n#{expected}\n\nActual result:\n#{result}\n\n" 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_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 blockquotes for GitHub-style callouts in uppercase format' do md = <<-MARKDOWN > [!WARNING] > This is a warning MARKDOWN expected = <<-HTML

This is a warning

HTML assert_parsing_result md, expected end it 'generates blockquotes for GitHub-style callouts in lowercase format' do md = <<-MARKDOWN > [!warning] > This is a warning MARKDOWN expected = <<-HTML

This is a warning

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

foo

bar

HTML assert_parsing_result md, html end it 'allows id data-next-message and data-step-title attributes, used in dynamic tutorials' do md = <<-MARKDOWN

First step

Text for first step MARKDOWN html = <<-HTML

First step

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 def assert_contains(md, contains) assert DevcenterParser.to_html(md).include?(contains), "Failed when parsing #{md}\n.\n\nDid not contain: #{contains}\n\n" end end