增加一个图片文件转存的功能

This commit is contained in:
2024-12-07 18:07:29 +08:00
parent d7aa8ef257
commit aa6fd6db64
4 changed files with 41 additions and 21 deletions

View File

@@ -1,17 +1,34 @@
<?php <?php
function decodeMarkdownEntities($markdown) { function downloadImage($url, $saveDir) {
// Decode HTML entities in code blocks // 获取图片内容
// $markdown = preg_replace_callback('/(```.*?```|`[^`]*`)/s', function ($matches) { $imageContent = file_get_contents($url);
// return htmlentities($matches[0], ENT_QUOTES, 'UTF-8'); if ($imageContent === FALSE) {
// }, $markdown); return $url;
// Remove <br /> tags in code blocks }
$markdown = preg_replace_callback('/(```.*?```|`[^`]*`)/s', function ($matches) {
return str_replace('<br />', '', $matches[0]); // 生成唯一的文件名
$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); }, $markdown);
// 对图片内容存储到本机并替换
// Decode HTML entities in inline math and display math $saveDir = __DIR__ . "/../../upload/markdown";
$markdown = preg_replace_callback('/(\$\$.*?\$\$|\$[^$]*\$)/s', function ($matches) { $markdown = preg_replace_callback('/!\[.*?\]\((.*?)\)/', function ($matches) use ($saveDir) {
return htmlentities($matches[0], ENT_QUOTES, 'UTF-8'); $imageUrl = $matches[1];
$savedPath = downloadImage($imageUrl, $saveDir);
return str_replace($imageUrl, $savedPath, $matches[0]);
}, $markdown); }, $markdown);
return $markdown; return $markdown;
} }

View File

@@ -18,13 +18,13 @@ $time_limit = $_POST['time_limit'];
$memory_limit = $_POST['memory_limit']; $memory_limit = $_POST['memory_limit'];
$description = $_POST['description']; $description = $_POST['description'];
$description = decodeMarkdownEntities($description); $description = MarkdownEntities($description);
$input = $_POST['input']; $input = $_POST['input'];
$input = decodeMarkdownEntities($input); $input = MarkdownEntities($input);
$output = $_POST['output']; $output = $_POST['output'];
$output = decodeMarkdownEntities($output); $output = MarkdownEntities($output);
$sample_input = $_POST['sample_input']; $sample_input = $_POST['sample_input'];
$sample_output = $_POST['sample_output']; $sample_output = $_POST['sample_output'];
@@ -32,7 +32,7 @@ $test_input = $_POST['test_input'];
$test_output = $_POST['test_output']; $test_output = $_POST['test_output'];
$hint = $_POST['hint']; $hint = $_POST['hint'];
$hint = decodeMarkdownEntities($hint); $hint = MarkdownEntities($hint);
$source = $_POST['source']; $source = $_POST['source'];

View File

@@ -234,14 +234,14 @@ include_once("kindeditor.php") ;
$memory_limit = $_POST['memory_limit']; $memory_limit = $_POST['memory_limit'];
$description = $_POST['description']; $description = $_POST['description'];
$description = decodeMarkdownEntities($description); $description = MarkdownEntities($description);
$input = $_POST['input']; $input = $_POST['input'];
$input = decodeMarkdownEntities($input); $input = MarkdownEntities($input);
$output = $_POST['output']; $output = $_POST['output'];
$output = decodeMarkdownEntities($output); $output = MarkdownEntities($output);
$sample_input = $_POST['sample_input']; $sample_input = $_POST['sample_input'];
$sample_output = $_POST['sample_output']; $sample_output = $_POST['sample_output'];
@@ -249,7 +249,7 @@ include_once("kindeditor.php") ;
//if ($sample_output=="") $sample_output="\n"; //if ($sample_output=="") $sample_output="\n";
$hint = $_POST['hint']; $hint = $_POST['hint'];
$hint = decodeMarkdownEntities($hint); $hint = MarkdownEntities($hint);
$source = $_POST['source']; $source = $_POST['source'];
$remote_oj= $_POST['remote_oj']; $remote_oj= $_POST['remote_oj'];

View File

@@ -128,7 +128,10 @@ if ($pr_flag) {
strpos($s, "class=\"md") === false strpos($s, "class=\"md") === false
) // 没找到,那就加上 ) // 没找到,那就加上
$s = "<span class='md'>" . $s . "</span>"; $s = "<span class='md'>" . $s . "</span>";
// $s = html_entity_decode($str, ENT_QUOTES, 'UTF-8'); // 删除代码块里的<br />防止引起混乱
$s = preg_replace_callback('/(```.*?```|`[^`]*`)/s', function ($matches) {
return str_replace('<br />', '', $matches[0]);
}, $s);
// 特殊转义 // 特殊转义
$s = str_replace("<br />", "\n", $s); $s = str_replace("<br />", "\n", $s);
return $s; return $s;