mirror of
https://github.com/flarum/core.git
synced 2025-07-13 04:46:24 +02:00
fix(bbcode): highlight.js does not work after changing post content (#3817)
* fix(bbcode): highlight.js does not work after changing post content Signed-off-by: Sami Mazouz <sychocouldy@gmail.com> * chore(bbcode): organize bbcode code Signed-off-by: Sami Mazouz <sychocouldy@gmail.com> * Apply fixes from StyleCI --------- Signed-off-by: Sami Mazouz <sychocouldy@gmail.com> Co-authored-by: StyleCI Bot <bot@styleci.io>
This commit is contained in:
@ -21,6 +21,11 @@
|
|||||||
"require": {
|
"require": {
|
||||||
"flarum/core": "^1.7"
|
"flarum/core": "^1.7"
|
||||||
},
|
},
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"Flarum\\BBCode\\": "src"
|
||||||
|
}
|
||||||
|
},
|
||||||
"extra": {
|
"extra": {
|
||||||
"branch-alias": {
|
"branch-alias": {
|
||||||
"dev-main": "1.x-dev"
|
"dev-main": "1.x-dev"
|
||||||
|
@ -7,36 +7,14 @@
|
|||||||
* LICENSE file that was distributed with this source code.
|
* LICENSE file that was distributed with this source code.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
namespace Flarum\BBCode;
|
||||||
|
|
||||||
use Flarum\Extend;
|
use Flarum\Extend;
|
||||||
use s9e\TextFormatter\Configurator;
|
|
||||||
use s9e\TextFormatter\Renderer;
|
|
||||||
|
|
||||||
return [
|
return [
|
||||||
new Extend\Locales(__DIR__.'/locale'),
|
new Extend\Locales(__DIR__.'/locale'),
|
||||||
|
|
||||||
(new Extend\Formatter)
|
(new Extend\Formatter)
|
||||||
->render(function (Renderer $renderer, $context, string $xml) {
|
->render(Render::class)
|
||||||
$renderer->setParameter('L_WROTE', resolve('translator')->trans('flarum-bbcode.forum.quote.wrote'));
|
->configure(Configure::class),
|
||||||
|
|
||||||
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' => '<xsl:value-of select="@author"/> <xsl:value-of select="$L_WROTE"/>'
|
|
||||||
]);
|
|
||||||
$config->BBCodes->addFromRepository('LIST');
|
|
||||||
$config->BBCodes->addFromRepository('DEL');
|
|
||||||
$config->BBCodes->addFromRepository('COLOR');
|
|
||||||
$config->BBCodes->addFromRepository('CENTER');
|
|
||||||
$config->BBCodes->addFromRepository('SIZE');
|
|
||||||
$config->BBCodes->addFromRepository('*');
|
|
||||||
}),
|
|
||||||
];
|
];
|
||||||
|
59
extensions/bbcode/src/Configure.php
Normal file
59
extensions/bbcode/src/Configure.php
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of Flarum.
|
||||||
|
*
|
||||||
|
* For detailed copyright and license information, please view the
|
||||||
|
* LICENSE file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Flarum\BBCode;
|
||||||
|
|
||||||
|
use s9e\TextFormatter\Configurator;
|
||||||
|
|
||||||
|
class Configure
|
||||||
|
{
|
||||||
|
public function __invoke(Configurator $config)
|
||||||
|
{
|
||||||
|
$this->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' => '<xsl:value-of select="@author"/> <xsl:value-of select="$L_WROTE"/>'
|
||||||
|
]);
|
||||||
|
$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 = '
|
||||||
|
<script>
|
||||||
|
if(window.hljsLoader && !document.currentScript.parentNode.hasAttribute(\'data-s9e-livepreview-onupdate\')) {
|
||||||
|
window.hljsLoader.highlightBlocks(document.currentScript.parentNode);
|
||||||
|
}
|
||||||
|
</script>';
|
||||||
|
$codeTag->template = str_replace('</pre>', $script.'</pre>', $codeTag->template);
|
||||||
|
}
|
||||||
|
}
|
33
extensions/bbcode/src/Render.php
Normal file
33
extensions/bbcode/src/Render.php
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of Flarum.
|
||||||
|
*
|
||||||
|
* For detailed copyright and license information, please view the
|
||||||
|
* LICENSE file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Flarum\BBCode;
|
||||||
|
|
||||||
|
use s9e\TextFormatter\Renderer;
|
||||||
|
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||||
|
|
||||||
|
class Render
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var TranslatorInterface
|
||||||
|
*/
|
||||||
|
protected $translator;
|
||||||
|
|
||||||
|
public function __construct(TranslatorInterface $translator)
|
||||||
|
{
|
||||||
|
$this->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;
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user