42 lines
1.1 KiB
TypeScript
42 lines
1.1 KiB
TypeScript
import { readFileSync, existsSync, mkdirSync, writeFileSync } from 'node:fs'
|
|
import { join, dirname } from 'node:path'
|
|
import Papa from 'papaparse'
|
|
import { fileURLToPath } from 'node:url'
|
|
import { v4 as uuidv4 } from 'uuid'
|
|
|
|
const __filename = fileURLToPath(import.meta.url)
|
|
const __dirname = dirname(__filename)
|
|
|
|
const NAME = '人生备忘录'
|
|
const csvPath = join(__dirname, `${NAME}.csv`)
|
|
const content = readFileSync(csvPath, 'utf-8')
|
|
const outputDir = join(__dirname, NAME)
|
|
if (!existsSync(outputDir)) {
|
|
mkdirSync(outputDir)
|
|
}
|
|
|
|
const result = Papa.parse(content, {
|
|
header: true,
|
|
skipEmptyLines: true,
|
|
trimHeaders: true
|
|
})
|
|
const headers = result.meta.fields || []
|
|
const records: any[] = result.data
|
|
|
|
|
|
for (const record of records) {
|
|
const id = record?.Id ?? uuidv4()
|
|
|
|
let mdContent = ''
|
|
for (const header of headers) {
|
|
if (header === '标题' && record[header]) {
|
|
mdContent = `# ${record[header]}\n\n` + mdContent
|
|
} else if (record[header]) {
|
|
mdContent += `**${header}**: ${record[header]}\n\n`
|
|
}
|
|
}
|
|
|
|
const outputPath = join(outputDir, `${NAME}${id}.md`)
|
|
writeFileSync(outputPath, mdContent, 'utf-8')
|
|
}
|