feat: update blog content and scripts

This commit is contained in:
KazooTTT
2024-10-10 21:35:42 +08:00
parent 950d014fdb
commit d2f4fa5fec
77 changed files with 4973 additions and 4862 deletions

42
scripts/sortByDate.cjs Normal file
View File

@ -0,0 +1,42 @@
const fs = require('fs')
const path = require('path')
const matter = require('gray-matter')
const contentDir = path.join(__dirname, '../src/content/post')
function processDirectory(dir) {
const files = fs.readdirSync(dir).filter((file) => path.extname(file) === '.md')
const fileInfos = files.map((file) => {
const filePath = path.join(dir, file)
const content = fs.readFileSync(filePath, 'utf8')
const { data } = matter(content)
return {
name: file,
path: filePath,
date: new Date(data.date)
}
})
// Sort files by date
fileInfos.sort((a, b) => a.date - b.date)
// Rename files
fileInfos.forEach((file, index) => {
const newName = `${index.toString().padStart(2, '0')} ${file.name}`
const newPath = path.join(dir, newName)
fs.renameSync(file.path, newPath)
console.log(`Renamed ${file.name} to ${newName}`)
})
}
function processAllDirectories(baseDir) {
const items = fs.readdirSync(baseDir)
items.forEach((item) => {
const itemPath = path.join(baseDir, item)
if (fs.statSync(itemPath).isDirectory()) {
processDirectory(itemPath)
}
})
}
processAllDirectories(contentDir)

View File

@ -0,0 +1,68 @@
// 批量修改 ./src/content/post/* 中的frontmatter中的category
const fs = require('fs')
const path = require('path')
const contentDir = path.join(__dirname, '../src/content/post')
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)
// The category is the first directory after "post"
if (pathParts.length > 1) {
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)