. */ declare(strict_types=1); namespace App\Services\Formatters; use Symfony\Contracts\Translation\TranslatorInterface; /** * This class allows you to convert markdown text to HTML. */ class MarkdownParser { protected TranslatorInterface $translator; public function __construct(TranslatorInterface $translator) { $this->translator = $translator; } /** * 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 When true, p blocks will have no margins behind them * * @return string the markdown in a version that can be parsed on client side */ public function markForRendering(string $markdown, bool $inline_mode = false): string { return sprintf( '
%s
', $inline_mode ? 'markdown-inline' : '', //Add class if inline mode is enabled, to prevent margin after p htmlspecialchars($markdown), $this->translator->trans('markdown.loading') ); } }