承接 pomodocs/commonmark-template-renderer 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

pomodocs/commonmark-template-renderer

Composer 安装命令:

composer require pomodocs/commonmark-template-renderer

包简介

CommonMark extension to render html via template engines

README 文档

README

Render your markdown by your favorite template library

Test Suite Static Analysis Maintainability Code Coverage GitHub

CommonMark Template Renderer Extension is an extension for League CommonmMark to render the html elements via your favorite template engine.

Warning

At the moment, we support only Twig. We scheduled Latte, Blade and Plates support to version 1.0

Why render html elements via templates?

The answer is simple: easy html customization. And it's the only reason to use this library. In fact, the defaul templates generate exactly the same output as standard renderers. But template engines introduce some complexity and a little overhead so if you don't customize your html you don't need to use this library.

Installation

Install the extension via Composer:

composer require pomodocs/commonmark-template-renderer

Basic Usage

After installing it, register the extension in your Commonmark Environment:

<?php

require __DIR__ . '/vendor/autoload.php';

use League\CommonMark\Environment\Environment;
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
use PomoDocs\CommonMark\TemplateRenderer\TemplateConverter;
use PomoDocs\CommonMark\TemplateRenderer\TemplateRendererExtension;

$config = [
    'templateRenderer' => [
        'engine' => 'twig',
        'templates_dirs' => [
            '/my/templates/dir',
            '/another/templates/dir',
        ],
    ]
];

$environment = new Environment($config);
$environment->addExtension(new CommonMarkCoreExtension());
$environment->addExtension(.....); // add your favorite extensions
$environment->addExtension(new TemplateRendrerExtension());

$converter = new TemplateConverter($environment);

Now you're ready to convert your html.

Configuration

You can configure the library by passing an array with templateRenderer key to the Environment object:

<?php

use League\CommonMark\Environment\Environment;


$config = [
    'templateRenderer' => [
        'engine' => 'twig',
        'templates_dirs' => [
            '/my/templates/dir',
            '/another/templates/dir',
        ],
    ]
];

$environment = new Environment($config);

Let's explain the configuration keys:

  • engine: the template engine by which we render our templates. Supported engines are: twig, latte, blade, plates. Default to twig.
  • templates_dirs: an array of directories where the engine looks for the templates. The dafult directory, containing the templates shipped by the library, is automatically included.

Customize your html

Let's introduce the goal of this library: easy customization of your html fragments. We try to explain it via a simple example.

Let's suppose we wanto to use Bulma css framework and we want to style headings nodes by Bulma title class. When we write the following markdown:

# Title 1
## Title 2
### Title 3

our resulting html should be:

<h1 class="title is-1">Title 1</h1>
<h2 class="title is-2">Title 2</h2>
<h3 class="title is-3">Title 3</h3>

Every node is rendered by a small template. You can see the full templates list in https://github.com/pomodocs/commonmark-template-renderer/tree/master/resources/templates/default/twig.

Heading node is rendered via heading.html.twig template so we create a directory where our custom template resides (i.e. resources/templates) and create the heading.html.twig file. For the first shot, you can copy-paste https://github.com/pomodocs/commonmark-template-renderer/blob/master/resources/templates/default/twig/heading.html.twig.

The content of the copied file is the following:

<h{{ node.level }}{{ node|render_attributes }}>{{ node|render_children }}</h{{ node.level }}>

Our engine passes two variables to the templates: node and configuration.

The content of our custom template resources\templates\heading.html.twig could be the following:

<h{{ node.level }} class="title is-{{ node.level }}>{{ node|render_children }}</h{{ node.level }}>

Important

This library ships two filters we can apply to node objects:

  • render_children filter renders all the children of the given node.
  • render_attributes filter renders all the attributes (class, name, type etc.) of the given node.

Now, you can convert your markdown into html:

<?php

use League\CommonMark\Environment\Environment;
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
use PomoDocs\CommonMark\TemplateRenderer\TemplateConverter;
use PomoDocs\CommonMark\TemplateRenderer\TemplateRendererExtension;

$config = [
    'templateRenderer' => [
        'engine' => 'twig',
        'templates_dirs' => [
            __DIR__ . '/resources/templates',
        ],
    ]
];

$environment = new Environment($config);
$environment->addExtension(new CommonMarkCoreExtension());
$environment->addExtension(new TemplateRendrerExtension());

$converter = new TemplateConverter($environment);

$converter->convert("
# Title 1
## Title 2
### Title 3"
);

// The result is:
// <h1 class="title is-1">Title 1</h1>
// <h2 class="title is-2">Title 2</h2>
// <h3 class="title is-3">Title 3</h3>

Issues

Please, open an issue on Github repository.

Contributing

Please, see our Contributing guide.

Licensing

This library is released under MIT license.

pomodocs/commonmark-template-renderer 适用场景与选型建议

pomodocs/commonmark-template-renderer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 04 月 24 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 pomodocs/commonmark-template-renderer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 pomodocs/commonmark-template-renderer 我们能提供哪些服务?
定制开发 / 二次开发

基于 pomodocs/commonmark-template-renderer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 33
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-04-24