Git Product home page Git Product logo

twig-markdown's Introduction

Twig Markdown Extension

Build Status

THIS EXTENSION IS NO LONGER MAINTAINED, AND WE WILL NOT MAKE ANY NEW RELEASES. IF ANYONE WANTS TO TAKE OVER, PLEASE MAKE A FORK AND PUBLISH A NEW PACKAGE, AND WE'LL LINK TO THE NEW VERSION.

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.

twig-markdown's People

Contributors

admad avatar caseyamcl avatar ceefour avatar eexit avatar gabrielbull avatar gunnarlium avatar jakeasmith avatar jbtbnl avatar jdeniau avatar jeroendedauw avatar joeworkman avatar lukas-w avatar masterodin avatar qasim avatar seblours avatar stof avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

twig-markdown's Issues

Twig 2.x support

Hello,

Can you please confirm if this library is working with Twig 2.x please?

Thanks,

How to get in touch regarding a security concern

Hello ๐Ÿ‘‹

I run a security community that finds and fixes vulnerabilities in OSS. A researcher (@Rudloff) has found a potential issue, which I would be eager to share with you.

Could you add a SECURITY.md file with an e-mail address for me to send further details to? GitHub recommends a security policy to ensure issues are responsibly disclosed, and it would help direct researchers in the future.

Looking forward to hearing from you ๐Ÿ‘

(cc @huntr-helper)

Wrong version tagged

Hi !

I saw that you release the 2.1.0 version, but it should have been the 3.1.0 ;)

Thank you !

"Bad" twig require in composer

This extension will work in either twig 1 or twig 2. As that is the case, it seems to make sense to allow people using twig 2 to install this package.

Deprecated Twig calls

When using the markdown filter, I am presented with the following warnings:

DEPRECATED - The Twig_Filter_Method class is deprecated since version 1.12 and will be removed in 2.0. Use Twig_SimpleFilter instead.

DEPRECATED - The Twig_Filter class is deprecated since version 1.12 and will be removed in 2.0. Use Twig_SimpleFilter instead.

DEPRECATED - Using an instance of "Twig_Filter_Method" for filter "markdown" is deprecated. Use Twig_SimpleFilter instead.

Extra Line breaks

I'm using the code below

{% markdown %}
Line 1
Line 2
Line 3
{% endmarkdown %}

It gets turned into

<p>Line 1</p><br />
<p>Line 2</p><br />
<p>Line 3</p><br />

This causes the lines to be too spaced out. Is it possible to get the below?

Line 1<br />
Line 2<br />
Line 3<br />

PHP 8 support

Hi,

It would be nice to add support to PHP 8 :)

Best regards

Token Parser is documented incorrectly

Hello! I'm working on a markdown-related Symfony bundle and I've stumbled upon this library. It looks very promising, although I'm having a problem with the Token Parser.

README recommends using it like this:

use Aptoma\Twig\Extension\MarkdownEngine;
use Aptoma\Twig\TokenParser\MarkdownTokenParser;

$engine = new MarkdownEngine\MichelfMarkdownEngine();

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

However, MarkdownTokenParser doesn't have a constructor that accepts MarkdownEngineInterface, which left me quite puzzled. I looked at MarkdownTokenParserTest hoping that it would clear things up, but it looks like the test doesn't use MarkdownTokenParser at all.

So to sum things up:

  • README needs an update
  • MarkdownTokenParser probably needs a real test

Feature request: add support for Markdown Extra for markdown tags

In the twig cache I found this:

$md = new dflydev\markdown\MarkdownParser();
echo $md->transformMarkdown($content);

So it looks like it is hard coded to the MarkdownParser.

I would like to be able to use a mixture of HTML and markdown, like this:

{% markdown %}
  <div id='foo'>
    Bar
    ===

    Hello world!
  </div>
{% endmarkdown %}

Which is a feature of Markdown Extra.

Generate HTML at compile time

I have noticed that the Twig cache contains the unparsed Markdown. Would it be possible to store the generated HTML instead to make make rendering faster?

As far as I am aware, it can't contain any variables inside the markdown tag?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.