优化知识库展示
列表页改进: - 显示元数据标题而非文件名 - 显示作者、发布时间、平台 - 显示正文概述前两行 - 根据平台显示图标 文章页改进: - 过滤 YAML front matter - 只显示正文内容
This commit is contained in:
@@ -10,14 +10,13 @@ $docDir = getUserDocDir($username);
|
||||
$file = $_GET['file'] ?? '';
|
||||
$search = $_GET['q'] ?? '';
|
||||
|
||||
// 安全检查:防止目录遍历攻击(允许子目录,但不能跳出用户目录)
|
||||
// 安全检查
|
||||
if (empty($file) || strpos($file, '..') !== false) {
|
||||
die('无效的文件名');
|
||||
}
|
||||
|
||||
$filePath = $docDir . '/' . $file;
|
||||
|
||||
// 确保文件在用户目录内
|
||||
$realPath = realpath($filePath);
|
||||
$realDocDir = realpath($docDir);
|
||||
if ($realPath === false || strpos($realPath, $realDocDir) !== 0) {
|
||||
@@ -30,9 +29,40 @@ if (!file_exists($filePath)) {
|
||||
|
||||
$content = file_get_contents($filePath);
|
||||
|
||||
// 解析元数据
|
||||
$title = basename($file);
|
||||
$meta = [
|
||||
'title' => $title,
|
||||
'author' => '',
|
||||
'date' => '',
|
||||
'platform' => ''
|
||||
];
|
||||
|
||||
// 提取并移除 YAML front matter
|
||||
if (preg_match('/^---\s*\n(.*?)\n---\s*\n(.*)$/s', $content, $matches)) {
|
||||
$yaml = $matches[1];
|
||||
$content = $matches[2];
|
||||
|
||||
// 解析元数据(用于页面标题)
|
||||
if (preg_match('/^title:\s*["\']?(.+?)["\']?\s*$/m', $yaml, $m)) {
|
||||
$title = trim($m[1], '"\'');
|
||||
$meta['title'] = $title;
|
||||
}
|
||||
if (preg_match('/^author:\s*(.+?)\s*$/m', $yaml, $m)) {
|
||||
$meta['author'] = trim($m[1]);
|
||||
}
|
||||
if (preg_match('/^date:\s*(.+?)\s*$/m', $yaml, $m)) {
|
||||
$meta['date'] = trim($m[1]);
|
||||
}
|
||||
if (preg_match('/^platform:\s*(.+?)\s*$/m', $yaml, $m)) {
|
||||
$meta['platform'] = trim($m[1]);
|
||||
}
|
||||
}
|
||||
|
||||
// 简单的 Markdown 转换
|
||||
function markdownToHtml($text) {
|
||||
// 标题
|
||||
$text = preg_replace('/^#### (.*$)/m', '<h4>$1</h4>', $text);
|
||||
$text = preg_replace('/^### (.*$)/m', '<h3>$1</h3>', $text);
|
||||
$text = preg_replace('/^## (.*$)/m', '<h2>$1</h2>', $text);
|
||||
$text = preg_replace('/^# (.*$)/m', '<h1>$1</h1>', $text);
|
||||
@@ -71,8 +101,6 @@ if ($search) {
|
||||
$htmlContent = highlightSearch($htmlContent, $search);
|
||||
}
|
||||
|
||||
// 获取文件名(不含路径)
|
||||
$displayName = basename($file);
|
||||
$folder = dirname($file) !== '.' ? dirname($file) : '';
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
@@ -80,7 +108,7 @@ $folder = dirname($file) !== '.' ? dirname($file) : '';
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title><?php echo htmlspecialchars($displayName); ?> - 知识库</title>
|
||||
<title><?php echo htmlspecialchars($title); ?> - 知识库</title>
|
||||
<link rel="stylesheet" href="style.css">
|
||||
</head>
|
||||
<body>
|
||||
@@ -89,7 +117,7 @@ $folder = dirname($file) !== '.' ? dirname($file) : '';
|
||||
<a href="dashboard.php<?php echo $search ? '?q=' . urlencode($search) : ''; ?>" class="btn-back">
|
||||
← 返回列表
|
||||
</a>
|
||||
<h1><?php echo htmlspecialchars($displayName); ?></h1>
|
||||
<h1><?php echo htmlspecialchars($title); ?></h1>
|
||||
<?php if ($folder): ?>
|
||||
<div class="folder-info">📁 <?php echo htmlspecialchars($folder); ?></div>
|
||||
<?php endif; ?>
|
||||
|
||||
Reference in New Issue
Block a user