• Stars
    star
    172
  • Rank 220,382 (Top 5 %)
  • Language
    PHP
  • Created almost 12 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Twig extension to work with Markdown content

Twig Markdown Extension

Build Status

Twig Markdown extension provides a new filter and a tag to allow parsing of content as Markdown in Twig templates.

This extension could be integrated with several Markdown parser as it provides an interface, which allows you to customize your Markdown parser.

Supported parsers

Features

  • Filter support {{ "# Heading Level 1"|markdown }}
  • Tag support {% markdown %}{% endmarkdown %}

When used as a tag, the indentation level of the first line sets the default indentation level for the rest of the tag content. From this indentation level, all same indentation or outdented levels text will be transformed as regular text.

This feature allows you to write your Markdown content at any indentation level without caring of Markdown internal transformation:

<div>
    <h1 class="someClass">{{ title }}</h1>

    {% markdown %}
    This is a list that is indented to match the context around the markdown tag:

    * List item 1
    * List item 2
        * Sub List Item
            * Sub Sub List Item

    The following block will be transformed as code, as it is indented more than the
    surrounding content:

        $code = "good";

    {% endmarkdown %}

</div>

Installation

Run the composer command to install the latest stable version:

composer require aptoma/twig-markdown

Or update your composer.json:

{
    "require": {
        "aptoma/twig-markdown": "~3.0"
    }
}

You can choose to provide your own Markdown engine, although we recommend using michelf/php-markdown:

composer require michelf/php-markdown ~1.8
{
    "require": {
        "michelf/php-markdown": "~1.8"
    }
}

You may also use the PHP League CommonMark engine:

composer require league/commonmark ~0.19
{
    "require": {
        "league/commonmark": "~0.19"
    }
}

Usage

Twig Extension

The Twig extension provides the markdown tag and filter support.

Assuming that you are using composer autoloading, add the extension to the Twig environment:

use Aptoma\Twig\Extension\MarkdownExtension;
use Aptoma\Twig\Extension\MarkdownEngine;

$engine = new MarkdownEngine\MichelfMarkdownEngine();

$twig->addExtension(new MarkdownExtension($engine));

Twig Token Parser

The Twig token parser provides the markdown tag only!

use Aptoma\Twig\TokenParser\MarkdownTokenParser;

$twig->addTokenParser(new MarkdownTokenParser());

Symfony

To use this extension in a Symfony 3/4 app (including Pimcore), add the following snippet to your app's app/config/services.yml file:

services:
    # ...

    markdown.engine:
        class: Aptoma\Twig\Extension\MarkdownEngine\MichelfMarkdownEngine
    twig.markdown:
        class: Aptoma\Twig\Extension\MarkdownExtension
        arguments: ['@markdown.engine']
        tags:
            - { name: twig.extension }

GitHub Markdown Engine

MarkdownEngine\GitHubMarkdownEngine provides an interface to GitHub's markdown engine using their public API via KnpLabs\php-github-api. To reduce API calls, rendered documents are hashed and cached in the filesystem. You can pass a GitHub repository and the path to be used for caching to the constructor:

use Aptoma\Twig\Extension\MarkdownEngine;

$engine = new MarkdownEngine\GitHubMarkdownEngine(
    'aptoma/twig-markdown', // The GitHub repository to use as a context
    true,                   // Whether to use GitHub's Flavored Markdown (GFM)
    '/tmp/markdown-cache',  // Path on filesystem to store rendered documents
);

In order to authenticate the API client (for instance), it's possible to pass an own instance of \GitHub\Client instead of letting the engine create one itself:

$client = new \Github\Client;
$client->authenticate('GITHUB_CLIENT_ID', 'GITHUB_CLIENT_SECRET', \Github\Client::AUTH_URL_CLIENT_ID);

$engine = new MarkdownEngine\GitHubMarkdownEngine('aptoma/twig-markdown', true, '/tmp/markdown-cache', $client);

Using a different Markdown parser engine

If you want to use a different Markdown parser, you need to create an adapter that implements Aptoma\Twig\Extension\MarkdownEngineInterface.php. Have a look at Aptoma\Twig\Extension\MarkdownEngine\MichelfMarkdownEngine for an example.

Tests

The test suite uses PHPUnit:

$ phpunit

License

Twig Markdown Extension is licensed under the MIT license.

More Repositories

1

silex-bootstrap

A starting point for Silex based applications. Primarily developed for use by the Aptoma Product Department, but should be suitable for everyoneโ„ข.
JavaScript
31
star
2

silex-extras

Collection of common stuff for Silex powered applications.
PHP
27
star
3

videoplaza-html5

Library for Videoplaza HTML5 video integration
JavaScript
14
star
4

lambda-papertrail-logger

A logger to send AWS Lambda logs to Papertrail
JavaScript
6
star
5

aloha-plugins

Custom plugins for the Aloha editor
JavaScript
4
star
6

ddblock

Distributed locking using DynamoDB
JavaScript
3
star
7

drpublish-plugin-api

Plugin JS for DrPublish
JavaScript
3
star
8

ansible-papertrail

Ansible role for setting up Papertrail export
Ruby
2
star
9

drpublish-api-client-php

Example implementation of a DrPublish API data extractor client
PHP
2
star
10

drmobile-integration

DrMobile integration example
HTML
2
star
11

zend-framework

Zend Framework
PHP
1
star
12

drpublish-hello-world-plugin

JavaScript
1
star
13

hapi-static-headers

Hapi plugin for adding a defined set of headers to be appended to all responses.
JavaScript
1
star
14

propel-lib

Propel Orm only the lib part, to work better as a submodule
PHP
1
star
15

lp-assets-print

Assets and components for DN (including Magasinet) print production.
CSS
1
star
16

ansible-aws-codedeploy

Ansible script for install AWS CodeDeploy Agent
1
star
17

ass-client-js

Aptoma Smooth Storage client for node.js
JavaScript
1
star
18

softmandel

SOFTware MANagement and DEveLopment Standards in Aptoma.
1
star
19

hapi-json-api

Hapi plugin for formatting output/errors according to the JSON API specification
JavaScript
1
star
20

hapi-graceful-stop

Hapi plugin that listens on SIGINT and graceful stop the server.
JavaScript
1
star
21

sqs-consumer

Nodejs AWS SQS consumer
JavaScript
1
star