Bolt\Twig\Handler\HtmlHandler::markdown PHP Method

markdown() public method

Formats the given string as Markdown in HTML.
public markdown ( string $content ) : string
$content string
return string Markdown output
    public function markdown($content)
    {
        // Parse the field as Markdown, return HTML
        $output = $this->app['markdown']->text($content);
        $config = $this->app['config']->get('general/htmlcleaner');
        $allowed_tags = !empty($config['allowed_tags']) ? $config['allowed_tags'] : ['div', 'p', 'br', 'hr', 's', 'u', 'strong', 'em', 'i', 'b', 'li', 'ul', 'ol', 'blockquote', 'pre', 'code', 'tt', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'dd', 'dl', 'dh', 'table', 'tbody', 'thead', 'tfoot', 'th', 'td', 'tr', 'a', 'img'];
        $allowed_attributes = !empty($config['allowed_attributes']) ? $config['allowed_attributes'] : ['id', 'class', 'name', 'value', 'href', 'src'];
        // Sanitize/clean the HTML.
        $maid = new Maid(['output-format' => 'html', 'allowed-tags' => $allowed_tags, 'allowed-attribs' => $allowed_attributes]);
        $output = $maid->clean($output);
        return $output;
    }

Usage Example

Example #1
0
    public function testMarkdown()
    {
        $app = $this->getApp();
        $handler = new HtmlHandler($app);
        $markdown = <<<MARKDOWN
# Episode IV
## A New Hope
It is a period of refactor war.
* BPFL
MARKDOWN;
        $html = <<<HTML
<h1>Episode IV</h1>
<h2>A New Hope</h2>
<p>It is a period of refactor war.</p>
<ul>
<li>BPFL</li>
</ul>
HTML;
        $result = $handler->markdown($markdown);
        $this->assertSame($html, $result);
    }