Added an console command to convert the old BBCode comments to markdown

This commit is contained in:
Jan Böhmer 2019-10-13 00:32:09 +02:00
parent c814bae3af
commit 8cfaee5c62
9 changed files with 427 additions and 95 deletions

View file

@ -31,9 +31,7 @@
namespace App\Services;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Cache\ItemInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* This class allows you to convert markdown text to HTML.
@ -41,52 +39,26 @@ use Symfony\Contracts\Cache\ItemInterface;
*/
class MarkdownParser
{
protected $cache;
/** @var \Parsedown */
protected $parsedown;
protected $translator;
public function __construct(CacheInterface $cache)
public function __construct(TranslatorInterface $translator)
{
$this->cache = $cache;
$this->initParsedown();
}
protected function initParsedown()
{
$this->parsedown = new \Parsedown();
$this->parsedown->setSafeMode(true);
$this->translator = $translator;
}
/**
* Converts the given markdown text to HTML.
* The result is cached.
* Mark the markdown for rendering.
* The rendering of markdown is done on client side
* @param string $markdown The markdown text that should be parsed to html.
* @param bool $inline_mode Only allow inline markdown codes like (*bold* or **italic**), not something like tables
* @return string The HTML version of the given text.
* @throws \Psr\Cache\InvalidArgumentException
* @return string The markdown in a version that can be parsed on client side.
*/
public function parse(string $markdown, bool $inline_mode = false) : string
public function markForRendering(string $markdown, bool $inline_mode = false) : string
{
return sprintf(
'<div class="markdown" data-markdown="%s">Markdown loading...</div>',
htmlspecialchars($markdown)
'<div class="markdown" data-markdown="%s">%s</div>',
htmlspecialchars($markdown),
$this->translator->trans('markdown.loading')
);
//Generate key
/*if ($inline_mode) {
$key = 'markdown_i_' . md5($markdown);
} else {
$key = 'markdown_' . md5($markdown);
}
return $this->cache->get($key, function (ItemInterface $item) use ($markdown, $inline_mode) {
//Expire text after 2 months
$item->expiresAfter(311040000);
if ($inline_mode) {
return $this->parsedown->line($markdown);
}
return '<div class="markdown">' . $this->parsedown->text($markdown) . '</div>';
});*/
}
}