northernco/markdown-bundle

此包的最新版本(2.1.1)没有可用的许可证信息。

Symfony 扩展包,用于将 markdown 转换为 html

安装次数: 10,494

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 3

分支: 0

公开问题: 0

类型:symfony-bundle

2.1.1 2023-04-26 13:28 UTC

This package is auto-updated.

Last update: 2024-08-26 16:31:03 UTC


README

安装

确保已全局安装 Composer,如 Composer 文档中的 安装章节 所述。

使用 Symfony Flex 的应用程序

打开命令行,进入您的项目目录,执行以下命令

$ composer require northernco/markdown-bundle

未使用 Symfony Flex 的应用程序

步骤 1: 下载 Bundle

打开命令行,进入您的项目目录,并执行以下命令以下载此 Bundle 的最新稳定版本

$ composer require northernco/markdown-bundle

步骤 2: 启用 Bundle

然后,将 Bundle 添加到项目的 config/bundles.php 文件中注册的 Bundle 列表中以启用它

// config/bundles.php

return [
    // ...
    Northern\MarkdownBundle\NorthernMarkdownBundle::class => ['all' => true],
];

使用方法

安装 Bundle 后,您可以将 MarkdownRepositoryInterfaceMarkdownParserInterface 自动注入到任何服务或控制器中。建议使用 MarkdownRepositoryInterface,因为它将缓存结果,使后续调用更快。

示例

use Northern\MarkdownBundle\Service\MarkdownParserInterface;
use Northern\MarkdownBundle\Service\MarkdownRepositoryInterface;

class Service {
    private $parser;

    private $repository;

    public function __construct(
        MarkdownParserInterface $parser,
        MarkdownRepositoryInterface $repository
    ) {
        $this->parser     = $parser;
        $this->repository = $repository;
    }

    public function someMethod()
    {
        $text = '# Test';

        // Converts the markdown
        $html = $this->parser->convertMarkdownToHtml($text);
        // or convert and cache the markdown
        $html = $this->repository->getHtmlFromMarkdown($text);
    }
}

在 Twig 中,您可以使用 md2html 过滤器

{{ markdown_string|md2html }}