avensome/commonmark-bundle

将 CommonMark 集成到 Symfony 4 应用中。

安装次数: 3,082

依赖: 1

建议者: 0

安全性: 0

星标: 3

关注者: 3

分支: 2

公开问题: 0

类型:symfony-bundle

0.1.1 2018-05-05 18:12 UTC

This package is auto-updated.

Last update: 2024-09-20 12:00:11 UTC


README

提供 CommonMark 与 Symfony 和 Twig 的无缝集成。

需要 PHP 7.0+ 和 Symfony 4.0+。

安装

使用 Composer 安装

composer require avensome/commonmark-bundle

然后,将 Avensome\CommonMarkBundle\AvensomeCommonMarkBundle 添加到 config/bundles.php

<?php

return [
    // ...
    Avensome\CommonMarkBundle\AvensomeCommonMarkBundle::class => ['all' => true],
];

在服务中使用

CommonMarkConverter 是一个可注入的服务

<?php
use League\CommonMark\CommonMarkConverter;

class MyService
{
    public function __construct(CommonMarkConverter $converter)
    {
        // Do something with $converter
        // https://github.com/thephpleague/commonmark#basic-usage
    }
}

在 Twig 中使用

在 Twig 中,可用 markdown 过滤器和标签。

{{ '# This string will be turned into HTML' | markdown }}

{% markdown %}
Contents of *these tags* will become HTML!

- Nunquam locus lanista.
- Neuter, barbatus solems aegre prensionem de secundus, salvus galatae.
- Rumor moris, tanquam castus verpa.
{% endmarkdown %}

配置

CommonMarkConverter 接受配置作为其参数之一。使用此包,您可以在 Symfony 配置中调整这些配置(例如 config.yaml 或类似)。只需添加 avensome_commonmark 键,其下的一切都将直接传递给转换器。

# ...

avensome_commonmark:
    html_input: allow
    allow_unsafe_links: true

扩展

要启用 CommonMark 扩展,只需将其注册为服务,并标记为 avensome_commonmark.extension

例如,要启用 WebUni Table Extension,请安装它并编辑您的 services.yaml

services:
    # ...
    
    Webuni\CommonMark\TableExtension\TableExtension:
        tags:
            - name: avensome_commonmark.extension