Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] feature: Str::inlineMarkdown() #43126

Merged
merged 3 commits into from
Jul 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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