feat: 文章类型显示原文字数代替时间 #1
+20
-1
@@ -21,6 +21,7 @@ function parseMarkdownWithMeta($filePath) {
|
||||
'tags' => [],
|
||||
'duration' => '',
|
||||
'type' => 'article',
|
||||
'word_count' => 0,
|
||||
'summary' => ''
|
||||
];
|
||||
|
||||
@@ -47,6 +48,13 @@ function parseMarkdownWithMeta($filePath) {
|
||||
}
|
||||
if (preg_match('/^duration:\s*(.+?)\s*$/m', $yaml, $m)) {
|
||||
$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';
|
||||
}
|
||||
if (preg_match('/^tags:\s*\[(.+)\]\s*$/m', $yaml, $m)) {
|
||||
@@ -55,6 +63,14 @@ function parseMarkdownWithMeta($filePath) {
|
||||
$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));
|
||||
$summaryLines = [];
|
||||
@@ -115,6 +131,7 @@ function scanDirectory($dir, $baseDir, $search) {
|
||||
'tags' => $meta['tags'],
|
||||
'duration' => $meta['duration'],
|
||||
'type' => $meta['type'],
|
||||
'word_count' => $meta['word_count'],
|
||||
'summary' => $meta['summary']
|
||||
];
|
||||
}
|
||||
@@ -209,7 +226,9 @@ function getTypeIcon($type) {
|
||||
<?php if ($doc['author']): ?>
|
||||
<span class="meta-item">👤 <?php echo htmlspecialchars($doc['author']); ?></span>
|
||||
<?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>
|
||||
<?php endif; ?>
|
||||
<?php if ($doc['platform']): ?>
|
||||
|
||||
Reference in New Issue
Block a user