Skip to content

Commit c6fe1cc

Browse files
author
仿生猫梦见苦力怕
committed
added markdown
1 parent 36ccaf9 commit c6fe1cc

File tree

7 files changed

+98
-38
lines changed

7 files changed

+98
-38
lines changed

app/forms.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
from flask_wtf import FlaskForm
2-
from wtforms import TextAreaField, SubmitField
2+
from wtforms import StringField, TextAreaField, SubmitField
33
from wtforms.validators import DataRequired
44

55

6+
class PostForm(FlaskForm):
7+
title = StringField('标题', validators=[DataRequired()])
8+
content = TextAreaField('内容', validators=[DataRequired()])
9+
submit = SubmitField('发布')
10+
11+
612
class CommentForm(FlaskForm):
713
content = TextAreaField('评论内容', validators=[DataRequired()])
814
submit = SubmitField('提交评论')

app/models.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,22 @@ def get_id(self):
3232
class Post(SQLModel, table=True):
3333
id: Optional[int] = Field(default=None, primary_key=True)
3434
title: str
35-
content: str
35+
content: str # 原始 Markdown 内容
36+
html_content: str # 解析后的 HTML 内容
3637
author_id: int = Field(foreign_key="user.id")
37-
created_at: datetime = Field(default_factory=datetime.utcnow) # 文章创建时间
38+
created_at: datetime = Field(default_factory=datetime.utcnow)
3839

3940
author: User = Relationship(back_populates="posts")
40-
comments: List["Comment"] = Relationship(back_populates="post") # 博文的评论
41+
comments: List["Comment"] = Relationship(back_populates="post")
4142

4243

4344
class Comment(SQLModel, table=True):
4445
id: Optional[int] = Field(default=None, primary_key=True)
45-
content: str # 评论内容
46-
created_at: datetime = Field(default_factory=datetime.utcnow) # 评论时间
47-
author_id: int = Field(foreign_key="user.id") # 评论作者
48-
post_id: int = Field(foreign_key="post.id") # 所属博文
49-
50-
# 关系
51-
author: "User" = Relationship(back_populates="comments")
52-
post: "Post" = Relationship(back_populates="comments")
46+
content: str # 原始 Markdown 内容
47+
html_content: str # 解析后的 HTML 内容
48+
created_at: datetime = Field(default_factory=datetime.utcnow)
49+
author_id: int = Field(foreign_key="user.id")
50+
post_id: int = Field(foreign_key="post.id")
51+
52+
author: User = Relationship(back_populates="comments")
53+
post: Post = Relationship(back_populates="comments")

app/routes.py

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import markdown
12
from flask import Blueprint, render_template, request, url_for, flash
23
from flask_login import login_required, current_user
34
from sqlmodel import Session, select, desc
@@ -6,7 +7,7 @@
67

78
from app import engine
89
from app.models import Post, User, Comment
9-
from app.forms import CommentForm, EditCommentForm
10+
from app.forms import CommentForm, EditCommentForm, PostForm
1011

1112
# 创建蓝图
1213
main = Blueprint('main', __name__)
@@ -44,22 +45,21 @@ def dashboard():
4445
@main.route('/create_post', methods=['GET', 'POST'])
4546
@login_required
4647
def create_post():
47-
if request.method == 'POST':
48-
title = request.form.get('title')
49-
content = request.form.get('content')
50-
51-
if not title or not content:
52-
flash('你必须填写标题和内容字段!')
53-
else:
54-
new_post = Post(title=title, content=content, author_id=current_user.id)
55-
with Session(engine) as session:
56-
session.add(new_post)
57-
session.commit()
58-
print(f"New post created: {new_post}") # 调试信息
59-
flash('你成功创建了一篇新的博文!')
48+
form = PostForm()
49+
if form.validate_on_submit():
50+
html_content = markdown.markdown(form.content.data) # 解析 Markdown
51+
new_post = Post(
52+
title=form.title.data,
53+
content=form.content.data,
54+
html_content=html_content,
55+
author_id=current_user.id
56+
)
57+
with Session(engine) as session:
58+
session.add(new_post)
59+
session.commit()
60+
flash('博文已发布!', 'success')
6061
return redirect(url_for('main.dashboard'))
61-
62-
return render_template('create_post.html')
62+
return render_template('create_post.html', form=form)
6363

6464

6565
@main.route('/post/<int:post_id>')
@@ -78,12 +78,14 @@ def view_post(post_id):
7878
def add_comment(post_id):
7979
form = CommentForm()
8080
if form.validate_on_submit():
81+
html_content = markdown.markdown(form.content.data) # 解析 Markdown
82+
new_comment = Comment(
83+
content=form.content.data,
84+
html_content=html_content,
85+
author_id=current_user.id,
86+
post_id=post_id
87+
)
8188
with Session(engine) as session:
82-
new_comment = Comment(
83-
content=form.content.data,
84-
author_id=current_user.id,
85-
post_id=post_id
86-
)
8789
session.add(new_comment)
8890
session.commit()
8991
flash('评论已发布!', 'success')
@@ -111,11 +113,12 @@ def edit_post(post_id):
111113
else:
112114
post.title = title
113115
post.content = content
116+
post.html_content = markdown.markdown(content)
114117
session.commit()
115118
flash('你的博文已被更新!')
116119
return redirect(url_for('main.view_post', post_id=post.id))
117120

