smith981/knp-markdown-bundle

Knplabs markdown bundle 将 markdown 转换为 html

安装: 107

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 50

类型:symfony-bundle

1.3.2 2014-02-20 11:01 UTC

This package is not auto-updated.

Last update: 2024-09-24 07:49:32 UTC


README

为您的 Symfony2 项目提供 markdown 转换(基于 Michel Fortin 的作品)。

Build Status

安装

将 KnpMarkdownBundle 添加到您的 composer.json

{
    "require": {
        "knplabs/knp-markdown-bundle": "~1.3"
    }
}

app/AppKernel.php 中注册该 Bundle

$bundles = array(
    // ...
    new Knp\Bundle\MarkdownBundle\KnpMarkdownBundle(),
);

使用

// Use the service
$html = $this->container->get('markdown.parser')->transformMarkdown($text);

// Use the helper with default parser
echo $view['markdown']->transform($text);

// Use the helper and a select specific parser
echo $view['markdown']->transform($text, $parserName);

如果您已启用 Twig markdown 过滤器,您可以在您的 Twig 模板中使用以下内容

{# Use default parser #}
{{ my_data|markdown }}

{# Or select specific parser #}
{{ my_data|markdown('parserName') }}

更改解析器实现

创建一个实现 Knp\Bundle\MarkdownBundle\MarkdownParserInterface 的服务,然后配置 Bundle 以使用它

knp_markdown:
    parser:
        service: my.markdown.parser

或者,如果您使用的是 markdown.parser.sundown,有选项可以启用 sundown 扩展和渲染标志,请参阅默认配置

php app/console config:dump-reference knp_markdown

此 Bundle 包含 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 中的实现

测试

phpunit -c myapp vendor/bundles/Knp/Bundle/MarkdownBundle