添加 extract.ts

This commit is contained in:
2025-12-31 02:38:31 +08:00
commit a61cfc1aa2

41
extract.ts Normal file
View File

@@ -0,0 +1,41 @@
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')
}