Merge pull request 'feat: 文章类型显示原文字数代替时间' (#1) from feature/article-word-count into main

Reviewed-on: http://192.168.1.22:8418/starmen1265/kb-web/pulls/1
This commit was merged in pull request #1.
This commit is contained in:
2026-02-28 23:58:33 +08:00
+20 -1
View File
@@ -21,6 +21,7 @@ function parseMarkdownWithMeta($filePath) {
'tags' => [], 'tags' => [],
'duration' => '', 'duration' => '',
'type' => 'article', 'type' => 'article',
'word_count' => 0,
'summary' => '' 'summary' => ''
]; ];
@@ -47,6 +48,13 @@ function parseMarkdownWithMeta($filePath) {
} }
if (preg_match('/^duration:\s*(.+?)\s*$/m', $yaml, $m)) { if (preg_match('/^duration:\s*(.+?)\s*$/m', $yaml, $m)) {
$meta['duration'] = trim($m[1]); $meta['duration'] = trim($m[1]);
}
// 解析 type 字段
if (preg_match('/^type:\s*(.+?)\s*$/m', $yaml, $m)) {
$meta['type'] = trim($m[1]);
}
// 如果有 duration 但没有明确 type,则设为 video
if ($meta['duration'] && $meta['type'] === 'article') {
$meta['type'] = 'video'; $meta['type'] = 'video';
} }
if (preg_match('/^tags:\s*\[(.+)\]\s*$/m', $yaml, $m)) { if (preg_match('/^tags:\s*\[(.+)\]\s*$/m', $yaml, $m)) {
@@ -55,6 +63,14 @@ function parseMarkdownWithMeta($filePath) {
$meta['tags'] = $tagMatches[1] ?? []; $meta['tags'] = $tagMatches[1] ?? [];
} }
// 计算正文字数(文章类型)
if ($meta['type'] === 'article') {
// 移除 Markdown 标记,计算纯文字字数
$plainText = strip_tags($body);
$plainText = preg_replace('/[#*_`\[\]()]/', '', $plainText);
$meta['word_count'] = mb_strlen(preg_replace('/\s+/', '', $plainText));
}
// 提取正文概述 // 提取正文概述
$lines = preg_split('/\r?\n/', trim($body)); $lines = preg_split('/\r?\n/', trim($body));
$summaryLines = []; $summaryLines = [];
@@ -115,6 +131,7 @@ function scanDirectory($dir, $baseDir, $search) {
'tags' => $meta['tags'], 'tags' => $meta['tags'],
'duration' => $meta['duration'], 'duration' => $meta['duration'],
'type' => $meta['type'], 'type' => $meta['type'],
'word_count' => $meta['word_count'],
'summary' => $meta['summary'] 'summary' => $meta['summary']
]; ];
} }
@@ -209,7 +226,9 @@ function getTypeIcon($type) {
<?php if ($doc['author']): ?> <?php if ($doc['author']): ?>
<span class="meta-item">👤 <?php echo htmlspecialchars($doc['author']); ?></span> <span class="meta-item">👤 <?php echo htmlspecialchars($doc['author']); ?></span>
<?php endif; ?> <?php endif; ?>
<?php if ($doc['date']): ?> <?php if ($doc['type'] === 'article' && $doc['word_count'] > 0): ?>
<span class="meta-item">📝 <?php echo number_format($doc['word_count']); ?> 字</span>
<?php elseif ($doc['date']): ?>
<span class="meta-item">📅 <?php echo htmlspecialchars($doc['date']); ?></span> <span class="meta-item">📅 <?php echo htmlspecialchars($doc['date']); ?></span>
<?php endif; ?> <?php endif; ?>
<?php if ($doc['platform']): ?> <?php if ($doc['platform']): ?>