html_to_markdown

The html_to_markdown filter converts a block of HTML to Markdown:

html_to_markdown フィルターは、HTML のブロックを Markdown に変換します。
1
2
3
4
5
{% apply html_to_markdown %}
    <html>
        <h1>Hello!</h1>
    </html>
{% endapply %}

You can also use the filter on an entire template which you include:

含めるテンプレート全体でフィルターを使用することもできます。
1
{{ include('some_template.html.twig')|html_to_markdown }}

Note

ノート

The html_to_markdown filter is part of the MarkdownExtension which is not installed by default. Install it first:

html_to_markdown フィルターは、デフォルトではインストールされない MarkdownExtension の一部です。最初にインストールします。
1
$ composer require twig/markdown-extra

On Symfony projects, you can automatically enable it by installing the twig/extra-bundle:

Symfony プロジェクトでは、twig/extra-bundle をインストールすることで自動的に有効にすることができます:
1
$ composer require twig/extra-bundle

Or add the extension explicitly on the Twig environment:

または、Twig 環境で拡張機能を明示的に追加します。
1
2
3
4
use Twig\Extra\Markdown\MarkdownExtension;

$twig = new \Twig\Environment(...);
$twig->addExtension(new MarkdownExtension());

If you are not using Symfony, you must also register the extension runtime:

Symfony を使用していない場合は、拡張ランタイムも登録する必要があります。
1
2
3
4
5
6
7
8
9
10
11
use Twig\Extra\Markdown\DefaultMarkdown;
use Twig\Extra\Markdown\MarkdownRuntime;
use Twig\RuntimeLoader\RuntimeLoaderInterface;

$twig->addRuntimeLoader(new class implements RuntimeLoaderInterface {
    public function load($class) {
        if (MarkdownRuntime::class === $class) {
            return new MarkdownRuntime(new DefaultMarkdown());
        }
    }
});

html_to_markdown is just a frontend; the actual conversion is done by one of the following compatible libraries, from which you can choose:

html_to_markdown は単なるフロントエンドです。実際の変換は、次の互換性のあるライブラリのいずれかによって行われ、そこから選択できます。

Depending on the library, you can also add some options by passing them as an argument to the filter. Example for league/html-to-markdown:

ライブラリによっては、引数としてフィルターに渡すことでいくつかのオプションを追加することもできます。リーグ/html からマークダウンへの例:
1
2
3
4
5
{% apply html_to_markdown({hard_break: false}) %}
    <html>
        <h1>Hello!</h1>
    </html>
{% endapply %}