Skip to content

Commit

Permalink
Fixed converting same line link/image markdown to html
Browse files Browse the repository at this point in the history
  • Loading branch information
jamielsharief committed Oct 25, 2019
1 parent 5fb5dfc commit 605ec43
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.0.1] - 2019-10-25
### Fixed
- Fixed converting sameline link/image markdown to html

## [1.0.0] - 2019-10-11

This component has been decoupled from the [OriginPHP framework](https://www.originphp.com/).
5 changes: 2 additions & 3 deletions src/Markdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public static function toHtml(string $Markdown, array $options = []): string

$Markdown = static::parseHeadings($Markdown);

$Markdown = preg_replace_callback('/!\[(.*)\]\((.*)\)/', function ($matches) {
$Markdown = preg_replace_callback('#!\[([^\]]*)\]\(([^\)]*)\)#', function ($matches) {
$alt = static::escape($matches[1]);

// Remove invalid urls
Expand All @@ -151,10 +151,9 @@ public static function toHtml(string $Markdown, array $options = []): string
return '<img src="'.$src.'" alt="'.$alt.'">';
}, $Markdown);

$Markdown = preg_replace_callback('/\[(.*)\]\((.*)\)/', function ($matches) {
$Markdown = preg_replace_callback('#\[([^\[]+)\]\(([^\)]+)\)#', function ($matches) {
$Markdown = static::escape($matches[1]);

// Remove invalid urls
$href = '';
if (filter_var($matches[2], FILTER_VALIDATE_URL)) {
$href = static::escape($matches[2]);
Expand Down
21 changes: 20 additions & 1 deletion tests/MarkdownTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,25 @@

class MarkdownTest extends \PHPUnit\Framework\TestCase
{
public function testLink()
{
$string = 'This is a [Test](http://www.example.com).';
$this->assertEquals('<p>This is a <a href="http://www.example.com">Test</a>.</p>', Markdown::toHtml($string));

$string = 'This is a [Test](http://www.example.com/t1) and [Test 2](http://www.example.com/t2)';
$this->assertEquals('<p>This is a <a href="http://www.example.com/t1">Test</a> and <a href="http://www.example.com/t2">Test 2</a></p>', Markdown::toHtml($string));
}

public function testImage()
{
$string = 'Google Logo ![](https://upload.wikimedia.org/wikipedia/commons/thumb/2/2f/Google_2015_logo.svg/220px-Google_2015_logo.svg.png) is cool';
$this->assertStringContainsString('<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/2/2f/Google_2015_logo.svg/220px-Google_2015_logo.svg.png" alt="">', Markdown::toHtml($string));
$string = '![logo 3](http://a.com/img/logo3.png)';
$this->assertStringContainsString('<img src="http://a.com/img/logo3.png" alt="logo 3">', Markdown::toHtml($string));
$string = 'A ![](http://a.com/img/logo1.png) B ![](http://a.com/img/logo2.png) C ![logo 3](http://a.com/img/logo3.png)';
$this->assertEquals('<p>A <img src="http://a.com/img/logo1.png" alt=""> B <img src="http://a.com/img/logo2.png" alt=""> C <img src="http://a.com/img/logo3.png" alt="logo 3"></p>', Markdown::toHtml($string));
}

public function testToText()
{
$text = <<< EOF
Expand Down Expand Up @@ -127,7 +146,7 @@ public function testToHtml()
Strikethrough uses two tildes. ~~Scratch this.~~
EOF;
$expected = '288d423d9404962691e75a88a3e3384a';

$this->assertEquals($expected, md5(Markdown::toHtml($text)));
$this->assertEquals($expected, md5(Markdown::toHtml($text, ['escape' => false])));
}
Expand Down

0 comments on commit 605ec43

Please sign in to comment.