-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost-skeletongen.mjs
50 lines (39 loc) · 1.2 KB
/
post-skeletongen.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import fs from 'node:fs/promises'
/** @typedef { import('./src/content/config').BlogCollectionKeys } Keys */
/** @type {Keys[]} */
const keys = ['title', 'summary', 'publishedAt', 'tags', 'draft', 'updatedAt']
/** YYYY-MM-DDの日付文字列を取得する */
const currentDate = new Date().toISOString().split('T')[0]
/**
* @param {keys} key
* @param {string|null|false} value
*/
const setFrontMatter = (key, value) => `${key}: ${value}`;
/** @param {Keys} key */
const getFrontMatterValue = (key) => {
switch (key) {
case 'title':
case 'summary':
return "''"
case 'publishedAt':
return `'${currentDate}'`
case 'draft':
return false
case 'tags':
case 'updatedAt':
default:
return null
}
}
const frontMatter = keys
.map((key) => setFrontMatter(key, getFrontMatterValue(key))).join('\n')
const content = `---
${frontMatter}
---`
console.log(content)
/** コマンドライン引数`--filename=hoge`から`hoge`の部分を取得する */
const filename = process.argv.slice(2).at(0)?.split('=').at(1)
const __dirname = import.meta.dirname
await fs
.writeFile(`${__dirname}/src/content/blog/${filename}.mdx`, content)
.catch((error) => console.error(error))