Search code examples
pythonflaskmarkdownmisaka

Flask-Misaka can't recognize fenced code in markdown strings


I'm using Flask-Misaka with Flask to render a markdown string to html. However, it seems that the Flask-Misaka can't recognize fenced code. It does removed the back-ticks, but no colored block is displayed. I have tried with versions 0.4.0 and 0.4.1.

app.py

from flask import Flask, render_template
from flask_misaka import Misaka, markdown
app = Flask(__name__)
Misaka(app, fenced_code=True)
TEST_MD = markdown("```block```\n", fenced_code=True)

@app.route("/", methods=['GET'])
def index():
    return render_template('{{s|markdown}}', s=TEST_MD)

Solution

  • The issue is that you are missing a stylesheet. If you look at the HTML output of Flask it will show <p><code>block</code></p>\n. So the fenced code is seen and the HTML output is rendered correctly.

    Short example which directly shows the result when the code is executed:

    from flask import Flask, render_template_string
    from flask_misaka import markdown
    
    app = Flask(__name__)
    
    with app.app_context():
        render_template_string('{{s}}', s=markdown("```block```\n", fenced_code=True))