2018-01-25 21:19:16 +01:00
|
|
|
Provide markdown conversion (based on [Michel Fortin work](https://github.com/michelf/php-markdown)) to your Symfony projects.
|
2011-04-07 08:35:00 +02:00
|
|
|
|
2017-06-22 08:14:08 +02:00
|
|
|
[![Build Status](https://secure.travis-ci.org/KnpLabs/KnpMarkdownBundle.svg)](http://travis-ci.org/KnpLabs/KnpMarkdownBundle)
|
2011-11-19 15:48:33 +01:00
|
|
|
|
2010-05-05 18:38:36 +02:00
|
|
|
## INSTALLATION
|
|
|
|
|
2016-05-04 20:39:37 +02:00
|
|
|
Add KnpMarkdownBundle to your project via [Composer](https://getcomposer.org/):
|
2011-08-25 02:54:09 +02:00
|
|
|
|
2012-10-15 12:24:11 +02:00
|
|
|
```
|
2016-05-04 20:39:37 +02:00
|
|
|
composer require knplabs/knp-markdown-bundle
|
|
|
|
```
|
|
|
|
|
2017-11-29 19:29:54 +01:00
|
|
|
If you're *not* using Symfony Flex, you will also need to enable
|
|
|
|
the bundle in your `app/AppKernel.php` file
|
|
|
|
(`new Knp\Bundle\MarkdownBundle\KnpMarkdownBundle()`).
|
2011-08-25 02:54:09 +02:00
|
|
|
|
2017-11-29 19:29:54 +01:00
|
|
|
That's it! Start using it!
|
2010-05-05 18:38:36 +02:00
|
|
|
|
|
|
|
## USAGE
|
|
|
|
|
2017-11-29 19:29:54 +01:00
|
|
|
Once the bundle is installed, you can autowire a `MarkdownParserInterface`
|
|
|
|
into any service or controller:
|
2010-05-06 15:20:04 +02:00
|
|
|
|
2017-11-29 19:29:54 +01:00
|
|
|
```php
|
|
|
|
use Knp\Bundle\MarkdownBundle\MarkdownParserInterface;
|
|
|
|
|
|
|
|
// from inside a controller
|
|
|
|
public function index(MarkdownParserInterface $parser)
|
|
|
|
{
|
|
|
|
$html = $parser->transformMarkdown($text);
|
|
|
|
}
|
|
|
|
|
|
|
|
// or from inside a service
|
|
|
|
private $parser;
|
|
|
|
|
|
|
|
public function __construct(MarkdownParserInterface $parser)
|
|
|
|
{
|
|
|
|
$this->parser = $parser;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function someMethod()
|
|
|
|
{
|
2018-10-31 10:11:57 +01:00
|
|
|
$html = $this->parser->transformMarkdown($text);
|
2017-11-29 19:29:54 +01:00
|
|
|
}
|
2012-10-15 12:24:11 +02:00
|
|
|
```
|
2010-07-22 12:06:54 +02:00
|
|
|
|
2017-11-29 19:29:54 +01:00
|
|
|
There is also a public `markdown.parser` service you can use.
|
|
|
|
|
|
|
|
In Twig, you can use the `markdown` filter:
|
2010-09-19 21:05:36 +02:00
|
|
|
|
2012-10-15 12:24:11 +02:00
|
|
|
```twig
|
|
|
|
{# Use default parser #}
|
|
|
|
{{ my_data|markdown }}
|
|
|
|
|
|
|
|
{# Or select specific parser #}
|
|
|
|
{{ my_data|markdown('parserName') }}
|
|
|
|
```
|
2010-09-19 21:05:36 +02:00
|
|
|
|
2011-04-07 08:35:00 +02:00
|
|
|
## Change the parser implementation
|
|
|
|
|
2012-10-15 12:24:11 +02:00
|
|
|
Create a service implementing `Knp\Bundle\MarkdownBundle\MarkdownParserInterface`,
|
2011-04-07 08:35:00 +02:00
|
|
|
then configure the bundle to use it:
|
|
|
|
|
2012-10-15 12:24:11 +02:00
|
|
|
```yaml
|
2018-01-25 18:39:06 +01:00
|
|
|
# Symfony 3: app/config/config.yml
|
|
|
|
# Symfony 4: config/packages/knp_markdown.yaml (you'll need to create this)
|
2012-10-15 12:24:11 +02:00
|
|
|
knp_markdown:
|
|
|
|
parser:
|
|
|
|
service: my.markdown.parser
|
|
|
|
```
|
2011-04-07 08:35:00 +02:00
|
|
|
|
2012-10-10 18:01:45 +02:00
|
|
|
Alternatively if you are using the ``markdown.parser.sundown`` there are
|
|
|
|
options for enabling sundown extensions and render flags, see the
|
|
|
|
default Configuration with:
|
|
|
|
|
2018-01-23 17:31:33 +01:00
|
|
|
php bin/console config:dump-reference knp_markdown
|
2012-10-10 18:01:45 +02:00
|
|
|
|
|
|
|
This bundle comes with 5 parser services, 4 based on the same algorithm
|
|
|
|
but providing different levels of compliance to the markdown specification,
|
|
|
|
and one which is uses the php sundown extension:
|
2011-04-07 08:35:00 +02:00
|
|
|
|
2012-10-15 12:24:11 +02:00
|
|
|
- markdown.parser.max // fully compliant = slower (default implementation)
|
|
|
|
- markdown.parser.medium // expensive and uncommon features dropped
|
|
|
|
- markdown.parser.light // expensive features dropped
|
|
|
|
- markdown.parser.min // most features dropped = faster
|
|
|
|
- markdown.parser.sundown // faster and fully compliant (recommended)
|
2012-10-10 18:01:45 +02:00
|
|
|
|
2012-10-16 10:52:58 +02:00
|
|
|
``markdown.parser.sundown`` requires the [php sundown extension](https://github.com/chobie/php-sundown).
|
2011-04-07 08:35:00 +02:00
|
|
|
|
|
|
|
For more details, see the implementations in Parser/Preset.
|