assertEquals($expected, markdown_to_bb($src, true, ['preserve_lf' => true])); } public static function markdown_to_bbcode_provider(): array { return [ 'empty text' => [ '', '' ], 'plain text' => [ 'This is a test', 'This is a test' ], 'bold and italic' => [ 'This is a test of [b]bold text[/b], [i]italic text[/i] and [b][i]bold and italic text[/i][/b]', 'This is a test of **bold text**, *italic text* and ***bold and italic text***' ], 'multiline text' => [ // This is not as expected in markdown, but may be needed // for compatibility with bbcode behaviour. "This text is\ntext wrapped\nover multiple\nlines.", "This text is\ntext wrapped\nover multiple\nlines." ], 'text with hard linebreak' => [ // An extra line break is inserted here... "Line one\n\nLine two", "Line one \nLine two" ], 'paragraphs' => [ "Paragraph one\n\nParagraph two", "Paragraph one\n\nParagraph two", ], 'inline image' => [ '[img=https://example.com/image.jpg]https://example.com/image.jpg[/img]', '![](https://example.com/image.jpg)' ], 'inline image with alt text' => [ '[img=https://example.com/image.jpg]Alt text[/img]', '![Alt text](https://example.com/image.jpg)' ], 'inline code' => [ '[code]some code[/code]', '`some code`' ], 'inline code with wrapped text' => [ // Not sure if the newline should be preseved here? "[code]some code\nunwrapped[/code]", "`some code\n unwrapped`" ], 'code block no language' => [ "[code]some code\nover multiple lines\n[/code]", "```\nsome code\nover multiple lines\n```" ], 'code block no language indented' => [ // For some reason one space char is eaten on indented lines. "[code]some code\n over multiple lines\n with indentation\n[/code]", "```\nsome code\n over multiple lines\n with indentation\n```" ], 'code block with language' => [ "[code=php]<?php\necho phpinfo();\n[/code]", "```php\n [ "[code]an example url https://example.com\n[/code]", "```\nan example url https://example.com\n```" ], 'bbcode code block with URL' => [ "[code]\nproxy_pass http://example.com;\n[/code]", "[code]\nproxy_pass http://example.com;\n[/code]" ], 'naked url followed by newline' => [ "https://example.com\nhave a great day.", "https://example.com\nhave a great day.", ], 'inline naked url' => [ 'This is a link https://example.com/some/path more info.', 'This is a link https://example.com/some/path more info.', ], ]; } /** * @covers ::html2markdown * @dataProvider html2markdownProvider */ public function testHtml2markdown(string $html, string $markdown): void { $this->assertEquals($markdown, html2markdown($html)); } public static function html2markdownProvider(): array { return [ 'empty text' => [ '', '' ], 'space and nbsp only' => [ '  ', '' ], 'strong, b, em, i, bib' => [ 'strong bold em italic boitalicld', '**strong** **bold** *em* *italic* **bo*italic*ld**' ], 'empty tags' => [ 'text1 text2 ', 'text1 text2' ], 'HTML entities' => [ '& gt > lt <', '& gt > lt <' ], 'escaped HTML entities' => [ '& lt < gt >', '& lt < gt >' ], 'linebreak' => [ "line1
line2\nline3", "line1 \nline2 line3" ], 'headlines' => [ '

header1

Header 3

', "header1\n=======\n\n### Header 3" ], 'unordered list' => [ '', "- Item 1\n- Item 2\n- Item **3**" ], 'ordered list' => [ '
  1. Item 1
  2. Item 2
  3. Item 3
', "1. Item 1\n2. Item 2\n3. Item **3**" ], 'nested lists' => [ '', "- Item A\n- Item B\n - Nested A\n - Nested B\n- Item C" ], 'img' => [ 'alt text', '![alt text](/path/to/img.png "title text")' ], 'link' => [ 'link', '[link](http://hubzilla.org "Hubzilla")' ], 'img link' => [ 'alt img text', '[![alt img text](/img/hubzilla.png "img title")](http://hubzilla.org "Hubzilla")' ], 'script' => [ "", "" ], 'blockquote, issue #793' => [ '
something
blah', "> something\n\nblah" ], 'code' => [ '<p>HTML text</p>', '`

HTML text

`' ], 'pre' => [ '
  one line with spaces  
', "```\n one line with spaces \n```" ], 'div p' => [ '
div

p

', "
div
p\n\n
" ] ]; } public function test_bb_to_markdown(): void { $input = "test[b]bold[/b]\n[i]i[/i][ul][li]li1[/li][li]li2[/li][/ul]\n"; $expected = "test**bold** \n*i*\n\n- li1\n- li2"; $this->assertEquals($expected, bb_to_markdown($input)); } }