giberti / commonmark-emoji-extension
为 CommonMark 添加表情符号支持
1.4
2024-09-11 12:51 UTC
Requires
- php: ^7.4|^8.0
- giberti/emoji-data: ^1.4
Requires (Dev)
- league/commonmark: ^2.4
- phpunit/phpunit: ^9.6
README
这是一个扩展,用于为League CommonMark 包提供 GitHub 和 Slack 风格的表情符号。默认情况下,它将使用官方的 Unicode CLDR 短名称 替换表情符号,但也可以使用别名将常见语言映射到官方名称。生成的输出将表情符号包裹在 <span>
中,以便进行额外的样式设置,并提供了 title
属性以提高可访问性。
质量
安装
composer require giberti/commonmark-emoji-extension
用法
use Giberti\EmojiExtension\EmojiExtension;
基本用法
要使用,请向 CommonMark 环境添加新的 EmojiExtension
实例,并像通常一样使用。
// Get a configured instance of the converter $environment = new \League\CommonMark\Environment\Environment(); $environment->addExtension(new \League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension()); $environment->addExtension(new \Giberti\EmojiExtension\EmojiExtension()); $converter = new \League\CommonMark\MarkdownConverter($environment); // <p>I can haz <span class="emoji" title="hot_beverage">☕</span>?</p> echo $converter->convert('I can haz :hot_beverage:?')->getContent();
提供别名
GitHub 和 Slack 都是快捷方式,它们并不直接映射到官方的 Unicode CLDR 短名称。映射可以在创建 EmojiExtension
实例时注入。
别名应该作为关联数组传递,键为新的别名,值为 CLDR 短名称等效项。
$aliases = [ ':coffee:' => ':hot_beverage:', ':smile:' => ':grinning_face_with_smiling_eyes:', // ... any other aliases you wish to support ]; // Get a configured instance of the converter $environment = new \League\CommonMark\Environment\Environment(); $environment->addExtension(new \League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension()); $environment->addExtension(new \Giberti\EmojiExtension\EmojiExtension($aliases)); $converter = new \League\CommonMark\MarkdownConverter($environment); // <p>I can haz <span class="emoji" title="coffee">☕</span>?</p> echo $converter->convert('I can haz :coffee:?')->getContent();