daily excerpt

This commit is contained in:
TC
2026-06-09 23:20:33 +10:00
parent 8c0627ca59
commit f7c884b2ec

View File

@@ -85,17 +85,21 @@ function pickExcerpt(files: { relativePath: string; content: string }[]): Excerp
const candidates: { text: string; source: string }[] = [] const candidates: { text: string; source: string }[] = []
for (const file of files) { for (const file of files) {
const name = file.relativePath.split('/').pop()?.replace(/\.md$/, '') ?? file.relativePath const name = file.relativePath.split('/').pop()?.replace(/\.md$/, '') ?? file.relativePath
const paragraphs = file.content.split(/\n\n+/).filter(p => { const blocks = file.content.split(/\n{2,}/)
const clean = p.replace(/^#{1,6}\s.*/, '').trim() for (const block of blocks) {
const clean = block.replace(/^#{1,6}\s[^\n]*/gm, '').trim()
if (clean.startsWith('>') || clean.startsWith('#')) continue
const words = countWords(clean) const words = countWords(clean)
return words >= 20 && words <= 80 && !clean.startsWith('#') && !clean.startsWith('>') if (words < 15) continue
}) const wordList = clean.split(/\s+/)
for (const p of paragraphs) { const text = wordList.length > 60 ? wordList.slice(0, 60).join(' ') + '…' : clean
candidates.push({ text: p.trim(), source: name }) candidates.push({ text, source: name })
} }
} }
if (candidates.length === 0) return null if (candidates.length === 0) return null
return candidates[Math.floor(Math.random() * candidates.length)] const today = new Date()
const seed = today.getFullYear() * 10000 + (today.getMonth() + 1) * 100 + today.getDate()
return candidates[seed % candidates.length]
} }
function relativeTime(ts: number): string { function relativeTime(ts: number): string {