Skip to content

webuni/commonmark-twig-renderer

Repository files navigation

CommonMark Twig Renderer

Latest Stable Version Build Status Scrutinizer Code Quality SensioLabsInsight

This extension allows you to use Twig templates to render CommonMark document.

Installation

This project can be installed via Composer:

composer require webuni/commonmark-twig-renderer

Usage

    use League\CommonMark\CommonMarkConverter;
    use Webuni\CommonMark\TwigRenderer\CommonMarkTwig;

    $converter = new CommonMarkConverter();
    CommonMarkTwig::setTwigRenderer($converter);

    echo $converter->convertToHtml('# Hello World!');

Template customization

In Twig, each Commonmark Node is represented by a Twig block. To customize any part of how a node renders, you just need to override the appropriate block.

{% extends 'commonmark.html.twig' %}

{% block header -%}
    {% set content = block('_inline_children') -%}
    <h{{ node.level }}><a name="{{ content|striptags|lower }}"></a>{{ content|raw }}</h{{ node.level }}>
{%- endblock %}