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
This commit is contained in:
Neo 2024-09-17 07:45:24 +00:00
parent 79a1de9968
commit 1d75c45a1d
5 changed files with 8 additions and 3 deletions

View file

@ -1,6 +1,7 @@
import os
from flask import Flask, request, g
import markdown
def create_app(test_config=None):

View file

@ -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)

View file

@ -8,7 +8,7 @@
<form method="post">
<label for="title">Title</label>
<input name="title" id="title" value="{{ request.form['title'] }}" required>
<label for="body">Body</label>
<label for="body">Body (Markdown supported)</label>
<textarea name="body" id="body">{{ request.form['body'] }}</textarea>
<input type="submit" value="Save">
</form>

View file

@ -19,7 +19,7 @@
<a class="action" href="{{ url_for('blog.update', id=post['id']) }}">Edit</a>
{% endif %}
</header>
<p class="body">{{ post['body'] }}</p>
<p class="body">{{ post['body']|safe }}</p>
</article>
{% if not loop.last %}
<hr>

View file

@ -8,7 +8,7 @@
<form method="post">
<label for="title">Title</label>
<input name="title" id="title" value="{{ request.form['title'] or post['title'] }}" required>
<label for="body">Body</label>
<label for="body">Body (Markdown supported)</label>
<textarea name="body" id="body">{{ request.form['body'] or post['body'] }}</textarea>
<input type="submit" value="Save">
</form>