73 lines
3.3 KiB
JavaScript
73 lines
3.3 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'))
|
|
}
|
|
|
|
const allPosts = getFiles(POSTS_DIR)
|
|
const allKniepunkte = getFiles(KNIEPUNKT_DIR)
|
|
const all = [...allPosts, ...allKniepunkte]
|
|
|
|
function mergeFiles(headerPath, articlePath, outputPath, issueNum, issueDate) {
|
|
if (fs.existsSync(headerPath) && fs.existsSync(articlePath)) {
|
|
const headerData = matter(fs.readFileSync(headerPath, 'utf8'))
|
|
const articleData = matter(fs.readFileSync(articlePath, 'utf8'))
|
|
|
|
// Combine
|
|
const mergedContent = headerData.content.trim() + '\n\n---\n\n' + articleData.content.trim()
|
|
|
|
// Update frontmatter
|
|
articleData.data.title = `Kniepunkt 0${issueNum}: ` + articleData.data.title.replace(/^Kniepunkt \d+: /, '')
|
|
if (issueDate) articleData.data.date = issueDate
|
|
|
|
fs.writeFileSync(outputPath, matter.stringify(mergedContent, articleData.data))
|
|
|
|
// Delete old
|
|
if (headerPath !== outputPath && fs.existsSync(headerPath)) fs.unlinkSync(headerPath)
|
|
if (articlePath !== outputPath && fs.existsSync(articlePath)) fs.unlinkSync(articlePath)
|
|
console.log(`Merged 0${issueNum} to ${path.basename(outputPath)}`)
|
|
}
|
|
}
|
|
|
|
// 3. Merge 029
|
|
const article029 = allKniepunkte.find(f => f.includes('029-ki-nudelsuppe'))
|
|
const h29 = allPosts.find(f => fs.existsSync(f) && fs.readFileSync(f, 'utf8').includes('Nudelsuppe')) || allKniepunkte.find(f => fs.existsSync(f) && path.basename(f).includes('29.md'))
|
|
if (h29 && article029) mergeFiles(h29, article029, path.join(KNIEPUNKT_DIR, '029-ki-nudelsuppe.md'), 29)
|
|
|
|
// 4. Merge 031
|
|
const header031 = all.find(f => f.includes('031-ist-da'))
|
|
const article031 = all.find(f => f.includes('031-ki-verkaeufer-statt-ki-spielberg')) || path.join(KNIEPUNKT_DIR, '031-ki-verkaeufer-statt-ki-spielberg.md')
|
|
if (fs.existsSync(header031) && fs.existsSync(article031)) {
|
|
mergeFiles(header031, article031, path.join(KNIEPUNKT_DIR, '031-ki-verkaeufer-statt-ki-spielberg.md'), 31)
|
|
} else {
|
|
console.log(`Could not find 031 files`)
|
|
}
|
|
|
|
// 5. Rename 002-006 and move to kniepunkt
|
|
const renames = [
|
|
{ match: '2025-09-06-ki-als-fitnessstudio', newName: '002-ki-als-fitnessstudio-abo.md', issue: 2 },
|
|
{ match: '2025-09-12-wird-ki-wirklich-alle-jobs-uebernehmen', newName: '003-wird-ki-wirklich-alle-jobs-uebernehmen.md', issue: 3 },
|
|
{ match: '2025-09-20-die-ki-wetterkarte', newName: '004-die-ki-wetterkarte.md', issue: 4 },
|
|
{ match: '2025-09-26-enough-let-s-move-on', newName: '005-enough-lets-move-on.md', issue: 5 },
|
|
{ match: '2025-10-10-zwischen-gigafactories', newName: '006-zwischen-gigafactories.md', issue: 6 }
|
|
]
|
|
|
|
renames.forEach(r => {
|
|
const f = allPosts.find(x => fs.existsSync(x) && x.includes(r.match))
|
|
if (f) {
|
|
const m = matter(fs.readFileSync(f, 'utf8'))
|
|
m.data.title = `Kniepunkt 00${r.issue}: ` + m.data.title
|
|
m.data.issue = r.issue
|
|
const out = path.join(KNIEPUNKT_DIR, r.newName)
|
|
fs.writeFileSync(out, matter.stringify(m.content, m.data))
|
|
fs.unlinkSync(f)
|
|
console.log(`Moved ${r.issue} to kniepunkt: ${r.newName}`)
|
|
}
|
|
})
|