增加一个函数统一处理markdown的问题

This commit is contained in:
2024-12-07 17:39:58 +08:00
parent 6f20a9ef73
commit b011e37b4d
3 changed files with 29 additions and 41 deletions

17
web/admin/md_check.php Normal file
View File

@@ -0,0 +1,17 @@
<?php
function decodeMarkdownEntities($markdown) {
// Decode HTML entities in code blocks
$markdown = preg_replace_callback('/(```.*?```|`[^`]*`)/s', function ($matches) {
return htmlentities($matches[0], ENT_QUOTES, 'UTF-8');
}, $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');
}, $markdown);
// 特殊转义
$markdown = str_replace("<p>", "", $markdown);
$markdown = str_replace("</p>", "\n\n", $markdown);
$markdown = str_replace("<br />", "\n\n", $markdown);
return $markdown;
}

View File

@@ -1,5 +1,6 @@
<?php
require_once ("admin-header.php");
require_once ("md_check.php");
require_once("../include/check_post_key.php");
if (!(isset($_SESSION[$OJ_NAME.'_'.'administrator']) || isset($_SESSION[$OJ_NAME.'_'.'contest_creator']) || isset($_SESSION[$OJ_NAME.'_'.'problem_editor']))) {
echo "<a href='../loginpage.php'>Please Login First!</a>";
@@ -12,45 +13,26 @@ require_once ("../include/problem.php");
// contest_id
$title = $_POST['title'];
// $title = str_replace(",", "&#44;", $title);
// $title = html_entity_decode($title, ENT_QUOTES, 'UTF-8');
$time_limit = $_POST['time_limit'];
$memory_limit = $_POST['memory_limit'];
$description = $_POST['description'];
//$description = str_replace("<p>", "", $description);
//$description = str_replace("</p>", "<br />", $description);
// $description = str_replace(",", "&#44;", $description);
// $description = html_entity_decode($description, ENT_QUOTES, 'UTF-8');
$description = decodeMarkdownEntities($description);
$input = $_POST['input'];
//$input = str_replace("<p>", "", $input);
//$input = str_replace("</p>", "<br />", $input);
// $input = str_replace(",", "&#44;", $input);
// $input = html_entity_decode($input, ENT_QUOTES, 'UTF-8');
$input = decodeMarkdownEntities($input);
$output = $_POST['output'];
//$output = str_replace("<p>", "", $output);
//$output = str_replace("</p>", "<br />", $output);
// $output = str_replace(",", "&#44;", $output);
// $output = html_entity_decode($output, ENT_QUOTES, 'UTF-8');
$output = decodeMarkdownEntities($output);
$sample_input = $_POST['sample_input'];
$sample_output = $_POST['sample_output'];
$test_input = $_POST['test_input'];
$test_output = $_POST['test_output'];
/* don't do this , we will left them empty for not generating invalid test data files
if ($sample_input=="") $sample_input="\n";
if ($sample_output=="") $sample_output="\n";
if ($test_input=="") $test_input="\n";
if ($test_output=="") $test_output="\n";
*/
$hint = $_POST['hint'];
//$hint = str_replace("<p>", "", $hint);
//$hint = str_replace("</p>", "<br />", $hint);
// $hint = str_replace(",", "&#44;", $hint);
// $hint = html_entity_decode($hint, ENT_QUOTES, 'UTF-8');
$hint = decodeMarkdownEntities($hint);
$source = $_POST['source'];

View File

@@ -1,6 +1,7 @@
<?php
require_once("../include/db_info.inc.php");
require_once("admin-header.php");
require_once ("md_check.php");
require_once("../include/my_func.inc.php");
if (!(isset($_SESSION[$OJ_NAME.'_'.'administrator']) || isset($_SESSION[$OJ_NAME.'_'.'problem_editor']))) {
@@ -227,29 +228,20 @@ include_once("kindeditor.php") ;
if (!(isset($_SESSION[$OJ_NAME.'_'."p$id"]) || isset($_SESSION[$OJ_NAME.'_'.'administrator']) || isset($_SESSION[$OJ_NAME.'_'.'problem_editor']) )) exit();
$title = $_POST['title'];
// $title = html_entity_decode($title,ENT_QUOTES,"UTF-8");
$time_limit = $_POST['time_limit'];
$memory_limit = $_POST['memory_limit'];
$description = $_POST['description'];
// $description = str_replace("<p>", "", $description);
// $description = str_replace("</p>", "<br />", $description);
// $description = str_replace(",", "&#44;", $description);
// $description = html_entity_decode($description,ENT_QUOTES,"UTF-8");
$description = decodeMarkdownEntities($description);
$input = $_POST['input'];
// $input = str_replace("<p>", "", $input);
// $input = str_replace("</p>", "<br />", $input);
// $input = str_replace(",", "&#44;", $input);
// $input = html_entity_decode($input,ENT_QUOTES,"UTF-8");
$input = decodeMarkdownEntities($input);
$output = $_POST['output'];
// $output = str_replace("<p>", "", $output);
// $output = str_replace("</p>", "<br />", $output);
// $output = str_replace(",", "&#44;", $output);
// $output = html_entity_decode($output,ENT_QUOTES,"UTF-8");
$output = decodeMarkdownEntities($output);
$sample_input = $_POST['sample_input'];
$sample_output = $_POST['sample_output'];
@@ -257,10 +249,7 @@ include_once("kindeditor.php") ;
//if ($sample_output=="") $sample_output="\n";
$hint = $_POST['hint'];
// $hint = str_replace("<p>", "", $hint);
// $hint = str_replace("</p>", "<br />", $hint);
// $hint = str_replace(",", "&#44;", $hint);
// $hint = html_entity_decode($hint,ENT_QUOTES,"UTF-8");
$hint = decodeMarkdownEntities($hint);
$source = $_POST['source'];
$remote_oj= $_POST['remote_oj'];