-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from Astrotomic/issue-3
#3 add ability to replace multiple emojis in plain text
- Loading branch information
Showing
23 changed files
with
270 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace Astrotomic\Twemoji\Concerns; | ||
|
||
use Astrotomic\Twemoji\Twemoji; | ||
|
||
trait Configurable | ||
{ | ||
protected string $type = Twemoji::SVG; | ||
|
||
protected string $base = 'https://twemoji.maxcdn.com/v/latest'; | ||
|
||
public function base(string $base): self | ||
{ | ||
$this->base = rtrim($base, '/'); | ||
|
||
return $this; | ||
} | ||
|
||
public function type(string $type): self | ||
{ | ||
$this->type = $type; | ||
|
||
return $this; | ||
} | ||
|
||
public function svg(): self | ||
{ | ||
$this->type = Twemoji::SVG; | ||
|
||
return $this; | ||
} | ||
|
||
public function png(): self | ||
{ | ||
$this->type = Twemoji::PNG; | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
namespace Astrotomic\Twemoji; | ||
|
||
use Astrotomic\Twemoji\Concerns\Configurable; | ||
use Closure; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
class EmojiText | ||
{ | ||
use Configurable; | ||
|
||
protected string $text; | ||
|
||
public function __construct(string $text) | ||
{ | ||
$this->text = $text; | ||
} | ||
|
||
public function toMarkdown(?Closure $alt = null): string | ||
{ | ||
return $this->replace('![%{alt}](%{src})', $alt); | ||
} | ||
|
||
public function toHtml(?Closure $alt = null, array $attributes = []): string | ||
{ | ||
$attributes = array_merge([ | ||
'width' => 72, | ||
'height' => 72, | ||
'loading' => 'lazy', | ||
'class' => 'twemoji', | ||
], $attributes); | ||
|
||
$attrs = implode(' ', array_map( | ||
fn (string $key, string $value): string => "{$key}=\"{$value}\"", | ||
array_keys($attributes), | ||
array_values($attributes) | ||
)); | ||
|
||
return $this->replace('<img src="%{src}" alt="%{alt}" '.$attrs.' />', $alt); | ||
} | ||
|
||
protected function replace(string $replacement, ?Closure $alt = null): string | ||
{ | ||
$text = $this->text; | ||
|
||
$text = preg_replace_callback( | ||
$this->regexp(), | ||
fn (array $matches): string => str_replace( | ||
['%{alt}', '%{src}'], | ||
[ | ||
$alt | ||
? $alt($matches[0]) | ||
: $matches[0], | ||
Twemoji::emoji($matches[0]) | ||
->base($this->base) | ||
->type($this->type) | ||
->url(), | ||
], | ||
$replacement | ||
), | ||
$text | ||
); | ||
|
||
return $text; | ||
} | ||
|
||
protected function regexp(): string | ||
{ | ||
return '/(?:'.json_decode(file_get_contents(dirname(__FILE__).'/regexp.json')).')/u'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Astrotomic\Twemoji; | ||
|
||
use Astrotomic\Twemoji\Concerns\Configurable; | ||
|
||
class Replacer | ||
{ | ||
use Configurable; | ||
|
||
public function text(string $text): EmojiText | ||
{ | ||
return (new EmojiText($text)) | ||
->base($this->base) | ||
->type($this->type); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
use Astrotomic\Twemoji\Replacer; | ||
use function Spatie\Snapshots\assertMatchesTextSnapshot; | ||
|
||
it('can replace emojis in plain text to markdown', function () { | ||
$replacer = new Replacer(); | ||
assertMatchesTextSnapshot($replacer->text("Hello \u{1F44B},\nEmojis are so cool! ππ")->toMarkdown()); | ||
}); | ||
|
||
it('can replace emojis in plain text to markdown using png', function () { | ||
$replacer = new Replacer(); | ||
$replacer->png(); | ||
assertMatchesTextSnapshot($replacer->text("Hello \u{1F44B},\nEmojis are so cool! ππ")->toMarkdown()); | ||
assertMatchesTextSnapshot($replacer->text("Hello \u{1F44B},\nEmojis are so cool! ππ")->toMarkdown()); | ||
}); | ||
|
||
it('can replace emojis in plain text to markdown using png once', function () { | ||
$replacer = new Replacer(); | ||
assertMatchesTextSnapshot($replacer->text("Hello \u{1F44B},\nEmojis are so cool! ππ")->png()->toMarkdown()); | ||
assertMatchesTextSnapshot($replacer->text("Hello \u{1F44B},\nEmojis are so cool! ππ")->toMarkdown()); | ||
}); | ||
|
||
it('can replace emojis in plain text to html', function () { | ||
$replacer = new Replacer(); | ||
assertMatchesTextSnapshot($replacer->text("Hello \u{1F44B},\nEmojis are so cool! ππ")->toHtml()); | ||
}); | ||
|
||
it('can replace emojis in plain text to html using png', function () { | ||
$replacer = new Replacer(); | ||
$replacer->png(); | ||
assertMatchesTextSnapshot($replacer->text("Hello \u{1F44B},\nEmojis are so cool! ππ")->toHtml()); | ||
assertMatchesTextSnapshot($replacer->text("Hello \u{1F44B},\nEmojis are so cool! ππ")->toHtml()); | ||
}); | ||
|
||
it('can replace emojis in plain text to html using png once', function () { | ||
$replacer = new Replacer(); | ||
assertMatchesTextSnapshot($replacer->text("Hello \u{1F44B},\nEmojis are so cool! ππ")->png()->toHtml()); | ||
assertMatchesTextSnapshot($replacer->text("Hello \u{1F44B},\nEmojis are so cool! ππ")->toHtml()); | ||
}); | ||
|
||
it('can replace multi codepoint emojis in plain text', function () { | ||
$replacer = new Replacer(); | ||
assertMatchesTextSnapshot($replacer->text(implode(PHP_EOL, [ | ||
'Hello π', | ||
'Hello ππ»', | ||
'Hello ππΌ', | ||
'Hello ππ½', | ||
'Hello ππΎ', | ||
'Hello ππΏ', | ||
]))->toMarkdown()); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
tests/__snapshots__/ReplacerTest__it_can_replace_emojis_in_plain_text_to_html__1.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Hello <img src="https://twemoji.maxcdn.com/v/latest/svg/1f44b.svg" alt="π" width="72" height="72" loading="lazy" class="twemoji" />, | ||
Emojis are so cool! <img src="https://twemoji.maxcdn.com/v/latest/svg/1f680.svg" alt="π" width="72" height="72" loading="lazy" class="twemoji" /><img src="https://twemoji.maxcdn.com/v/latest/svg/1f389.svg" alt="π" width="72" height="72" loading="lazy" class="twemoji" /> |
2 changes: 2 additions & 0 deletions
2
.../__snapshots__/ReplacerTest__it_can_replace_emojis_in_plain_text_to_html_using_png__1.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Hello <img src="https://twemoji.maxcdn.com/v/latest/72x72/1f44b.png" alt="π" width="72" height="72" loading="lazy" class="twemoji" />, | ||
Emojis are so cool! <img src="https://twemoji.maxcdn.com/v/latest/72x72/1f680.png" alt="π" width="72" height="72" loading="lazy" class="twemoji" /><img src="https://twemoji.maxcdn.com/v/latest/72x72/1f389.png" alt="π" width="72" height="72" loading="lazy" class="twemoji" /> |
2 changes: 2 additions & 0 deletions
2
.../__snapshots__/ReplacerTest__it_can_replace_emojis_in_plain_text_to_html_using_png__2.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Hello <img src="https://twemoji.maxcdn.com/v/latest/72x72/1f44b.png" alt="π" width="72" height="72" loading="lazy" class="twemoji" />, | ||
Emojis are so cool! <img src="https://twemoji.maxcdn.com/v/latest/72x72/1f389.png" alt="π" width="72" height="72" loading="lazy" class="twemoji" /><img src="https://twemoji.maxcdn.com/v/latest/72x72/1f680.png" alt="π" width="72" height="72" loading="lazy" class="twemoji" /> |
2 changes: 2 additions & 0 deletions
2
...apshots__/ReplacerTest__it_can_replace_emojis_in_plain_text_to_html_using_png_once__1.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Hello <img src="https://twemoji.maxcdn.com/v/latest/72x72/1f44b.png" alt="π" width="72" height="72" loading="lazy" class="twemoji" />, | ||
Emojis are so cool! <img src="https://twemoji.maxcdn.com/v/latest/72x72/1f680.png" alt="π" width="72" height="72" loading="lazy" class="twemoji" /><img src="https://twemoji.maxcdn.com/v/latest/72x72/1f389.png" alt="π" width="72" height="72" loading="lazy" class="twemoji" /> |
2 changes: 2 additions & 0 deletions
2
...apshots__/ReplacerTest__it_can_replace_emojis_in_plain_text_to_html_using_png_once__2.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Hello <img src="https://twemoji.maxcdn.com/v/latest/svg/1f44b.svg" alt="π" width="72" height="72" loading="lazy" class="twemoji" />, | ||
Emojis are so cool! <img src="https://twemoji.maxcdn.com/v/latest/svg/1f389.svg" alt="π" width="72" height="72" loading="lazy" class="twemoji" /><img src="https://twemoji.maxcdn.com/v/latest/svg/1f680.svg" alt="π" width="72" height="72" loading="lazy" class="twemoji" /> |
2 changes: 2 additions & 0 deletions
2
tests/__snapshots__/ReplacerTest__it_can_replace_emojis_in_plain_text_to_markdown__1.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Hello ![π](https://twemoji.maxcdn.com/v/latest/svg/1f44b.svg), | ||
Emojis are so cool! ![π](https://twemoji.maxcdn.com/v/latest/svg/1f680.svg)![π](https://twemoji.maxcdn.com/v/latest/svg/1f389.svg) |
2 changes: 2 additions & 0 deletions
2
...napshots__/ReplacerTest__it_can_replace_emojis_in_plain_text_to_markdown_using_png__1.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Hello ![π](https://twemoji.maxcdn.com/v/latest/72x72/1f44b.png), | ||
Emojis are so cool! ![π](https://twemoji.maxcdn.com/v/latest/72x72/1f680.png)![π](https://twemoji.maxcdn.com/v/latest/72x72/1f389.png) |
2 changes: 2 additions & 0 deletions
2
...napshots__/ReplacerTest__it_can_replace_emojis_in_plain_text_to_markdown_using_png__2.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Hello ![π](https://twemoji.maxcdn.com/v/latest/72x72/1f44b.png), | ||
Emojis are so cool! ![π](https://twemoji.maxcdn.com/v/latest/72x72/1f389.png)![π](https://twemoji.maxcdn.com/v/latest/72x72/1f680.png) |
2 changes: 2 additions & 0 deletions
2
...ots__/ReplacerTest__it_can_replace_emojis_in_plain_text_to_markdown_using_png_once__1.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Hello ![π](https://twemoji.maxcdn.com/v/latest/72x72/1f44b.png), | ||
Emojis are so cool! ![π](https://twemoji.maxcdn.com/v/latest/72x72/1f680.png)![π](https://twemoji.maxcdn.com/v/latest/72x72/1f389.png) |
2 changes: 2 additions & 0 deletions
2
...ots__/ReplacerTest__it_can_replace_emojis_in_plain_text_to_markdown_using_png_once__2.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Hello ![π](https://twemoji.maxcdn.com/v/latest/svg/1f44b.svg), | ||
Emojis are so cool! ![π](https://twemoji.maxcdn.com/v/latest/svg/1f389.svg)![π](https://twemoji.maxcdn.com/v/latest/svg/1f680.svg) |
6 changes: 6 additions & 0 deletions
6
tests/__snapshots__/ReplacerTest__it_can_replace_multi_codepoint_emojis_in_plain_text__1.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Hello ![π](https://twemoji.maxcdn.com/v/latest/svg/1f44b.svg) | ||
Hello ![ππ»](https://twemoji.maxcdn.com/v/latest/svg/1f44b-1f3fb.svg) | ||
Hello ![ππΌ](https://twemoji.maxcdn.com/v/latest/svg/1f44b-1f3fc.svg) | ||
Hello ![ππ½](https://twemoji.maxcdn.com/v/latest/svg/1f44b-1f3fd.svg) | ||
Hello ![ππΎ](https://twemoji.maxcdn.com/v/latest/svg/1f44b-1f3fe.svg) | ||
Hello ![ππΏ](https://twemoji.maxcdn.com/v/latest/svg/1f44b-1f3ff.svg) |
2 changes: 2 additions & 0 deletions
2
tests/__snapshots__/TwemojiTest__it_can_replace_emojis_in_plain_text_to_html__1.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Hello <img src="https://twemoji.maxcdn.com/v/latest/svg/1f44b.svg" alt="π" width="72" height="72" loading="lazy" class="twemoji" />, | ||
Emojis are so cool! <img src="https://twemoji.maxcdn.com/v/latest/svg/1f680.svg" alt="π" width="72" height="72" loading="lazy" class="twemoji" /><img src="https://twemoji.maxcdn.com/v/latest/svg/1f389.svg" alt="π" width="72" height="72" loading="lazy" class="twemoji" /> |
2 changes: 2 additions & 0 deletions
2
tests/__snapshots__/TwemojiTest__it_can_replace_emojis_in_plain_text_to_markdown__1.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Hello ![π](https://twemoji.maxcdn.com/v/latest/svg/1f44b.svg), | ||
Emojis are so cool! ![π](https://twemoji.maxcdn.com/v/latest/svg/1f680.svg)![π](https://twemoji.maxcdn.com/v/latest/svg/1f389.svg) |