2022-01-27 18:09:57 +01:00
|
|
|
> **WARNING:** This bundle is **abandoned** in favor of [markdown_to_html](https://twig.symfony.com/doc/3.x/filters/markdown_to_html.html)
|
|
|
|
> Twig filter. Please, consider using it instead. Migration should be easy because it also supports
|
|
|
|
> `michelf/php-markdown` Markdown parser that is used in this project under the hood.
|
|
|
|
|
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
|
|
|
|
2021-11-08 12:58:03 +01:00
|
|
|
![ci.yml](https://github.com/tacman/KnpMarkdownBundle/actions/workflows/ci.yml/badge.svg)
|
|
|
|
![php.yml](https://github.com/tacman/KnpMarkdownBundle/actions/workflows/php.yml/badge.svg)
|
|
|
|
|
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 }}
|
|
|
|
|
2020-04-15 15:55:14 +02:00
|
|
|
{# If my_data is entered by a user, escape HTML tags before printing it #}
|
|
|
|
{{ my_data|escape|markdown }}
|
|
|
|
{# or strip HTML tags #}
|
|
|
|
{{ my_data|striptags|markdown }}
|
|
|
|
|
2012-10-15 12:24:11 +02:00
|
|
|
{# 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.
|