118-
return render_template('edit_post.html', post=post)
121+
return render_template('edit_post.html', post=post, form=CommentForm())
119122

120123

121124
@main.route('/delete_post/<int:post_id>', methods=['POST'])
@@ -151,6 +154,7 @@ def edit_comment(comment_id):
151154
form = EditCommentForm()
152155
if form.validate_on_submit():
153156
comment.content = form.content.data
157+
comment.html_content = markdown.markdown(form.content.data) # 解析 Markdown
154158
session.commit()
155159
flash('评论已更新!', 'success')
156160
return redirect(url_for('main.view_post', post_id=comment.post_id))

app/templates/create_post.html

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,16 @@
22

33
{% block title %}发布新博文 - 六(5)班 班级博客{% endblock %}
44

5+
{% block head %}
6+
{{ super() }}
7+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/simplemde/latest/simplemde.min.css">
8+
<script src="https://cdn.jsdelivr.net/simplemde/latest/simplemde.min.js"></script>
9+
{% endblock %}
10+
511
{% block content %}
612
<h1>发布新博文</h1>
713
<form method="POST" action="{{ url_for('main.create_post') }}" class="post-form">
14+
{{ form.hidden_tag() }}
815
<div class="form-group">
916
<label for="title">标题:</label>
1017
<input type="text" id="title" name="title" class="form-input" required>
@@ -15,5 +22,22 @@ <h1>发布新博文</h1>
1522
</div>
1623
<button type="submit" class="form-button">发布</button>
1724
</form>
18-
<button onclick="window.location.href='{{ url_for('main.dashboard') }}';" class="form-button back-button">返回仪表盘</button>
25+
<button onclick="window.location.href='{{ url_for('main.dashboard') }}'" class="form-button back-button">返回仪表盘</button>
26+
27+
<script>
28+
// 初始化 SimpleMDE 编辑器
29+
const simplemde = new SimpleMDE({
30+
element: document.getElementById("content"),
31+
spellChecker: false,
32+
autosave: {
33+
enabled: true,
34+
uniqueId: "post-content",
35+
},
36+
});
37+
38+
document.querySelector('.post-form').addEventListener('submit', function (e) {
39+
const contentTextarea = document.getElementById('content');
40+
contentTextarea.value = simplemde.value(); // 将 Markdown 编辑器的内容同步到 textarea
41+
});
42+
</script>
1943
{% endblock %}

app/templates/edit_post.html

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,16 @@
22

33
{% block title %}编辑博文 - 六(5)班 班级博客{% endblock %}
44

5+
{% block head %}
6+
{{ super() }}
7+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/simplemde/latest/simplemde.min.css">
8+
<script src="https://cdn.jsdelivr.net/simplemde/latest/simplemde.min.js"></script>
9+
{% endblock %}
10+
511
{% block content %}
612
<h1>编辑博文</h1>
713
<form method="POST" action="{{ url_for('main.edit_post', post_id=post.id) }}" class="post-form">
14+
{{ form.hidden_tag() }}
815
<div class="form-group">
916
<label for="title">标题:</label>
1017
<input type="text" id="title" name="title" class="form-input" value="{{ post.title }}" required>
@@ -16,4 +23,21 @@ <h1>编辑博文</h1>
1623
<button type="submit" class="form-button">更新</button>
1724
</form>
1825
<button onclick="window.location.href='{{ url_for('main.view_post', post_id=post.id) }}';" class="form-button back-button">取消</button>
26+
<script>
27+
// 初始化 SimpleMDE 编辑器
28+
const simplemde = new SimpleMDE({
29+
element: document.getElementById("content"),
30+
spellChecker: false,
31+
autosave: {
32+
enabled: true,
33+
uniqueId: "post-content",
34+
},
35+
});
36+
37+
// 确保表单提交时,Markdown 内容被正确传递
38+
document.querySelector('.post-form').addEventListener('submit', function (e) {
39+
const contentTextarea = document.getElementById('content');
40+
contentTextarea.value = simplemde.value(); // 将 Markdown 编辑器的内容同步到 textarea
41+
});
42+
</script>
1943
{% endblock %}

app/templates/view_post.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ <h1 class="post-title">{{ post.title }}</h1>
1010
<span class="post-date">发布时间:{{ post.created_at }}</span>
1111
</div>
1212
<div class="post-content">
13-
{{ post.content }}
13+
{{ post.html_content | safe }}
1414
</div>
1515
{% if current_user == post.author %}
1616
<div class="post-actions">
@@ -47,7 +47,7 @@ <h3>评论</h3>
4747
<span class="comment-date">{{ comment.created_at }}</span>
4848
</div>
4949
<div class="comment-content">
50-
{{ comment.content }}
50+
{{ comment.html_content | safe }}
5151
</div>
5252
{% if current_user == comment.author %}
5353
<div class="comment-actions">

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ Flask-Login==0.5.0
44
Flask-WTF==0.15.1
55
Werkzeug~=2.0.3
66
SQLAlchemy~=2.0.38
7-
WTForms~=3.0.1
7+
WTForms~=3.0.1
8+
markdown~=3.4.4

0 commit comments

Comments
 (0)