Skip to content
This repository was archived by the owner on Oct 17, 2019. It is now read-only.

Repository files navigation

Twig Markdown Extension

WARNINIG: This package is deprecate; migrate to twig/markdown-extra instead.

This package provides a Markdown to HTML filter (markdown) and an HTML to Markdown filter (html_to_markdown) for Twig and a Symfony bundle.

If you are not using Symfony, register the extension on Twig's Environment manually:

useTwig\Markdown\MarkdownExtension;
useTwig\Environment;
$twig = newEnvironment(...);
$twig->addExtension(newMarkdownExtension());

You must also register the extension runtime (skip this step if you are using Symfony or a framework with a Twig integration):

useTwig\Markdown\DefaultMarkdown;
useTwig\Markdown\MarkdownRuntime;
useTwig\RuntimeLoader\RuntimeLoaderInterface;
$twig->addRuntimeLoader(newclassimplements RuntimeLoaderInterface {
publicfunctionload($class) {
if (MarkdownRuntime::class === $class) {
returnnewMarkdownRuntime(newDefaultMarkdown());
}
}
});

Use the markdown and html_to_markdown filters from a Twig template:

{% filter markdown %}
Title
======
Hello!
{% endfilter %}
{% filter html_to_markdown %}
<html>
<h1>Hello!</h1>
</html>
{% endfilter %}

Note that you can indent the Markdown content as leading whitespaces will be removed consistently before conversion:

{% filter markdown %}
Title
======
Hello!
{% endfilter %}

You can also add some options by passing them as an argument to the filter:

{% filter html_to_markdown({hard_break: false}) %}
<html>
<h1>Hello!</h1>
</html>
{% endfilter %}

You can also use the filters on an included file:

{{ include('some_template.html.twig')|html_to_markdown }}
{{ include('some_template.markdown.twig')|markdown }}

About

[DEPRECATED] Markdown support for Twig

Topics

Resources

Security policy

Stars

102 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages