92 lines
2.9 KiB
JavaScript
92 lines
2.9 KiB
JavaScript
import fs from 'fs'
|
|
import path from 'path'
|
|
import matter from 'gray-matter'
|
|
|
|
const CONTENT_DIR = path.join(process.cwd(), 'content')
|
|
const KNIEPUNKT_DIR = path.join(CONTENT_DIR, 'kniepunkt')
|
|
const POSTS_DIR = path.join(CONTENT_DIR, 'posts')
|
|
|
|
function getFiles(dir) {
|
|
return fs.readdirSync(dir).map(f => path.join(dir, f)).filter(f => f.endsWith('.md'))
|
|
}
|
|
|
|
// 1. Clean up stubs (less than 150 chars of body)
|
|
const allPosts = getFiles(POSTS_DIR)
|
|
allPosts.forEach(file => {
|
|
const content = fs.readFileSync(file, 'utf-8')
|
|
const { content: body } = matter(content)
|
|
if (body.trim().length < 150) {
|
|
console.log(`🗑️ Deleting stub: ${path.basename(file)} (length: ${body.trim().length})`)
|
|
fs.unlinkSync(file)
|
|
}
|
|
})
|
|
|
|
// 2. Deduplicate Kniepunkte
|
|
const allKniepunktFiles = getFiles(KNIEPUNKT_DIR)
|
|
const issues = {}
|
|
|
|
allKniepunktFiles.forEach(file => {
|
|
const content = fs.readFileSync(file, 'utf-8')
|
|
const { data, content: body } = matter(content)
|
|
|
|
if (body.trim().length < 150) {
|
|
console.log(`🗑️ Deleting stub: ${path.basename(file)}`)
|
|
fs.unlinkSync(file)
|
|
return
|
|
}
|
|
|
|
const filename = path.basename(file)
|
|
|
|
// Extract issue number (1 to 3 digits)
|
|
const match = filename.match(/(?:kniepunkt|kp|issue|ausgabe)[^\d]*(\d{1,3})/i) || data.title.match(/(?:kniepunkt|kp|issue|ausgabe)[^\d]*(\d{1,3})/i)
|
|
|
|
let num = -1
|
|
if (match) {
|
|
num = parseInt(match[1], 10)
|
|
} else {
|
|
// try to find any standalone 2-3 digit number
|
|
const m2 = filename.match(/\b(\d{2,3})\b/)
|
|
if (m2) num = parseInt(m2[1], 10)
|
|
}
|
|
|
|
if (num !== -1) {
|
|
if (!issues[num]) issues[num] = []
|
|
issues[num].push({ file, filename, data, body, length: body.length })
|
|
} else {
|
|
console.log(`⚠️ Could not parse issue number for: ${filename}`)
|
|
}
|
|
})
|
|
|
|
Object.keys(issues).forEach(num => {
|
|
const files = issues[num]
|
|
if (files.length > 1) {
|
|
console.log(`\n🔍 Found ${files.length} duplicates for Issue ${num}:`)
|
|
|
|
// Pick the best one
|
|
// Priority: 1. Real date (not 2026-07-18) 2. Longest content 3. Starts with YYYY-MM-DD
|
|
files.sort((a, b) => {
|
|
const aRealDate = a.data.date !== '2026-07-18'
|
|
const bRealDate = b.data.date !== '2026-07-18'
|
|
if (aRealDate && !bRealDate) return -1
|
|
if (!aRealDate && bRealDate) return 1
|
|
|
|
const aDatePrefix = /^\d{4}-\d{2}-\d{2}/.test(a.filename)
|
|
const bDatePrefix = /^\d{4}-\d{2}-\d{2}/.test(b.filename)
|
|
if (aDatePrefix && !bDatePrefix) return -1
|
|
if (!aDatePrefix && bDatePrefix) return 1
|
|
|
|
return b.length - a.length // Longer is better
|
|
})
|
|
|
|
const best = files[0]
|
|
console.log(`✅ Keeping best: ${best.filename} (Date: ${best.data.date}, Len: ${best.length})`)
|
|
|
|
// Delete the rest
|
|
for (let i = 1; i < files.length; i++) {
|
|
console.log(`🗑️ Deleting duplicate: ${files[i].filename}`)
|
|
fs.unlinkSync(files[i].file)
|
|
}
|
|
}
|
|
})
|
|
console.log('✅ Deduplication complete!')
|