Skip to content
This repository has been archived by the owner on Oct 25, 2023. It is now read-only.

Commit

Permalink
🤔 将评论区代码高亮预解析交给后端
Browse files Browse the repository at this point in the history
(说白了兼容性问题,偷懒交给后端解析)
  • Loading branch information
bakaomg committed Apr 18, 2021
1 parent 27a1b1b commit 3975667
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
4 changes: 2 additions & 2 deletions core/libs/Comments.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* Castle Comments Libs
* Last Update: 2020/04/21
* Last Update: 2021/04/18
*/
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
//基本照搬 Typecho 的评论组件
Expand Down Expand Up @@ -137,7 +137,7 @@ private function threadedCommentsCallback() {
</div>

<div class="moe-comments-textBox moe-card-content">
<?php echo $this->getParent(); echo preg_replace('#</?[p][^>]*>#','', Castle_Contents::parseOwO(Castle_ShortCode::parseAll($this->content))); ?>
<?php echo $this->getParent(); echo Castle_Contents::code(preg_replace('#</?[p][^>]*>#','', Castle_Contents::parseOwO(Castle_ShortCode::parseAll($this->content)))); ?>
</div>
</div>
</main>
Expand Down
14 changes: 13 additions & 1 deletion core/libs/Contents.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* Castle Content Class
* Last Update: 2021/04/13
* Last Update: 2021/04/18
*/
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
//大部分修改搬自 AlanDecode[https://github.com/AlanDecode] 的主题 VOID
Expand Down Expand Up @@ -163,6 +163,18 @@ public static function markdown($text)
return $text;
}

/**
* Code
*/
public static function code($text)
{
$reg = '/<code class="lang-(.*)">([\\s\\S]*?)<\/code>/i';
$rp = '<pre><code class="lang-${1}">${2}</code></pre>';
$new = preg_replace($reg, $rp, $text);

return $new;
}

/**
* 文章阅读数
*
Expand Down
12 changes: 2 additions & 10 deletions static/js/castle.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
\_____| \__,_| |___/ \__| |_| \___|
----------------------------------------
* Castle's JavaScript
* Last Update: 2021/04/13 [0.9.6]
* Last Update: 2021/04/18 [0.9.6.1]
* Author: ohmyga (https://ohmyga.cn)
* GitHub: https://github.com/bakaomg/castle-Typecho-Theme/
* LICENSE: GPL V3 (https://www.gnu.org/licenses/gpl-3.0.html)
Expand Down Expand Up @@ -978,14 +978,6 @@ var CastlePost = {

//代码高亮
highLight: function () {
// 为评论区的代码高亮补全样式
$$('code[class^="lang"]:not(pre code)').each(function (key, item) {
let child = item;
let parent = document.createElement("pre");
child.parentNode.replaceChild(parent, child);
parent.appendChild(child);
});

$$("pre code").each(function (key, item) {
if (item.classList.contains("hljs") === false) {
$$(this).html("<ol><li>" + $$(this).html().replace(/\n/g, "\n</li><li>") + "\n</li></ol>");
Expand Down Expand Up @@ -1450,7 +1442,7 @@ var CastleComments = {
if (next[0] == null) {
return node;
}
if (next.hasClass("moe-comments-list-box") == false) {
if (next.hasClass("moe-comments-list-box") === false) {
return node;
}
nextNodeList = next.children(".moe-comments-box");
Expand Down

0 comments on commit 3975667

Please sign in to comment.