Skip to content

Latest commit

 

History

History
49 lines (38 loc) · 1.94 KB

Extending-Parsedown.md

File metadata and controls

49 lines (38 loc) · 1.94 KB

Extending Parsedown

Markdown pages uses Parsedown and MetaParsedown as the markdown parser. You can use Parsedown extension feature to add your own elements.

Listening to the onMarkdownInitialized event

In your sprinkle Bootstrapper class, you'll need to listen for the onMarkdownInitialized event. This event is fired once the base markdown parser is setup and allows you to add your custom elements to the parser.

For example :

namespace UserFrosting\Sprinkle\Site;

use RocketTheme\Toolbox\Event\Event;
use UserFrosting\Sprinkle\Site\SomeRandomStaticClass;
use UserFrosting\System\Sprinkle\Sprinkle;

class Site extends Sprinkle
{
    /**
     * Defines which events in the UF lifecycle our Sprinkle should hook into.
     */
    public static function getSubscribedEvents()
    {
        return [
            'onMarkdownInitialized' => ['onMarkdownInitialized', 0]
        ];
    }

    /**
     * Adds custom markdown elements
     */
    public function onMarkdownInitialized(Event $event)
    {
        $markdown = $event['markdown'];

        $markdown->addBlockType('!', 'Notices', true, false);

        $markdown->blockNotices = function($Line) {
            // ...
        };
        $markdown->blockNoticesContinue = function($Line, array $Block) {
            // ...
        };
    }
}

The $markdown variable will contain the markdown parser and addBlockType or addInlineType methods can be used to register new block or inline elements. At this point, refer to Parsedown documentation on how to setup your own markdown element. Only difference is element functions should be added as variable instead of declared as functions.