mirror of
https://github.com/KazooTTT/kazoottt-blog.git
synced 2025-06-16 23:41:21 +08:00
75 lines
2.0 KiB
JavaScript
75 lines
2.0 KiB
JavaScript
// 批量修改 ./src/content/post/* 中的frontmatter中的category
|
|
|
|
const fs = require('fs')
|
|
const path = require('path')
|
|
|
|
const contentDir = path.join(__dirname, '../src/content/post')
|
|
const USE_FULL_PATH = true
|
|
|
|
function processDirectory(dir) {
|
|
fs.readdirSync(dir).forEach((item) => {
|
|
const itemPath = path.join(dir, item)
|
|
const stats = fs.statSync(itemPath)
|
|
|
|
if (stats.isDirectory()) {
|
|
processDirectory(itemPath)
|
|
} else if (stats.isFile() && path.extname(item) === '.md') {
|
|
processFile(itemPath)
|
|
}
|
|
})
|
|
}
|
|
|
|
function getCategoryFromPath(filePath) {
|
|
const relativePath = path.relative(contentDir, filePath)
|
|
const pathParts = relativePath.split(path.sep)
|
|
|
|
if (pathParts.length > 1) {
|
|
if (USE_FULL_PATH) {
|
|
// Join all directory parts except the filename
|
|
return pathParts.slice(0, -1).join('-')
|
|
} else {
|
|
// Just use the first directory after "post"
|
|
return pathParts[0]
|
|
}
|
|
}
|
|
|
|
return null
|
|
}
|
|
|
|
function processFile(filePath) {
|
|
const fileContent = fs.readFileSync(filePath, 'utf8')
|
|
const category = getCategoryFromPath(filePath)
|
|
|
|
if (!category) {
|
|
console.log(`No category found for: ${filePath}`)
|
|
return
|
|
}
|
|
|
|
const frontmatter = fileContent.match(/^---\n[\s\S]*?\n---/)
|
|
if (frontmatter) {
|
|
const frontmatterContent = frontmatter[0]
|
|
const frontmatterLines = frontmatterContent.split('\n')
|
|
|
|
let categoryUpdated = false
|
|
const updatedFrontmatterLines = frontmatterLines.map((line) => {
|
|
if (line.startsWith('category:')) {
|
|
categoryUpdated = true
|
|
return `category: ${category}`
|
|
}
|
|
return line
|
|
})
|
|
|
|
// Add category if it doesn't exist
|
|
if (!categoryUpdated) {
|
|
updatedFrontmatterLines.splice(-1, 0, `category: ${category}`)
|
|
}
|
|
|
|
const updatedFrontmatter = updatedFrontmatterLines.join('\n')
|
|
const updatedFileContent = fileContent.replace(frontmatterContent, updatedFrontmatter)
|
|
fs.writeFileSync(filePath, updatedFileContent, 'utf8')
|
|
console.log(`Updated category to '${category}' for: ${filePath}`)
|
|
}
|
|
}
|
|
|
|
processDirectory(contentDir)
|