100) { $desc = substr($desc, 0, 100) . '...'; } break; } $articles[] = [ 'name' => $name, 'title' => $title, 'desc' => $desc, 'file' => $file ]; } return $articles; } // JSON API 模式 if (isset($_GET['list-api'])) { header('Content-Type: application/json; charset=utf-8'); $articles = get_articles_list($md_dir); echo json_encode([ 'version' => $current_version, 'articles' => $articles ], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); exit; } // Markdown 解析函数(改进版) function parse_markdown($text, $version) { // 替换版本号占位符 $text = str_replace('{{VERSION}}', $version, $text); // 处理代码块(先处理,避免被其他规则干扰) $text = preg_replace_callback('/```(\w*)\n(.*?)```/s', function($m) { $lang = $m[1] ? ' class="language-' . htmlspecialchars($m[1]) . '"' : ''; return '
' . htmlspecialchars($m[2]) . '';
}, $text);
// 处理行内代码
$text = preg_replace('/`([^`]+)`/', '$1', $text);
// 处理标题
$text = preg_replace('/^### (.+)$/m', '') !== false) {
$in_code = true;
}
if (strpos($trimmed, '') !== false) {
$in_code = false;
}
if ($in_code) {
$result[] = $line;
continue;
}
// 处理列表项
if (preg_match('/^- (.+)$/', $trimmed, $m)) {
if (!$in_list) {
$result[] = '' . $trimmed; $in_paragraph = true; } else if ($in_paragraph && $trimmed !== '') { $result[] = $trimmed; } } // 结束未闭合的标签 if ($in_list) { $result[] = ''; } if ($in_paragraph) { $result[] = '
'; } return implode("\n", $result); } // 获取文章列表 $articles = get_articles_list($md_dir); // 判断是否显示列表模式 $show_list = isset($_GET['list']); // 获取请求的文档 $doc = isset($_GET['doc']) ? $_GET['doc'] : ''; // 安全处理:只允许字母、数字、下划线、连字符 $doc = preg_replace('/[^a-zA-Z0-9_-]/', '', $doc); // 文件路径 $file_path = ''; if ($doc !== '') { $file_path = $md_dir . '/' . $doc . '.md'; } // 如果文件不存在,显示第一个可用文档 if ($file_path === '' || !file_exists($file_path)) { if (count($articles) > 0) { $file_path = $articles[0]['file']; $doc = $articles[0]['name']; } else { // 没有文档时显示默认页面 $html_content = '暂无文档。
'; $title = 'Oraset 文档'; $doc = ''; } } // 读取文档内容 if ($file_path !== '' && file_exists($file_path)) { $content = file_get_contents($file_path); // 先执行 PHP 代码 ob_start(); eval('?>' . $content . '文档列表\n以下是所有可用的文档:
\n