erusev / parsedown-extra

Markdown Extra Extension for Parsedown

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Parsedown Extra Plugin v1.2.0

taufik-nurrohman opened this issue · comments

Hello, I have just released a new version of Parsedown plugin that compatible with Parsedown Extra 0.8.0-beta-1 with a more generic configuration approach using the element content, attributes and node as callback function parameters to allow modification of the HTML output without having to create new class that depends on the Parsedown class. Hopefully this can solve some of the issues here related to features.

Here are some examples:

require 'Parsedown.php';
require 'ParsedownExtra.php';
require 'ParsedownExtraPlugin.php';

$Parsedown = new ParsedownExtraPlugin;

// Detect external links
$Parsedown->setLinkAttributes(function($Text, $Attributes, &$Element, $Internal) {
    if (isset($Attributes['href'])) {
        if (!$Internal) {
            return array(
                'rel' => 'nofollow',
                'target' => '_blank'
            );
        }
    }
});

// Automatic headers ID
$Parsedown->setHeaderText(function($Text, $Attributes, &$Element, $Level) {
    if (isset($Attributes['id'])) {
        $Id = $Attributes['id'];
    } else {
        $Id = trim(preg_replace('/[^a-z\d]+/', '-', strtolower($Text)), '-');
    }
    return '[#](#' . $Id . ') ' . $Text;
});

$Parsedown->setHeaderAttributes(function($Text, $Attributes, &$Element, $Level) {
    if (isset($Attributes['id'])) {
        $Id = $Attributes['id'];
    } else {
        $Id = trim(preg_replace('/[^a-z\d]+/', '-', strtolower($Text)), '-');
    }
    return array('id' => $Id);
});

// Add border to tables
$Parsedown->setTableAttributes(array('border' => 1));

echo $Parsedown->text(file_get_contents('.\path\to\file.md'));

Let me know if there is anything that needs to be improved. Thanks.