mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-07-10 18:34:32 +02:00
Render Comment and description as BBCode.
This commit is contained in:
parent
daad7ec60a
commit
3da6b03b23
7 changed files with 114 additions and 8 deletions
|
@ -34,22 +34,28 @@ namespace App\Twig;
|
|||
|
||||
use App\Entity\DBElement;
|
||||
use App\Services\EntityURLGenerator;
|
||||
use Symfony\Component\Cache\Adapter\AdapterInterface;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFilter;
|
||||
|
||||
use s9e\TextFormatter\Bundles\Forum as TextFormatter;
|
||||
|
||||
class AppExtension extends AbstractExtension
|
||||
{
|
||||
protected $entityURLGenerator;
|
||||
protected $cache;
|
||||
|
||||
public function __construct(EntityURLGenerator $entityURLGenerator)
|
||||
public function __construct(EntityURLGenerator $entityURLGenerator, AdapterInterface $cache)
|
||||
{
|
||||
$this->entityURLGenerator = $entityURLGenerator;
|
||||
$this->cache = $cache;
|
||||
}
|
||||
|
||||
public function getFilters()
|
||||
{
|
||||
return [
|
||||
new TwigFilter('entityURL', [$this, 'generateEntityURL'])
|
||||
new TwigFilter('entityURL', [$this, 'generateEntityURL']),
|
||||
new TwigFilter('bbCode', [$this, 'parseBBCode'], ['pre_escape' => 'html', 'is_safe' => ['html']])
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -67,4 +73,18 @@ class AppExtension extends AbstractExtension
|
|||
throw new \InvalidArgumentException('method is not supported!');
|
||||
}
|
||||
|
||||
public function parseBBCode(string $bbcode) : string
|
||||
{
|
||||
if($bbcode === '') return '';
|
||||
|
||||
$item = $this->cache->getItem('bbcode_' . md5($bbcode));
|
||||
if(!$item->isHit()) {
|
||||
$xml = TextFormatter::parse($bbcode);
|
||||
$item->set(TextFormatter::render($xml));
|
||||
$this->cache->save($item);
|
||||
}
|
||||
|
||||
return $item->get();
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue