Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added MarkdownExtra support #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,37 @@

class Module extends \Zend\View\Helper\AbstractHelper
{
const MARKDOWN_NORMAL = 'normal';
const MARKDOWN_EXTRA = 'extra';

protected $classWhitelist = array(
self::MARKDOWN_NORMAL => '\Michelf\Markdown',
self::MARKDOWN_EXTRA => '\Michelf\MarkdownExtra'
);

protected static $defaultType = self::MARKDOWN_NORMAL;

public function getViewHelperConfig()
{
return array('services' => array('markdown' => $this));
}

public function __invoke($string = null)
public function __invoke($string = null, $type = null)
{
if (!class_exists('Michelf\Markdown')) require_once __DIR__ . '/vendor/php-markdown/Michelf/Markdown.inc.php';
return \Michelf\Markdown::defaultTransform($string);
if (null == $type) {
$type = self::$defaultType;
}

$className = $this->classWhitelist[$type];
if (!class_exists($className)) {
$fileName = str_replace('\\', '/', $className) . '.inc.php';
require_once __DIR__ . '/vendor/php-markdown' . $fileName;
}
return $className::defaultTransform($string);
}

public static function setDefaultType($type)
{
self::$defaultType = $type;
}
}
23 changes: 19 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,29 @@ To install EdpMarkdown, simply recursively clone this repository (`git clone
With this module installed, using Markdown in your view scripts is easy:

```php
<?= $this->markdown('Hello, **this** is _Markdown_!'); ?>
<?php echo $this->markdown('Hello, **this** is _Markdown_!'); ?>
```

**NOTE:** For security purposes, the output **SHOULD** be [sanitized](http://htmlpurifier.org/) if the Markdown is from an untrusted source. ([@padraic](https://github.com/padraic) says so!) See the [Markdown documentation on inline HTML](http://daringfireball.net/projects/markdown/syntax#html) to understand why this is necessary.
You may also use MarkdownExtra with this module:

```php
<?php echo $this->markdown('Hello, **this** is <strong>MarkdownExtra</strong>!', \EdpMarkdown\Module::MARKDOWN_EXTRA); ?>
```

or simpler:

```php
<?php echo $this->markdown('Hello, **this** is <strong>MarkdownExtra</strong>!', 'extra'); ?>
```

If you want all your calls to the markdown-helper to use MarkdownExtra, you can setup the default Markdown-type as follows:

## Configuration
```php
// For example in your onBootsrap-method:
\EdpMarkdown\Module::setDefaultType(\EdpMarkdown\Module::MARKDOWN_EXTRA);
```

TODO: Create simple way to toggle to the 'extra' parser.
**NOTE:** For security purposes, the output **SHOULD** be [sanitized](http://htmlpurifier.org/) if the Markdown is from an untrusted source. ([@padraic](https://github.com/padraic) says so!) See the [Markdown documentation on inline HTML](http://daringfireball.net/projects/markdown/syntax#html) to understand why this is necessary.

## License

Expand Down