Skip to content
Christian Tietze edited this page Mar 5, 2024 · 19 revisions

Disable Element

This example disables Header elements. That is, both Atx Header and Setext Header elements.

class Extension extends Parsedown
{
    protected function blockHeader($Line)
    {
        return;
    }

    protected function blockSetextHeader($Line)
    {
        return;
    }
}

You can use the same approach for other elements types. This includes inline element types. For example, to disable Image elements you should override inlineImage.

Note that to disable all headers you should also disable support for HTML. You can do that by setting the MarkupEscaped option to true.

Change Element Markup

This example prepends a base path to the src of Image elements.

class Extension extends Parsedown
{
    private $baseImagePath = 'http://cdn.example.com/';

    protected function inlineImage($Excerpt)
    {
        $Image = parent::inlineImage($Excerpt);

        $Image['element']['attributes']['src'] = $this->baseImagePath . $Image['element']['attributes']['src'];

        return $Image;
    }
}

You can use the same approach for other element types.

Add Inline Element

This example adds a ColoredText element. You can find a description of this element at https://github.com/erusev/parsedown/issues/262.

class Extension extends Parsedown
{
    function __construct()
    {
        $this->InlineTypes['{'] []= 'ColoredText';

        $this->inlineMarkerList .= '{';
    }

    protected function inlineColoredText($Excerpt)
    {
        if (preg_match('/^{c:([#\w]\w+)}([^{]+){\/c}/', $Excerpt['text'], $matches))
        {
            return array(
                'extent' => strlen($matches[0]),
                'element' => array(
                    'name' => 'span',
                    'text' => $matches[2],
                    'attributes' => array(
                        'style' => 'color: '.$matches[1],
                    ),
                ),
            );
        }
    }
}

You can use the same approach for other element types.