EmojiExtension.php in Markdown 8.2
File
src/Plugin/Markdown/CommonMark/Extension/EmojiExtension.php
View source
<?php
namespace Drupal\markdown\Plugin\Markdown\CommonMark\Extension;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\PluginFormInterface;
use Drupal\Core\Theme\ActiveTheme;
use Drupal\markdown\Plugin\Markdown\AllowedHtmlInterface;
use Drupal\markdown\Plugin\Markdown\CommonMark\BaseExtension;
use Drupal\markdown\Plugin\Markdown\ParserInterface;
use Drupal\markdown\Plugin\Markdown\SettingsInterface;
use Drupal\markdown\Traits\SettingsTrait;
use Drupal\markdown\Util\LaravelCacheRepositoryAdapter;
use Drupal\markdown\Util\LaravelCacheStoreAdapter;
class EmojiExtension extends BaseExtension implements AllowedHtmlInterface, PluginFormInterface, SettingsInterface {
use SettingsTrait;
public static function defaultSettings($pluginDefinition) {
return [
'github_api_token' => NULL,
];
}
public function allowedHtmlTags(ParserInterface $parser, ActiveTheme $activeTheme = NULL) {
return [
'img' => [
'class' => 'emoji',
'data-emoji' => TRUE,
],
];
}
public function register($environment) {
$token = $this
->getSetting('github_api_token');
if (!$token) {
return;
}
switch ($this->pluginDefinition->object) {
case 'CachetHQ\\Emoji\\EmojiExtension':
$repo = new \CachetHQ\Emoji\Repositories\GitHubRepository(\Drupal::httpClient(), $token);
$cache = new LaravelCacheRepositoryAdapter(new LaravelCacheStoreAdapter(\Drupal::cache('markdown'), 'commonmark-ext-emoji'));
$parser = new \CachetHQ\Emoji\EmojiParser(new \CachetHQ\Emoji\Repositories\CachingRepository($repo, $cache, 'github-repo', 604800));
$environment
->addInlineParser($parser);
break;
}
}
public function buildConfigurationForm(array $element, FormStateInterface $form_state) {
$element += $this
->createSettingElement('github_api_token', [
'#type' => 'textfield',
'#title' => $this
->t('GitHub API Token'),
'#description' => $this
->t('You must <a href=":generate_token" target="_blank">generate a GitHub API token</a> that will be used as the map of available emojis. Note: it does not need any scopes or permissions, it is just used for authentication to avoid rate limiting.', [
':generate_token' => 'https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line',
]),
], $form_state);
return $element;
}
public function settingsKey() {
return FALSE;
}
}