knplabs / knp-markdown-bundle
1.10.0
2022-01-26 18:46 UTC
Requires
- php: ^7.4|^8.0
- michelf/php-markdown: ^1.9
- symfony/dependency-injection: ^4.4|^5.0|^6.0
- symfony/framework-bundle: ^4.4|^5.0|^6.0
Requires (Dev)
- phpstan/phpstan: ^1.2
- phpstan/phpstan-symfony: ^1.0
- symfony/phpunit-bridge: ^4.4.11|^5.0|^6.0
- symfony/templating: ^4.4|^5.0|^6.0
Suggests
- ext-sundown: to use optional support for php-sundown extension instead of php implementation
- symfony/twig-bundle: to use the Twig markdown filter
README
警告: 此包已被弃用,推荐使用 markdown_to_html Twig 过滤器。请考虑使用它。迁移应该很容易,因为它也支持
michelf/php-markdown
Markdown 解析器,该解析器在此项目中作为底层使用。
为您的 Symfony 项目提供基于 Michel Fortin 的工作 的 markdown 转换。
安装
通过 Composer 将 KnpMarkdownBundle 添加到您的项目中
composer require knplabs/knp-markdown-bundle
如果您未使用 Symfony Flex,您还需要在 app/AppKernel.php
文件中启用该包(new Knp\Bundle\MarkdownBundle\KnpMarkdownBundle()
)。
就这些!开始使用吧!
使用
安装包后,您可以在任何服务或控制器中将 MarkdownParserInterface
自动装配
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() { $html = $this->parser->transformMarkdown($text); }
还有一个公开的 markdown.parser
服务可供使用。
在 Twig 中,您可以使用 markdown
过滤器
{# Use default parser #} {{ my_data|markdown }} {# 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 }} {# Or select specific parser #} {{ my_data|markdown('parserName') }}
更改解析器实现
创建一个实现 Knp\Bundle\MarkdownBundle\MarkdownParserInterface
的服务,然后配置包以使用它
# Symfony 3: app/config/config.yml # Symfony 4: config/packages/knp_markdown.yaml (you'll need to create this) knp_markdown: parser: service: my.markdown.parser
如果您正在使用 markdown.parser.sundown
,则可以启用 sundown 扩展和渲染标志的选项,请参阅默认配置
php bin/console config:dump-reference knp_markdown
此包包含 5 个解析器服务,4 个基于相同的算法,但提供不同级别的 markdown 规范兼容性,以及一个使用 php sundown 扩展的服务
- 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)
markdown.parser.sundown
需要 php sundown 扩展。
有关更多详细信息,请参阅 Parser/Preset 中的实现。