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', '

$1

', $text); $text = preg_replace('/^## (.+)$/m', '

$1

', $text); $text = preg_replace('/^# (.+)$/m', '

$1

', $text); // 处理粗体和斜体 $text = preg_replace('/\*\*([^*]+)\*\*/', '$1', $text); $text = preg_replace('/\*([^*]+)\*/', '$1', $text); // 处理链接(支持 URL 中包含括号) $text = preg_replace_callback('/\[([^\]]+)\]\(\s*([^)]*(?:\([^)]*\)[^)]*)*)\s*\)/', function($m) { $link_text = $m[1]; $url = trim($m[2]); return '' . $link_text . ''; }, $text); // 处理分隔线 $text = preg_replace('/^---$/m', '
', $text); // 处理无序列表 $lines = explode("\n", $text); $result = []; $in_list = false; $in_code = false; $in_paragraph = false; foreach ($lines as $line) { $trimmed = trim($line); // 检查是否在代码块中 if (strpos($trimmed, '
') !== 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[] = ''; $in_list = false; } // 处理块元素(标题、hr、pre等) if (preg_match('/^<(h[1-6]|hr|pre|ul|li)/', $trimmed) || $trimmed === '') { if ($in_paragraph) { $result[] = '

'; $in_paragraph = false; } $result[] = $line; continue; } // 处理段落 if (!$in_paragraph && $trimmed !== '') { $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 = '

Oraset 文档

暂无文档。

'; $title = 'Oraset 文档'; $doc = ''; } } // 读取文档内容 if ($file_path !== '' && file_exists($file_path)) { $content = file_get_contents($file_path); // 先执行 PHP 代码 ob_start(); eval('?>' . $content . '文档列表\n

以下是所有可用的文档:

\n'; $html_content = $list_html; } ?> <?php echo htmlspecialchars($title); ?>