Skip to content

Commit

Permalink
Merge branch 'markdown-str' into 8.x
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Jan 29, 2021
2 parents 741dbed + cb091a2 commit 44e5d47
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Support;

use Illuminate\Support\Traits\Macroable;
use League\CommonMark\GithubFlavoredMarkdownConverter;
use Ramsey\Uuid\Codec\TimestampFirstCombCodec;
use Ramsey\Uuid\Generator\CombGenerator;
use Ramsey\Uuid\Uuid;
Expand Down Expand Up @@ -376,6 +377,20 @@ public static function words($value, $words = 100, $end = '...')
return rtrim($matches[0]).$end;
}

/**
* Converts GitHub flavored Markdown into HTML.
*
* @param string $string
* @param array $options
* @return string
*/
public static function markdown($string, array $options = [])
{
$converter = new GithubFlavoredMarkdownConverter($options);

return $converter->convertToHtml($string);
}

/**
* Pad both sides of a string with another.
*
Expand Down
11 changes: 11 additions & 0 deletions src/Illuminate/Support/Stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,17 @@ public function lower()
return new static(Str::lower($this->value));
}

/**
* Convert GitHub flavored Markdown into HTML.
*
* @param array $options
* @return string
*/
public function markdown(array $options = [])
{
return new static(Str::markdown($this->value, $options));
}

/**
* Get the string matching the given pattern.
*
Expand Down
6 changes: 6 additions & 0 deletions tests/Support/SupportStrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,12 @@ public function invalidUuidList()
['ff6f8cb0-c57da-51e1-9b21-0800200c9a66'],
];
}

public function testMarkdown()
{
$this->assertEquals("<p><em>hello world</em></p>\n", Str::markdown('*hello world*'));
$this->assertEquals("<h1>hello world</h1>\n", Str::markdown('# hello world'));
}
}

class StringableObjectStub
Expand Down
6 changes: 6 additions & 0 deletions tests/Support/SupportStringableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -574,4 +574,10 @@ public function testPipe()
$this->assertInstanceOf(Stringable::class, $this->stringable('foo')->pipe($callback));
$this->assertSame('bar', (string) $this->stringable('foo')->pipe($callback));
}

public function testMarkdown()
{
$this->assertEquals("<p><em>hello world</em></p>\n", $this->stringable('*hello world*')->markdown());
$this->assertEquals("<h1>hello world</h1>\n", $this->stringable('# hello world')->markdown());
}
}

0 comments on commit 44e5d47

Please sign in to comment.