basename($filePath), 'author' => '', 'date' => '', 'platform' => '', 'url' => '', 'summary' => '' ]; // 解析 YAML front matter if (preg_match('/^---\s*\n(.*?)\n---\s*\n(.*)$/s', $content, $matches)) { $yaml = $matches[1]; $body = $matches[2]; // 解析 YAML 字段 if (preg_match('/^title:\s*["\']?(.+?)["\']?\s*$/m', $yaml, $m)) { $meta['title'] = trim($m[1], '"\''); } 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]); } if (preg_match('/^url:\s*(.+?)\s*$/m', $yaml, $m)) { $meta['url'] = trim($m[1]); } // 提取正文概述(前两行非空内容) $lines = preg_split('/\r?\n/', trim($body)); $summaryLines = []; foreach ($lines as $line) { $line = trim($line); if (empty($line) || strpos($line, '##') === 0) continue; $summaryLines[] = $line; if (count($summaryLines) >= 2) break; } $meta['summary'] = implode(' ', $summaryLines); } else { // 没有 front matter,从正文提取 $lines = preg_split('/\r?\n/', trim($content)); $summaryLines = []; foreach ($lines as $line) { $line = trim($line); if (empty($line) || strpos($line, '#') === 0) continue; $summaryLines[] = $line; if (count($summaryLines) >= 2) break; } $meta['summary'] = implode(' ', $summaryLines); } return $meta; } // 递归获取文档列表 $documents = []; function scanDirectory($dir, $baseDir, $search) { $docs = []; if (!is_dir($dir)) return $docs; $items = scandir($dir); foreach ($items as $item) { if ($item === '.' || $item === '..') continue; $fullPath = $dir . '/' . $item; $relativePath = str_replace($baseDir . '/', '', $fullPath); if (is_dir($fullPath)) { $docs = array_merge($docs, scanDirectory($fullPath, $baseDir, $search)); } elseif (is_file($fullPath)) { $ext = strtolower(pathinfo($item, PATHINFO_EXTENSION)); if (!in_array($ext, ['md', 'txt', 'markdown'])) continue; $content = file_get_contents($fullPath); // 搜索过滤 if ($search) { $searchLower = mb_strtolower($search); $contentLower = mb_strtolower($content); if (strpos($contentLower, $searchLower) === false) { continue; } } // 解析元数据 $meta = parseMarkdownWithMeta($fullPath); $docs[] = [ 'name' => $item, 'path' => $relativePath, 'folder' => dirname($relativePath) === '.' ? '' : dirname($relativePath), 'size' => filesize($fullPath), 'mtime' => filemtime($fullPath), 'title' => $meta['title'], 'author' => $meta['author'], 'date' => $meta['date'], 'platform' => $meta['platform'], 'summary' => $meta['summary'] ]; } } return $docs; } $documents = scanDirectory($docDir, $docDir, $search); // 按日期排序(新文件在前) usort($documents, function($a, $b) { // 优先按日期排序 if ($a['date'] && $b['date']) { return strcmp($b['date'], $a['date']); } // 其次按修改时间 return $b['mtime'] - $a['mtime']; }); // 平台图标映射 function getPlatformIcon($platform) { $icons = [ 'bilibili' => '📺', 'youtube' => '▶️', 'zhihu' => '💡', 'wechat' => '💬', ]; return $icons[$platform] ?? '📄'; } ?> 知识库 - <?php echo htmlspecialchars($username); ?>

📚 我的知识库

共 个文档 (搜索: "")
没有找到匹配的文档 暂无文档
👤 📅 🏷️
150 ? '...' : ''; ?>