From 1d75c45a1dc0cac9dfe35bc491364adac5d0f1d8 Mon Sep 17 00:00:00 2001 From: Neo Date: Tue, 17 Sep 2024 07:45:24 +0000 Subject: [PATCH 1/2] Add markdown support to post body Implemented markdown support in the post body to enhance text formatting capabilities. This change allows users to enter markdown in the create/update interface and have it rendered as HTML in the main blog interface. The markdown library was imported and utilized to convert markdown text to HTML in the blog index view. Additionally, updated the form labels to indicate markdown support and ensured safe rendering of HTML content in the blog index template. Issue ID: FLAS-67 --- flaskr/__init__.py | 1 + flaskr/blog.py | 4 ++++ flaskr/templates/blog/create.html | 2 +- flaskr/templates/blog/index.html | 2 +- flaskr/templates/blog/update.html | 2 +- 5 files changed, 8 insertions(+), 3 deletions(-) diff --git a/flaskr/__init__.py b/flaskr/__init__.py index b92e3fe4..38d703d3 100644 --- a/flaskr/__init__.py +++ b/flaskr/__init__.py @@ -1,6 +1,7 @@ import os from flask import Flask, request, g +import markdown def create_app(test_config=None): 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 4658b876..d10466dc 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 @@
- +
From c1f791efee96502f3d996ec323dccb3855765494 Mon Sep 17 00:00:00 2001 From: Neo Date: Tue, 17 Sep 2024 07:47:14 +0000 Subject: [PATCH 2/2] Fix immutable cursor issue Address the issue where 'posts' was treated as a mutable object in 'flaskr/blog.py'. The 'posts' variable is an immutable cursor, which led to errors when attempting to modify it directly. The solution involved creating a new list from the cursor to allow modifications without affecting the original cursor. This change ensures that the application logic remains intact while preventing any runtime errors related to immutability. --- flaskr/blog.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/flaskr/blog.py b/flaskr/blog.py index 9ae4adaf..e4008778 100644 --- a/flaskr/blog.py +++ b/flaskr/blog.py @@ -23,6 +23,8 @@ def index(): " FROM post p JOIN user u ON p.author_id = u.id" " ORDER BY created DESC" ).fetchall() + # Convert the immutable cursor to a list of dictionaries + posts = [dict(post) for post in posts] # Convert markdown to HTML for each post body for post in posts: post['body'] = markdown.markdown(post['body'])