From e949e898896db4d6a6b502f81ab12d5fb229f687 Mon Sep 17 00:00:00 2001
From: Tiaotiao <65841827@qq.com>
Date: Sat, 27 Dec 2025 15:30:06 +0800
Subject: [PATCH 1/2] Implement CustomTagParser for custom tag handling
Added a CustomTagParser class to handle custom tags in text, including user, experiment, discussion, model, external, size, and color tags. Updated the rendering of content descriptions to utilize the new parser.
---
med.php | 225 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 223 insertions(+), 2 deletions(-)
diff --git a/med.php b/med.php
index f43b79e..af07d23 100644
--- a/med.php
+++ b/med.php
@@ -93,6 +93,120 @@ function formatDate($timestamp) {
];
$pageTitle = $pageTitles[$type] ?? '作品详情';
+// 自定义标签解析器类
+class CustomTagParser {
+
+ /**
+ * 解析包含自定义标签的文本
+ */
+ public static function parse(string $text): string {
+ if (empty($text)) {
+ return '';
+ }
+
+ // 先转义HTML特殊字符,防止XSS攻击
+ $text = htmlspecialchars($text);
+
+ // 先解析Markdown标题(在换行转换之前)
+ $text = self::parseMarkdownHeadings($text);
+
+ // 将换行符转换为
+ $text = nl2br($text);
+
+ // 解析自定义标签
+ $patterns = [
+ // user标签 - 保持原有格式
+ '/<user=([a-f0-9]+)>(.*?)<\/user>/i',
+ // experiment标签 - 跳转链接
+ '/<experiment=([a-f0-9]+)>(.*?)<\/experiment>/i',
+ // discussion标签 - 跳转链接
+ '/<discussion=([a-f0-9]+)>(.*?)<\/discussion>/i',
+ // model标签 - 跳转链接
+ '/<model=([a-f0-9]+)>(.*?)<\/model>/i',
+ // external标签 - 外部链接
+ '/<external=([^&]+)>(.*?)<\/external>/i',
+ // size标签
+ '/<size=([^&]+)>(.*?)<\/size>/i',
+ // color标签 - 新增
+ '/<color=([^&]+)>(.*?)<\/color>/i',
+ // b标签
+ '/<b>(.*?)<\/b>/i',
+ // i标签
+ '/<i>(.*?)<\/i>/i',
+ // a标签 - 深蓝色标签
+ '/<a>(.*?)<\/a>/i'
+ ];
+
+ $replacements = [
+ // user标签
+ '$2',
+ // experiment标签
+ '$2',
+ // discussion标签
+ '$2',
+ // model标签
+ '$2',
+ // external标签
+ '$2',
+ // size标签
+ '$2',
+ // color标签 - 新增
+ '$2',
+ // b标签
+ '$1',
+ // i标签
+ '$1',
+ // a标签
+ '$1'
+ ];
+
+ $text = preg_replace($patterns, $replacements, $text);
+
+ // 解析简单的Markdown格式
+ $text = self::parseSimpleMarkdown($text);
+
+ return $text;
+ }
+
+ /**
+ * 解析Markdown标题
+ */
+ private static function parseMarkdownHeadings(string $text): string {
+ // 支持1-6级标题
+ // 一级标题: # 标题
+ $text = preg_replace('/^# (.+)$/m', '$1', $text);
+
+ // 二级标题: ## 标题
+ $text = preg_replace('/^## (.+)$/m', '$1', $text);
+
+ // 三级标题: ### 标题
+ $text = preg_replace('/^### (.+)$/m', '$1', $text);
+
+ // 四级标题: #### 标题
+ $text = preg_replace('/^#### (.+)$/m', '$1', $text);
+
+ return $text;
+ }
+
+ /**
+ * 解析简单的Markdown格式
+ */
+ private static function parseSimpleMarkdown(string $text): string {
+ // 粗体
+ $text = preg_replace('/\*\*(.*?)\*\*/', '$1', $text);
+
+ // 斜体
+ $text = preg_replace('/\*(.*?)\*/', '$1', $text);
+
+ // 删除线
+ $text = preg_replace('/~~(.*?)~~/', '$1', $text);
+
+ // 行内代码
+ $text = preg_replace('/`([^`]+)`/', '$1', $text);
+
+ return $text;
+ }
+}
?>
@@ -107,6 +221,103 @@ function formatDate($timestamp) {
+