commit a61cfc1aa2dfe98381038ffa30ae8725792470b5 Author: 熊潇 Date: Wed Dec 31 02:38:31 2025 +0800 添加 extract.ts diff --git a/extract.ts b/extract.ts new file mode 100644 index 0000000..872248b --- /dev/null +++ b/extract.ts @@ -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') +}