Skip to content

Commit

Permalink
[ENHANCEMENT] - Allow shortcode aliases (#44)
Browse files Browse the repository at this point in the history
* [ENHANCEMENT] - Allow shortcode aliases

* Apply coding guidelines
  • Loading branch information
loranger authored Sep 11, 2024
1 parent 319324e commit bd221f4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ LitEmoji::config('excludeShortcodes', ['mobile', 'android']);

echo LitEmoji::encodeShortcode('📱');
// ':iphone:'

// Add aliases for custom shortcodes
LitEmoji::config('aliasShortcodes', ['yeah' => 'thumbsup']);
echo LitEmoji::encodeUnicode('Hell :yeah:!');
// 'Hell 👍!'
```

# Encodings
Expand Down
17 changes: 17 additions & 0 deletions src/LitEmoji.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class LitEmoji
private static array $shortcodeEntities = [];
private static array $entityCodepoints = [];
private static array $excludedShortcodes = [];
private static array $aliasedShortcodes = [];

/**
* Converts all unicode emoji and HTML entities to plaintext shortcodes.
Expand Down Expand Up @@ -183,6 +184,15 @@ public static function config(string $property, $value): void
self::$entityCodepoints = [];
self::$excludedShortcodes = [];
break;
case 'aliasShortcodes':
self::$aliasedShortcodes = (array) $value;

// Invalidate shortcode cache
self::$shortcodes = [];
self::$shortcodeCodepoints = [];
self::$shortcodeEntities = [];
self::$entityCodepoints = [];
break;
}
}

Expand All @@ -209,6 +219,13 @@ private static function getShortcodes(): array
return !in_array($code, self::$excludedShortcodes);
}, ARRAY_FILTER_USE_KEY);

// Append shortcode aliases
foreach (self::$aliasedShortcodes as $alias => $code) {
if (array_key_exists($code, self::$shortcodes)) {
self::$shortcodes[$alias] = self::$shortcodes[$code];
}
}

return self::$shortcodes;
}

Expand Down
10 changes: 8 additions & 2 deletions tests/LitEmojiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,19 @@ public function testConfigExcludeShortcodes()
$this->assertEquals(':android:', LitEmoji::encodeShortcode('📱'));
}

public function testConfigIncludeShortcodeAliases()
{
LitEmoji::config('aliasShortcodes', ['thumbs_up' => 'thumbsup', 'yeah' => 'thumbsup']);
$this->assertEquals('👍', LitEmoji::encodeUnicode(':yeah:'));
}

public function testUnicodeMatching()
{
$shortcodes = require(__DIR__ . '/../src/emoji.php');
$shortcodes = require __DIR__ . '/../src/emoji.php';
$shortcodes = array_flip($shortcodes);

foreach ($shortcodes as $shortcode) {
$unicode = LitEmoji::encodeUnicode(':' . $shortcode . ':');
$unicode = LitEmoji::encodeUnicode(':'.$shortcode.':');
$matched = LitEmoji::unicodeToShortcode($unicode);

$this->assertNotEquals($unicode, $matched);
Expand Down

0 comments on commit bd221f4

Please sign in to comment.