122 lines
5.1 KiB
JavaScript
122 lines
5.1 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'))
|
|
}
|
|
|
|
let allPosts = getFiles(POSTS_DIR)
|
|
let allKniepunkte = getFiles(KNIEPUNKT_DIR)
|
|
|
|
// 1. Undo false attributions
|
|
const unassign = [
|
|
{ match: '003-wird-ki-wirklich-alle-jobs-uebernehmen', newName: 'wird-ki-wirklich-alle-jobs-uebernehmen.md' },
|
|
{ match: '005-enough-lets-move-on', newName: 'enough-lets-move-on.md' },
|
|
{ match: '006-zwischen-gigafactories', newName: 'zwischen-gigafactories-und-dsgvo.md' }
|
|
]
|
|
|
|
unassign.forEach(u => {
|
|
const f = allKniepunkte.find(x => x.includes(u.match))
|
|
if (f && fs.existsSync(f)) {
|
|
const m = matter(fs.readFileSync(f, 'utf8'))
|
|
m.data.title = m.data.title.replace(/^Kniepunkt 00[356]: /, '')
|
|
delete m.data.issue
|
|
const out = path.join(POSTS_DIR, u.newName)
|
|
fs.writeFileSync(out, matter.stringify(m.content, m.data))
|
|
fs.unlinkSync(f)
|
|
console.log(`↩️ Undid false attribution for ${u.match}`)
|
|
}
|
|
})
|
|
|
|
// 2. Merge intro posts from POSTS_DIR into KNIEPUNKT_DIR and delete them
|
|
allPosts = getFiles(POSTS_DIR) // Refresh
|
|
allKniepunkte = getFiles(KNIEPUNKT_DIR) // Refresh
|
|
|
|
const introPosts = allPosts.filter(p => fs.readFileSync(p, 'utf8').toLowerCase().includes('kniepunkt'))
|
|
introPosts.forEach(intro => {
|
|
const content = fs.readFileSync(intro, 'utf8')
|
|
const introData = matter(content)
|
|
const isHeader = introData.data.title.toLowerCase().includes('kniepunkt') || content.toLowerCase().includes('kniepunkt')
|
|
if (isHeader) {
|
|
// Try to find issue number
|
|
const titleMatch = introData.data.title.match(/kniepunkt\s*0?(\d+)/i) || path.basename(intro).match(/kniepunkt-0?(\d+)/i)
|
|
|
|
// Also handle specific ones like 040, 029, 16.05.2026 (037)
|
|
let issueNum = titleMatch ? titleMatch[1] : null
|
|
|
|
// Manual overrides based on dates/content
|
|
if (intro.includes('05-30-der-papst-scheint')) issueNum = '40'
|
|
if (intro.includes('03-14-der-neue-kniepunkt')) issueNum = '29'
|
|
if (intro.includes('05-16-guten-morgen')) issueNum = '37' // check if 37
|
|
if (intro.includes('05-02-der-neue-kniepunkt')) issueNum = '36'
|
|
if (intro.includes('05-09-der-neue-kniepunkt')) issueNum = '38' // Example
|
|
if (intro.includes('05-23-der-neue-kniepunkt')) issueNum = '39' // Example
|
|
|
|
if (issueNum) {
|
|
const paddedNum = String(issueNum).padStart(3, '0')
|
|
const article = allKniepunkte.find(k => k !== intro && (path.basename(k).startsWith(paddedNum)))
|
|
|
|
if (article && fs.existsSync(article)) {
|
|
const articleData = matter(fs.readFileSync(article, 'utf8'))
|
|
// Only merge if not already merged
|
|
if (!articleData.content.includes(introData.content.trim().substring(0, 50))) {
|
|
const mergedContent = introData.content.trim() + '\n\n---\n\n' + articleData.content.trim()
|
|
articleData.data.date = introData.data.date
|
|
fs.writeFileSync(article, matter.stringify(mergedContent, articleData.data))
|
|
console.log(`✅ Merged Intro ${path.basename(intro)} -> ${path.basename(article)}`)
|
|
} else {
|
|
console.log(`⚠️ Already merged: ${path.basename(intro)} into ${path.basename(article)}`)
|
|
}
|
|
}
|
|
}
|
|
// Delete the intro post from POSTS_DIR to remove it from Artikel
|
|
fs.unlinkSync(intro)
|
|
console.log(`🗑️ Deleted intro post from posts/: ${path.basename(intro)}`)
|
|
}
|
|
})
|
|
|
|
// 3. Fix 012 dupe in Kniepunkt
|
|
const dupe12 = allKniepunkte.filter(k => path.basename(k).includes('012'))
|
|
if (dupe12.length > 1) {
|
|
// Keep the one with better content or specific name
|
|
const toDelete = dupe12.find(k => k.includes('KI-Soundtrack---LinkedIn'))
|
|
if (toDelete && fs.existsSync(toDelete)) {
|
|
fs.unlinkSync(toDelete)
|
|
console.log(`🗑️ Deleted 012 dupe: ${path.basename(toDelete)}`)
|
|
}
|
|
}
|
|
|
|
// 4. Fix 040 dupe in Kniepunkt
|
|
const dupe40 = allKniepunkte.filter(k => path.basename(k).includes('040'))
|
|
if (dupe40.length > 1) {
|
|
const toDelete = dupe40.find(k => !k.includes('paepstliche'))
|
|
if (toDelete && fs.existsSync(toDelete)) {
|
|
fs.unlinkSync(toDelete)
|
|
console.log(`🗑️ Deleted 040 dupe: ${path.basename(toDelete)}`)
|
|
}
|
|
}
|
|
|
|
// 5. Fix dates for articles in POSTS_DIR that are wrongly dated 18.07.2026 (or 17.07.2026)
|
|
allPosts = getFiles(POSTS_DIR) // Refresh
|
|
allPosts.forEach(post => {
|
|
if (fs.existsSync(post)) {
|
|
const pData = matter(fs.readFileSync(post, 'utf8'))
|
|
// If date is July 18, 2026 or July 17, 2026 (the import date)
|
|
if (pData.data.date && pData.data.date.includes('2026-07-18') || pData.data.date && pData.data.date.includes('2026-07-17')) {
|
|
// Try to get original date from filename
|
|
const match = path.basename(post).match(/^(\d{4}-\d{2}-\d{2})/)
|
|
if (match) {
|
|
pData.data.date = match[1] + 'T00:00:00.000Z'
|
|
fs.writeFileSync(post, matter.stringify(pData.content, pData.data))
|
|
console.log(`📅 Fixed date for ${path.basename(post)} to ${match[1]}`)
|
|
}
|
|
}
|
|
}
|
|
})
|
|
|