From 8f648d6819eb27596f09d8e41d12935094424383 Mon Sep 17 00:00:00 2001 From: spirit Date: Sun, 17 Nov 2019 17:00:40 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=84=E8=AE=BAMarkdown=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- functions.php | 49 +++++++++++++++++++++++++++++++++++++++++++++++ inc/Parsedown.php | 18 +++++++++++++++-- 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/functions.php b/functions.php index 7c7ef87..4de01d5 100644 --- a/functions.php +++ b/functions.php @@ -1655,4 +1655,53 @@ if (akina_option('sakura_widget')) { )); } } + +// 评论Markdown解析 +function markdown_parser($incoming_comment) { + global $wpdb,$comment_markdown_content; + $myCustomer = $wpdb->get_row("SELECT * FROM wp_comments"); + //Add column if not present. + if (!isset($myCustomer->say_state)) { + $wpdb->query("ALTER TABLE wp_comments ADD comment_markdown text"); + } + $comment_markdown_content = $incoming_comment['comment_content']; + include 'inc/Parsedown.php'; + $Parsedown = new Parsedown(); + $incoming_comment['comment_content'] = $Parsedown->text($incoming_comment['comment_content']); + return $incoming_comment; +} +add_filter('preprocess_comment' , 'markdown_parser'); + +//保存Markdown评论 +function save_markdown_comment($comment_ID, $comment_approved) { + global $wpdb,$comment_markdown_content; + $comment = get_comment($comment_ID); + $comment_content = $comment_markdown_content; + //store markdow content + $wpdb->query("UPDATE wp_comments SET comment_markdown='".$comment_content."' WHERE comment_ID='".$comment_ID."';"); +} +add_action('comment_post', 'save_markdown_comment', 10, 2); + +//打开评论HTML标签限制 +function allow_more_tag_in_comment() { + global $allowedtags; + $allowedtags['pre'] = array('class'=>array()); + $allowedtags['code'] = array('class'=>array()); + $allowedtags['h1'] = array('class'=>array()); + $allowedtags['h2'] = array('class'=>array()); + $allowedtags['h3'] = array('class'=>array()); + $allowedtags['h4'] = array('class'=>array()); + $allowedtags['h5'] = array('class'=>array()); + $allowedtags['ul'] = array('class'=>array()); + $allowedtags['ol'] = array('class'=>array()); + $allowedtags['li'] = array('class'=>array()); + $allowedtags['td'] = array('class'=>array()); + $allowedtags['th'] = array('class'=>array()); + $allowedtags['tr'] = array('class'=>array()); + $allowedtags['table'] = array('class'=>array()); + $allowedtags['thead'] = array('class'=>array()); + $allowedtags['tbody'] = array('class'=>array()); + $allowedtags['span'] = array('class'=>array()); +} +add_action('pre_comment_on_post', 'allow_more_tag_in_comment'); //code end diff --git a/inc/Parsedown.php b/inc/Parsedown.php index 87d612a..a34b44f 100644 --- a/inc/Parsedown.php +++ b/inc/Parsedown.php @@ -17,7 +17,7 @@ class Parsedown { # ~ - const version = '1.7.1'; + const version = '1.7.3'; # ~ @@ -429,7 +429,21 @@ class Parsedown if (isset($matches[1])) { - $class = 'language-'.$matches[1]; + /** + * https://www.w3.org/TR/2011/WD-html5-20110525/elements.html#classes + * Every HTML element may have a class attribute specified. + * The attribute, if specified, must have a value that is a set + * of space-separated tokens representing the various classes + * that the element belongs to. + * [...] + * The space characters, for the purposes of this specification, + * are U+0020 SPACE, U+0009 CHARACTER TABULATION (tab), + * U+000A LINE FEED (LF), U+000C FORM FEED (FF), and + * U+000D CARRIAGE RETURN (CR). + */ + $language = substr($matches[1], 0, strcspn($matches[1], " \t\n\f\r")); + + $class = 'language-'.$language; $Element['attributes'] = array( 'class' => $class,