Home > Back-end >  You cannot use the "markdown_to_html" filter as no Markdown library is available
You cannot use the "markdown_to_html" filter as no Markdown library is available

Time:10-05

I am using Symfony 5.3.9 with PHP 7.4.24 and Composer 2.1.8. I want to render the following template with markdown_to_html but I am getting an error.

This is blog.html.twig

...
<article class="text-center">
    {{ content | markdown_to_html }}
</article>
...

And this is the error that I am getting:

An exception has been thrown during the rendering of a template ("You cannot use the "markdown_to_html" filter as no Markdown library is available; try running "composer require league/html-to-markdown".").

I run composer require league/html-to-markdown but it says "Nothing to install, update or remove".

My composer.json:

{
    "type": "project",
    "license": "proprietary",
    "require": {
        "php": "^7.4.24",
        "ext-ctype": "*",
        "ext-iconv": "*",
        "composer/package-versions-deprecated": "^1.10",
        "doctrine/annotations": "^1.0",
        "doctrine/doctrine-bundle": "^2.1",
        "doctrine/doctrine-migrations-bundle": "^3.0",
        "doctrine/orm": "^2.7",
        "league/html-to-markdown": "^5.0",
        "phpdocumentor/reflection-docblock": "^5.2",
        "sensio/framework-extra-bundle": "^5.5",
        "symfony/apache-pack": "^1.0",
        "symfony/asset": "5.3.*",
        "symfony/console": "5.3.*",
        "symfony/dotenv": "5.3.*",
        "symfony/expression-language": "5.3.*",
        "symfony/flex": "^1.3.1",
        "symfony/form": "5.3.*",
        "symfony/framework-bundle": "5.3.*",
        "symfony/http-client": "5.3.*",
        "symfony/intl": "5.3.*",
        "symfony/mailer": "5.3.*",
        "symfony/mime": "5.3.*",
        "symfony/monolog-bundle": "^3.1",
        "symfony/notifier": "5.3.*",
        "symfony/process": "5.3.*",
        "symfony/property-access": "5.3.*",
        "symfony/property-info": "5.3.*",
        "symfony/runtime": "5.3.*",
        "symfony/security-bundle": "5.3.*",
        "symfony/serializer": "5.3.*",
        "symfony/string": "5.3.*",
        "symfony/translation": "5.3.*",
        "symfony/twig-bundle": "5.3.*",
        "symfony/validator": "5.3.*",
        "symfony/web-link": "5.3.*",
        "symfony/yaml": "5.3.*",
        "twig/cssinliner-extra": "^3.3",
        "twig/extra-bundle": "^3.3",
        "twig/inky-extra": "^3.3",
        "twig/markdown-extra": "^3.3",
        "twig/twig": "^2.12|^3.0"
    },
    "require-dev": {
        "symfony/browser-kit": "^5.3",
        "symfony/css-selector": "^5.3",
        "symfony/debug-bundle": "^5.3",
        "symfony/maker-bundle": "^1.20",
        "symfony/phpunit-bridge": "^5.3",
        "symfony/stopwatch": "^5.3",
        "symfony/var-dumper": "^5.3",
        "symfony/web-profiler-bundle": "^5.3"
    },
    "config": {
        "optimize-autoloader": true,
        "preferred-install": {
            "*": "dist"
        },
        "sort-packages": true
    },
    "autoload": {
        "psr-4": {
            "App\\": "src/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "App\\Tests\\": "tests/"
        }
    },
    "replace": {
        "paragonie/random_compat": "2.*",
        "symfony/polyfill-ctype": "*",
        "symfony/polyfill-iconv": "*",
        "symfony/polyfill-php72": "*",
        "symfony/polyfill-php71": "*",
        "symfony/polyfill-php70": "*",
        "symfony/polyfill-php56": "*"
    },
    "scripts": {
        "auto-scripts": {
            "cache:clear": "symfony-cmd",
            "assets:install %PUBLIC_DIR%": "symfony-cmd"
        },
        "post-install-cmd": [
            "@auto-scripts"
        ],
        "post-update-cmd": [
            "@auto-scripts"
        ]
    },
    "conflict": {
        "symfony/symfony": "*"
    },
    "extra": {
        "symfony": {
            "allow-contrib": false,
            "require": "5.3.*"
        }
    }
}

I have followed this documentation https://twig.symfony.com/doc/3.x/filters/markdown_to_html.html . What I am doing wrong?

CodePudding user response:

The solution is to execute:

composer require league/commonmark

The twig/markdown-extra package just provides a twig extension for calling the markdown processor. You still need to install an actual markdown package. This is sort of documented at the very end of the markdown_to_html page but it is not very clear.

If no markdown packages are found then the html_to_markdown suggestion is emitted. Which would be fine if that is what was needed but markdown_to_html is a far more common requirement. Hence all the comments. The suggestion will be fixed in the next release.

And just for info, the league package is not the only one supported. You could also use:

   "require-dev": {
        "erusev/parsedown": "^1.7",
        "league/commonmark": "^1.0",
        "league/html-to-markdown": "^4.8|^5.0",
        "michelf/php-markdown": "^1.8"

As listed in markdown-extra/composer.json

  • Related