From aa6fd6db644bfae39c0470a5c4c17bf6f77ac3da Mon Sep 17 00:00:00 2001 From: klarkxy <278370456@qq.com> Date: Sat, 7 Dec 2024 18:07:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=B8=80=E4=B8=AA=E5=9B=BE?= =?UTF-8?q?=E7=89=87=E6=96=87=E4=BB=B6=E8=BD=AC=E5=AD=98=E7=9A=84=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web/admin/md_check.php | 41 ++++++++++++++++++++++++---------- web/admin/problem_add.php | 8 +++---- web/admin/problem_edit.php | 8 +++---- web/template/syzoj/problem.php | 5 ++++- 4 files changed, 41 insertions(+), 21 deletions(-) diff --git a/web/admin/md_check.php b/web/admin/md_check.php index 6195e52..55f55f2 100644 --- a/web/admin/md_check.php +++ b/web/admin/md_check.php @@ -1,17 +1,34 @@ tags in code blocks - $markdown = preg_replace_callback('/(```.*?```|`[^`]*`)/s', function ($matches) { - return str_replace('
', '', $matches[0]); +function downloadImage($url, $saveDir) { + // 获取图片内容 + $imageContent = file_get_contents($url); + if ($imageContent === FALSE) { + return $url; + } + + // 生成唯一的文件名 + $imageName = uniqid() . basename($url); + $savePath = rtrim($saveDir, '/') . '/' . $imageName; + + // 保存图片到本地 + if (file_put_contents($savePath, $imageContent) === FALSE) { + return $url; + } + + // 返回保存路径 + return $savePath; +} +function MarkdownEntities($markdown) { + // 对markdown中的内容进行转义 + $markdown = preg_replace_callback('/\$\$(.*?)\$\$/s', function ($matches) { + return str_replace(['<', '>'], ['\\lt', '\\gt'], $matches[0]); }, $markdown); - - // Decode HTML entities in inline math and display math - $markdown = preg_replace_callback('/(\$\$.*?\$\$|\$[^$]*\$)/s', function ($matches) { - return htmlentities($matches[0], ENT_QUOTES, 'UTF-8'); + // 对图片内容存储到本机并替换 + $saveDir = __DIR__ . "/../../upload/markdown"; + $markdown = preg_replace_callback('/!\[.*?\]\((.*?)\)/', function ($matches) use ($saveDir) { + $imageUrl = $matches[1]; + $savedPath = downloadImage($imageUrl, $saveDir); + return str_replace($imageUrl, $savedPath, $matches[0]); }, $markdown); return $markdown; } diff --git a/web/admin/problem_add.php b/web/admin/problem_add.php index cb9a85e..08c2d14 100644 --- a/web/admin/problem_add.php +++ b/web/admin/problem_add.php @@ -18,13 +18,13 @@ $time_limit = $_POST['time_limit']; $memory_limit = $_POST['memory_limit']; $description = $_POST['description']; -$description = decodeMarkdownEntities($description); +$description = MarkdownEntities($description); $input = $_POST['input']; -$input = decodeMarkdownEntities($input); +$input = MarkdownEntities($input); $output = $_POST['output']; -$output = decodeMarkdownEntities($output); +$output = MarkdownEntities($output); $sample_input = $_POST['sample_input']; $sample_output = $_POST['sample_output']; @@ -32,7 +32,7 @@ $test_input = $_POST['test_input']; $test_output = $_POST['test_output']; $hint = $_POST['hint']; -$hint = decodeMarkdownEntities($hint); +$hint = MarkdownEntities($hint); $source = $_POST['source']; diff --git a/web/admin/problem_edit.php b/web/admin/problem_edit.php index 78f6e2e..6557905 100644 --- a/web/admin/problem_edit.php +++ b/web/admin/problem_edit.php @@ -234,14 +234,14 @@ include_once("kindeditor.php") ; $memory_limit = $_POST['memory_limit']; $description = $_POST['description']; - $description = decodeMarkdownEntities($description); + $description = MarkdownEntities($description); $input = $_POST['input']; - $input = decodeMarkdownEntities($input); + $input = MarkdownEntities($input); $output = $_POST['output']; - $output = decodeMarkdownEntities($output); + $output = MarkdownEntities($output); $sample_input = $_POST['sample_input']; $sample_output = $_POST['sample_output']; @@ -249,7 +249,7 @@ include_once("kindeditor.php") ; //if ($sample_output=="") $sample_output="\n"; $hint = $_POST['hint']; - $hint = decodeMarkdownEntities($hint); + $hint = MarkdownEntities($hint); $source = $_POST['source']; $remote_oj= $_POST['remote_oj']; diff --git a/web/template/syzoj/problem.php b/web/template/syzoj/problem.php index 39e14d8..05c81d4 100644 --- a/web/template/syzoj/problem.php +++ b/web/template/syzoj/problem.php @@ -128,7 +128,10 @@ if ($pr_flag) { strpos($s, "class=\"md") === false ) // 没找到,那就加上 $s = "" . $s . ""; - // $s = html_entity_decode($str, ENT_QUOTES, 'UTF-8'); + // 删除代码块里的
防止引起混乱 + $s = preg_replace_callback('/(```.*?```|`[^`]*`)/s', function ($matches) { + return str_replace('
', '', $matches[0]); + }, $s); // 特殊转义 $s = str_replace("
", "\n", $s); return $s;