Skip to content

Commit

Permalink
[9.x] feature: Str::inlineMarkdown() (#43126)
Browse files Browse the repository at this point in the history
* feature: Inline markdown helpers

* chore: fix styleci

* Update Str.php

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
danharrin and taylorotwell authored Jul 11, 2022
1 parent 6dd6884 commit 8301bcd
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
use Closure;
use Illuminate\Support\Traits\Macroable;
use JsonException;
use League\CommonMark\Environment\Environment;
use League\CommonMark\Extension\GithubFlavoredMarkdownExtension;
use League\CommonMark\Extension\InlinesOnly\InlinesOnlyExtension;
use League\CommonMark\GithubFlavoredMarkdownConverter;
use League\CommonMark\MarkdownConverter;
use Ramsey\Uuid\Codec\TimestampFirstCombCodec;
use Ramsey\Uuid\Generator\CombGenerator;
use Ramsey\Uuid\Uuid;
Expand Down Expand Up @@ -503,6 +507,25 @@ public static function markdown($string, array $options = [])
return (string) $converter->convert($string);
}

/**
* Converts inline Markdown into HTML.
*
* @param string $string
* @param array $options
* @return string
*/
public static function inlineMarkdown($string, array $options = [])
{
$environment = new Environment($options);

$environment->addExtension(new GithubFlavoredMarkdownExtension());
$environment->addExtension(new InlinesOnlyExtension());

$converter = new MarkdownConverter($environment);

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

/**
* Masks a portion of a string with a repeated character.
*
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 @@ -391,6 +391,17 @@ public function markdown(array $options = [])
return new static(Str::markdown($this->value, $options));
}

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

/**
* Masks a portion of a string with a repeated character.
*
Expand Down
6 changes: 6 additions & 0 deletions tests/Support/SupportStrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,12 @@ public function testMarkdown()
$this->assertSame("<h1>hello world</h1>\n", Str::markdown('# hello world'));
}

public function testInlineMarkdown()
{
$this->assertSame("<em>hello world</em>\n", Str::inlineMarkdown('*hello world*'));
$this->assertSame("<a href=\"https://laravel.com\"><strong>Laravel</strong></a>\n", Str::inlineMarkdown('[**Laravel**](https://laravel.com)'));
}

public function testRepeat()
{
$this->assertSame('aaaaa', Str::repeat('a', 5));
Expand Down
6 changes: 6 additions & 0 deletions tests/Support/SupportStringableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -959,6 +959,12 @@ public function testMarkdown()
$this->assertEquals("<h1>hello world</h1>\n", $this->stringable('# hello world')->markdown());
}

public function testInlineMarkdown()
{
$this->assertEquals("<em>hello world</em>\n", $this->stringable('*hello world*')->inlineMarkdown());
$this->assertEquals("<a href=\"https://laravel.com\"><strong>Laravel</strong></a>\n", $this->stringable('[**Laravel**](https://laravel.com)')->inlineMarkdown());
}

public function testMask()
{
$this->assertSame('tay*************', (string) $this->stringable('[email protected]')->mask('*', 3));
Expand Down

0 comments on commit 8301bcd

Please sign in to comment.