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

$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); // 代码块 $text = preg_replace('/```(\w*)\n(.*?)```/s', '
$2
', $text); $text = preg_replace('/`(.+?)`/', '$1', $text); // 链接 $text = preg_replace('/\[(.+?)\]\((.+?)\)/', '$1', $text); // 列表 $text = preg_replace('/^- (.*$)/m', '
  • $1
  • ', $text); $text = preg_replace('/(
  • .*<\/li>\n?)+/', '', $text); // 段落 $text = preg_replace('/\n\n/', '

    ', $text); $text = '

    ' . $text . '

    '; return $text; } // 高亮搜索词 function highlightSearch($text, $search) { if (empty($search)) return $text; return preg_replace('/(' . preg_quote($search, '/') . ')/i', '$1', $text); } $htmlContent = markdownToHtml(htmlspecialchars($content)); if ($search) { $htmlContent = highlightSearch($htmlContent, $search); } $folder = dirname($file) !== '.' ? dirname($file) : ''; ?> <?php echo htmlspecialchars($title); ?> - 知识库
    ← 返回列表

    📁