. */ declare(strict_types=1); namespace App\Helpers; use League\HTMLToMarkdown\HtmlConverter; use s9e\TextFormatter\Bundles\Forum as TextFormatter; /** * @see \App\Tests\Helpers\BBCodeToMarkdownConverterTest */ class BBCodeToMarkdownConverter { protected HtmlConverter $html_to_markdown; public function __construct() { $this->html_to_markdown = new HtmlConverter(); } /** * Converts the given BBCode to markdown. * BBCode tags that does not have a markdown aequivalent are outputed as HTML tags. * * @param string $bbcode The Markdown that should be converted * * @return string the markdown version of the text */ public function convert(string $bbcode): string { //Convert BBCode to html $xml = TextFormatter::parse($bbcode); $html = TextFormatter::render($xml); //Now convert the HTML to markdown return $this->html_to_markdown->convert($html); } }