-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
articles.indexes.ts
92 lines (79 loc) · 2.47 KB
/
articles.indexes.ts
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { dirname, join } from 'path'
import { fileURLToPath, pathToFileURL } from 'url'
import { readFileSync, readdirSync, renameSync, statSync, writeFileSync } from 'fs'
import { argv, cwd } from 'process'
import YAML from 'yaml'
import colors from 'picocolors'
import { createLogger } from 'vite'
import { paramCase } from 'change-case'
interface Formatter {
[key: string]: any
id: string
updateDate: Date
createDate: Date
}
let MODE = 'production'
const BLOG_DIR = join(cwd(), 'src/pages/blog')
const ARTICLES_DIR = join(cwd(), 'src/articles')
const markdownReg = new RegExp(`${pathToFileURL(BLOG_DIR).href}.*\.md$`)
function isArticleMD(file: string) {
return markdownReg.test(pathToFileURL(file).href)
}
// 构建文章索引
export function generateIndex() {
const postFiles = readdirSync(BLOG_DIR)
.filter((fileName: string) => isArticleMD(`${BLOG_DIR}/${fileName}`))
const articleIndexes: Formatter[] = []
postFiles.forEach((fileName: string) => {
const path = join(BLOG_DIR, fileName)
const stat = statSync(path)
if (!stat.isFile())
return
const md = readFileSync(path, 'utf-8')
const yaml = (md.match(/---([\s\S]*)---/) || [])[1]
if (!yaml)
return
try {
const formatter = YAML.parse(yaml)
formatter.id = paramCase(fileName.replace('.md', ''))
if (!fileName.includes(formatter.id) && MODE === 'production')
renameSync(path, join(BLOG_DIR, `${formatter.id}.md`))
if (!formatter.updateDate)
formatter.updateDate = stat.mtime
if (!formatter.createDate)
formatter.createDate = stat.ctime
articleIndexes.push(formatter)
}
catch (e) {}
})
return articleIndexes
}
const log = createLogger('info')
const prefix = colors.cyan(colors.bold('[artg]'))
const format = (message: string) => {
return `${prefix} ${message}`
}
const createIndexesFile = () => {
const indexes = generateIndex()
const indexesFileName = join(ARTICLES_DIR, 'indexes.json')
writeFileSync(indexesFileName, `${JSON.stringify(indexes)}`)
log.info(format(`articles indexes has been created in ${indexesFileName}`))
}
if (argv.includes('--fix'))
createIndexesFile()
export default function ArticleIndexes() {
MODE = 'development'
return {
name: 'article-indexes-generator',
apply: 'serve',
buildStart() {
setTimeout(() => {
createIndexesFile()
})
},
handleHotUpdate({ file }: { file: string }) {
if (isArticleMD(file))
createIndexesFile()
},
}
}