diff --git a/extensions/bbcode/composer.json b/extensions/bbcode/composer.json index b89c937f97..6561adebe7 100644 --- a/extensions/bbcode/composer.json +++ b/extensions/bbcode/composer.json @@ -21,6 +21,11 @@ "require": { "flarum/core": "^1.7" }, + "autoload": { + "psr-4": { + "Flarum\\BBCode\\": "src" + } + }, "extra": { "branch-alias": { "dev-main": "1.x-dev" diff --git a/extensions/bbcode/extend.php b/extensions/bbcode/extend.php index 325d7b4185..36dec929b8 100644 --- a/extensions/bbcode/extend.php +++ b/extensions/bbcode/extend.php @@ -7,36 +7,14 @@ * LICENSE file that was distributed with this source code. */ +namespace Flarum\BBCode; + use Flarum\Extend; -use s9e\TextFormatter\Configurator; -use s9e\TextFormatter\Renderer; return [ new Extend\Locales(__DIR__.'/locale'), (new Extend\Formatter) - ->render(function (Renderer $renderer, $context, string $xml) { - $renderer->setParameter('L_WROTE', resolve('translator')->trans('flarum-bbcode.forum.quote.wrote')); - - return $xml; - }) - ->configure(function (Configurator $config) { - $config->BBCodes->addFromRepository('B'); - $config->BBCodes->addFromRepository('I'); - $config->BBCodes->addFromRepository('U'); - $config->BBCodes->addFromRepository('S'); - $config->BBCodes->addFromRepository('URL'); - $config->BBCodes->addFromRepository('IMG'); - $config->BBCodes->addFromRepository('EMAIL'); - $config->BBCodes->addFromRepository('CODE'); - $config->BBCodes->addFromRepository('QUOTE', 'default', [ - 'authorStr' => ' ' - ]); - $config->BBCodes->addFromRepository('LIST'); - $config->BBCodes->addFromRepository('DEL'); - $config->BBCodes->addFromRepository('COLOR'); - $config->BBCodes->addFromRepository('CENTER'); - $config->BBCodes->addFromRepository('SIZE'); - $config->BBCodes->addFromRepository('*'); - }), + ->render(Render::class) + ->configure(Configure::class), ]; diff --git a/extensions/bbcode/src/Configure.php b/extensions/bbcode/src/Configure.php new file mode 100644 index 0000000000..89eb64d218 --- /dev/null +++ b/extensions/bbcode/src/Configure.php @@ -0,0 +1,59 @@ +addTagsFromRepositories($config); + $this->adaptHighlightJs($config); + } + + protected function addTagsFromRepositories(Configurator $config): void + { + $config->BBCodes->addFromRepository('B'); + $config->BBCodes->addFromRepository('I'); + $config->BBCodes->addFromRepository('U'); + $config->BBCodes->addFromRepository('S'); + $config->BBCodes->addFromRepository('URL'); + $config->BBCodes->addFromRepository('IMG'); + $config->BBCodes->addFromRepository('EMAIL'); + $config->BBCodes->addFromRepository('CODE'); + $config->BBCodes->addFromRepository('QUOTE', 'default', [ + 'authorStr' => ' ' + ]); + $config->BBCodes->addFromRepository('LIST'); + $config->BBCodes->addFromRepository('DEL'); + $config->BBCodes->addFromRepository('COLOR'); + $config->BBCodes->addFromRepository('CENTER'); + $config->BBCodes->addFromRepository('SIZE'); + $config->BBCodes->addFromRepository('*'); + } + + /** + * Fix for highlight JS not working after changing post content. + * + * @link https://github.com/flarum/framework/issues/3794 + */ + protected function adaptHighlightJs(Configurator $config): void + { + $codeTag = $config->tags->get('CODE'); + $script = ' + '; + $codeTag->template = str_replace('', $script.'', $codeTag->template); + } +} diff --git a/extensions/bbcode/src/Render.php b/extensions/bbcode/src/Render.php new file mode 100644 index 0000000000..2a2ed3cc3c --- /dev/null +++ b/extensions/bbcode/src/Render.php @@ -0,0 +1,33 @@ +translator = $translator; + } + + public function __invoke(Renderer $renderer, $context, string $xml): string + { + $renderer->setParameter('L_WROTE', $this->translator->trans('flarum-bbcode.forum.quote.wrote')); + + return $xml; + } +}