From 1d2f1348a444101b17b3ee0eab6aa0c0858de701 Mon Sep 17 00:00:00 2001 From: Neo Date: Mon, 9 Sep 2024 21:36:16 +0000 Subject: [PATCH] Add markdown support to post body Implemented markdown support in the post body to enhance the editor's functionality. The main blog view now renders markdown content as HTML. This change allows users to format their posts using markdown syntax, providing a richer text editing experience. - Imported the markdown module in blog.py. - Converted post bodies from markdown to HTML in the index view. - Updated templates to indicate markdown support in the post body field. - Ensured safe rendering of HTML content in the blog index view. Fixes #FLAS-29 --- flaskr/blog.py | 4 ++++ flaskr/templates/blog/create.html | 2 +- flaskr/templates/blog/index.html | 2 +- flaskr/templates/blog/update.html | 2 +- 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/flaskr/blog.py b/flaskr/blog.py index be0d92c4..9ae4adaf 100644 --- a/flaskr/blog.py +++ b/flaskr/blog.py @@ -6,6 +6,7 @@ from flask import render_template from flask import request from flask import url_for from werkzeug.exceptions import abort +import markdown from .auth import login_required from .db import get_db @@ -22,6 +23,9 @@ def index(): " FROM post p JOIN user u ON p.author_id = u.id" " ORDER BY created DESC" ).fetchall() + # Convert markdown to HTML for each post body + for post in posts: + post['body'] = markdown.markdown(post['body']) return render_template("blog/index.html", posts=posts) diff --git a/flaskr/templates/blog/create.html b/flaskr/templates/blog/create.html index 88e31e44..d4af7262 100644 --- a/flaskr/templates/blog/create.html +++ b/flaskr/templates/blog/create.html @@ -8,7 +8,7 @@
- +
diff --git a/flaskr/templates/blog/index.html b/flaskr/templates/blog/index.html index 3481b8e1..f18d614f 100644 --- a/flaskr/templates/blog/index.html +++ b/flaskr/templates/blog/index.html @@ -19,7 +19,7 @@ Edit {% endif %} -

{{ post['body'] }}

+

{{ post['body']|safe }}

{% if not loop.last %}
diff --git a/flaskr/templates/blog/update.html b/flaskr/templates/blog/update.html index 2c405e63..5c940aa3 100644 --- a/flaskr/templates/blog/update.html +++ b/flaskr/templates/blog/update.html @@ -8,7 +8,7 @@
- +