Skip to content

Tutorial: Create Extensions

Pavel Kácha edited this page Jul 16, 2016 · 4 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:/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.

Add Multi-Line Element

This example extends the bold element (**) to function on multiple lines.

class Extension extends Parsedown
{

the array index is the first character of the pattern to match. the second array is all the functions that should be called when the character is found. leave out the block or inline part of the function name.

    function __construct()
    {
        $this->BlockTypes['*'] []= 'BoldText'; 
    }

    protected function blockBoldText($Line,$Block)
    {
        if (preg_match('/^\*\*/', $Line['text'], $matches))
        {
            return array(
                'char' => $Line['text'][0],
                'element' => array(
                    'name' => 'strong',
                ),
                'text' => ''
            );
        }
    } 

Adding the continue to the end of the function will cause this function to be caused on the next line after the start of the block and every proceeding line until

    protected function blockBoldTextContinue($Line,$Block)
    {
        if (isset($Block['complete']))
        {
            return;
        }
        // A blank newline has occurred
        if (isset($Block['interrupted']))
        {
            $Block['element']['text'] .= "\n";
            unset($Block['interrupted']);
        }
        //Check for end of the block. 
        if (preg_match('/\*\*/', $Line['text']))
        {
            $Block['element']['text'] = substr($Block['element']['text'], 1);

This will flag the block as complete. The continue function will not be called again. the complete function will be called instead

            $Block['complete'] = true;
            return $Block;
        }
        
        $Block['element']['text'] .= "\n".$Line['body'];
        
        return $Block;
    }
}

Adding Complete to the end of the function name will cause this function to be called when the block is marked as complete.

    protected function blockBoldTextComplete($block)
    {
        return $block;
    }