erusev / parsedown

Better Markdown Parser in PHP
https://parsedown.org
MIT License
14.74k stars 1.12k forks source link

Added a hook to change blocks on-the-fly #715

Closed scorninpc closed 2 years ago

scorninpc commented 5 years ago

I see alot issue about change text and block style, like a Geshi or new styles

I see it's confuse create Extras for every block we need to change. I'm big fan of hooks, so, I implemented something like that to do any change on-the-fly.

Something like that:

$Parsedown = new Parsedown();

// Hook for change element blocks
$Parsedown->setTextHook(function($Element) {

    // Look for code block
    if($Element['name'] === "code") {
        $geshi = new Geshi($Element['text'], str_replace("language-", "", $Element['attributes']['class']), "/media/backup/www/andor.com.br/library/General/Library/Geshi/geshi/");
        $geshi->set_header_type(GESHI_HEADER_NONE);
        unset($Element['text']);

        $Element['rawHtml'] = $geshi->parse_code();
        $Element['allowRawHtmlInSafeMode'] = TRUE;
    }

    // ANOTHER ONE TO CHANGE
    elseif (1) {

    }

    // Return the same element block
    return $Element;
});

I don't know if you like or it's can be useful for you. But, was already coded!

onelikeandidie commented 1 year ago

This sounds like a great feature to have, I also wanted to hook into Parsedown without modifying it directly to add some extra features. Shame this was request was closed!