From 8081e9b90ce34ea060bf9f051436c9cca63e2c8d Mon Sep 17 00:00:00 2001 From: Sami Mazouz Date: Sun, 30 Apr 2023 19:16:29 +0100 Subject: [PATCH 1/3] fix(bbcode): highlight.js does not work after changing post content Signed-off-by: Sami Mazouz --- extensions/bbcode/extend.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/extensions/bbcode/extend.php b/extensions/bbcode/extend.php index 325d7b4185..16a49d9320 100644 --- a/extensions/bbcode/extend.php +++ b/extensions/bbcode/extend.php @@ -38,5 +38,15 @@ $config->BBCodes->addFromRepository('CENTER'); $config->BBCodes->addFromRepository('SIZE'); $config->BBCodes->addFromRepository('*'); + + // Fix for highlight JS not working after changing post content. + $codeTag = $config->tags->get('CODE'); + $script = ' + '; + $codeTag->template = str_replace('', $script.'', $codeTag->template); }), ]; From b0f3635fa2642896fc3b522294b3bd4cfa20c845 Mon Sep 17 00:00:00 2001 From: Sami Mazouz Date: Sun, 30 Apr 2023 19:26:52 +0100 Subject: [PATCH 2/3] chore(bbcode): organize bbcode code Signed-off-by: Sami Mazouz --- extensions/bbcode/composer.json | 5 +++ extensions/bbcode/extend.php | 40 +++------------------- extensions/bbcode/src/Configure.php | 52 +++++++++++++++++++++++++++++ extensions/bbcode/src/Render.php | 26 +++++++++++++++ 4 files changed, 87 insertions(+), 36 deletions(-) create mode 100644 extensions/bbcode/src/Configure.php create mode 100644 extensions/bbcode/src/Render.php 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 16a49d9320..36dec929b8 100644 --- a/extensions/bbcode/extend.php +++ b/extensions/bbcode/extend.php @@ -7,46 +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('*'); - - // Fix for highlight JS not working after changing post content. - $codeTag = $config->tags->get('CODE'); - $script = ' - '; - $codeTag->template = str_replace('', $script.'', $codeTag->template); - }), + ->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..aa20ec2680 --- /dev/null +++ b/extensions/bbcode/src/Configure.php @@ -0,0 +1,52 @@ +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..6e94c7fda4 --- /dev/null +++ b/extensions/bbcode/src/Render.php @@ -0,0 +1,26 @@ +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; + } +} From 8770cea7d5c9e5f125d29efc7353230b832bd56d Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Sun, 30 Apr 2023 18:27:56 +0000 Subject: [PATCH 3/3] Apply fixes from StyleCI --- extensions/bbcode/src/Configure.php | 7 +++++++ extensions/bbcode/src/Render.php | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/extensions/bbcode/src/Configure.php b/extensions/bbcode/src/Configure.php index aa20ec2680..89eb64d218 100644 --- a/extensions/bbcode/src/Configure.php +++ b/extensions/bbcode/src/Configure.php @@ -1,5 +1,12 @@