From c98a893fb6a81fd46eb9e71746ee20cd92c7bd54 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Fri, 24 Feb 2023 22:10:29 +0800 Subject: [PATCH] feat(blog): markdown comment support --- web/app/controllers/subdomain/blog/blog.php | 59 ++++++++++++++------ web/app/models/UOJMarkdown.php | 4 +- web/css/uoj-bs5.css | 3 - web/js/uoj.js | 62 +++++++++++++++------ 4 files changed, 89 insertions(+), 39 deletions(-) diff --git a/web/app/controllers/subdomain/blog/blog.php b/web/app/controllers/subdomain/blog/blog.php index 85a324c..0b7cc2c 100644 --- a/web/app/controllers/subdomain/blog/blog.php +++ b/web/app/controllers/subdomain/blog/blog.php @@ -9,20 +9,46 @@ UOJBlog::cur()->belongsToUserBlog() || UOJResponse::page404(); UOJBlog::cur()->userCanView(Auth::user()) || UOJResponse::page403(); $blog = UOJBlog::info(); +$purifier = HTML::purifier(); +$parsedown = HTML::parsedown([ + 'username_with_color' => true, +]); function getCommentContentToDisplay($comment) { - if (!$comment['is_hidden']) { - return $comment['content']; - } else if (UOJUserBlog::userHasManagePermission(Auth::user())) { - return '【' . HTML::escape($comment['reason_to_hide']) . '】' . $comment['content']; - } else { - return '【' . HTML::escape($comment['reason_to_hide']) . '】'; + global $purifier, $parsedown; + + $rendered = $purifier->purify($parsedown->text($comment['content'])); + + if ($comment['is_hidden']) { + $esc_hide_reason = $comment['reason_to_hide']; + $res = << +
+ +
+
+
该评论被隐藏
+
{$esc_hide_reason}
+
+ + EOD; + + if (UOJUserBlog::userHasManagePermission(Auth::user())) { + $res .= <<{$rendered} + EOD; + } + + return $res; } + + return $rendered; } $comment_form = new UOJForm('comment'); $comment_form->addTextArea('comment', [ 'label' => '内容', + 'help' => '评论支持 Markdown 语法。可以用 @mike 来提到 mike 这个用户,mike 会被高亮显示。如果你真的想打 @ 这个字符,请用 @@。', 'validator_php' => function ($comment) { if (!Auth::check()) { return '请先登录'; @@ -257,22 +283,24 @@ $comments_pag = new Paginator([ $asrc = HTML::avatar_addr($poster, 80); $replies = DB::selectAll([ - "select id, poster, content, post_time, is_hidden, reason_to_hide from blogs_comments", + "select id, poster, content, post_time, is_hidden, reason_to_hide, zan from blogs_comments", "where", ["reply_id" => $comment['id']], "order by id" ]); foreach ($replies as $idx => $reply) { $reply_user = UOJUser::query($reply['poster']); + $replies[$idx]['poster_avatar'] = HTML::avatar_addr($reply_user, 80); $replies[$idx]['poster_realname'] = $reply_user['realname']; $replies[$idx]['poster_username_color'] = UOJUser::getUserColor($reply_user); $replies[$idx]['content'] = getCommentContentToDisplay($reply); + $replies[$idx]['click_zan_block'] = ClickZans::getBlock('BC', $reply['id'], $reply['zan']); } $replies_json = json_encode($replies); ?>
-
- + @@ -285,7 +313,7 @@ $comments_pag = new Paginator([
-
+
  • @@ -304,11 +332,11 @@ $comments_pag = new Paginator([
-
+
+ -
@@ -318,7 +346,6 @@ $comments_pag = new Paginator([ pagination() ?>

发表评论

-

可以用 @mike 来提到 mike 这个用户,mike 会被高亮显示。如果你真的想打“@”这个字符,请用“@@”。

printHTML() ?>