Skip to content

Commit bdcf097

Browse files
committed
feat: default date -> git -> mtime & date.format, close #45
1 parent bcd674f commit bdcf097

File tree

7 files changed

+68
-28
lines changed

7 files changed

+68
-28
lines changed

demo/yun/pages/docs/dev/index.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ end: false
1414
## Todo
1515

1616
- [ ] create-valaxy-theme: Script to generate theme template.
17-
- [ ] Github actions auto generate site from theme repo/npm.
1817
- [ ] Debug component.
19-
- [ ] Git timestamp for post without `date`.
2018
- [ ] local search
2119

2220
## Dev

packages/valaxy/node/config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ const defaultValaxyConfig: ValaxyConfig = {
4747
},
4848
social: [],
4949

50+
date: {
51+
format: '',
52+
},
5053
lastUpdated: true,
5154

5255
license: {

packages/valaxy/node/plugins/preset.ts

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import fs from 'fs'
22
import type { PluginOption } from 'vite'
33
import { splitVendorChunkPlugin } from 'vite'
44

5-
import consola from 'consola'
6-
75
import MarkdownIt from 'markdown-it'
86
import matter from 'gray-matter'
97

@@ -13,10 +11,10 @@ import Layouts from 'vite-plugin-vue-layouts'
1311
import Components from 'unplugin-vue-components/vite'
1412
import VueI18n from '@intlify/vite-plugin-vue-i18n'
1513

16-
import { dim, yellow } from 'kolorist'
1714
import type { ResolvedValaxyOptions, ValaxyPluginOptions, ValaxyServerOptions } from '../options'
1815
import { setupMarkdownPlugins } from '../markdown'
1916
// import { createMarkdownPlugin, excerpt_separator } from './markdown'
17+
import { formatMdDate } from '../utils/date'
2018
import { createUnocssPlugin } from './unocss'
2119
import { createConfigPlugin } from './extendConfig'
2220
import { createClientSetupPlugin } from './setupClient'
@@ -117,28 +115,23 @@ export async function ViteValaxyPlugins(
117115
})
118116

119117
// page is post
120-
let isPost = false
121-
if (route.path.startsWith('/posts/')) {
122-
isPost = true
118+
if (route.path.startsWith('/posts/'))
123119
route.meta.layout = 'post'
124-
}
125120

126121
// set default frontmatter
127-
const defaultFrontmatter = {
128-
date: new Date(),
129-
}
130-
if (!route.meta.frontmatter)
131-
route.meta.frontmatter = defaultFrontmatter
122+
const defaultFrontmatter = {}
132123

133124
if (path.endsWith('.md')) {
134125
const md = fs.readFileSync(path, 'utf-8')
135126
const { data, excerpt } = matter(md, { excerpt_separator: '<!-- more -->' })
136127

137-
if (isPost) {
138-
// warn for post frontmatter
139-
if (!data.date)
140-
consola.warn(`You forgot to write ${yellow('date')} for post: ${dim(`${route.component}`)}`)
141-
}
128+
// todo, optimize it to cache or on demand
129+
formatMdDate(
130+
data,
131+
path,
132+
options.config.date.format,
133+
options.config.lastUpdated,
134+
)
142135

143136
route.meta = Object.assign(route.meta, {
144137
frontmatter: Object.assign(defaultFrontmatter, data),

packages/valaxy/node/rss.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type { Author, FeedOptions, Item } from 'feed'
99
import { Feed } from 'feed'
1010
import consola from 'consola'
1111
import type { ResolvedValaxyOptions } from './options'
12+
import { formatMdDate } from './utils/date'
1213

1314
const markdown = MarkdownIt({
1415
html: true,
@@ -64,17 +65,13 @@ export async function build(options: ResolvedValaxyOptions) {
6465
const raw = fs.readFileSync(i, 'utf-8')
6566
const { data, content, excerpt } = matter(raw)
6667

67-
// not add to posts
68-
if (!data.date) {
69-
consola.warn(`Do you forget to write date for ${dim(i)}`)
70-
return false
71-
}
72-
7368
if (data.draft) {
7469
consola.warn(`Ignore draft post: ${dim(i)}`)
7570
return false
7671
}
7772

73+
formatMdDate(data, i, config.date.format, config.lastUpdated)
74+
7875
// todo i18n
7976

8077
// render excerpt

packages/valaxy/node/utils/date.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import dayjs from 'dayjs'
2+
import fs from 'fs-extra'
3+
import { getGitTimestamp } from './getGitTimestamp'
4+
5+
/**
6+
* get created time of file
7+
* @param file
8+
* @returns
9+
*/
10+
export async function getCreatedTime(file: string): Promise<Date | number> {
11+
return await getGitTimestamp(file, 'created') || (await fs.stat(file)).ctime
12+
}
13+
14+
/**
15+
* get created time of file
16+
* @param file
17+
* @returns
18+
*/
19+
export async function getUpdatedTime(file: string): Promise<Date | number> {
20+
return await getGitTimestamp(file, 'created') || (await fs.stat(file)).mtime
21+
}
22+
23+
export function formatMdDate(data: any, path: string, format = 'YYYY-MM-DD HH:mm:ss', lastUpdated = true) {
24+
if (!data.date)
25+
data.date = getCreatedTime(path)
26+
27+
if (!data.updated && lastUpdated)
28+
data.updated = getUpdatedTime(path)
29+
30+
// format
31+
data.date = dayjs(data.date).format(format)
32+
33+
if (lastUpdated)
34+
data.updated = dayjs(data.updated).format(format)
35+
}

packages/valaxy/node/utils/getGitTimestamp.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
import { spawn } from 'cross-spawn'
22

3-
export function getGitTimestamp(file: string) {
3+
export function getGitTimestamp(file: string, type: 'created' | 'updated' = 'updated') {
44
return new Promise<number>((resolve, reject) => {
5-
const child = spawn('git', ['log', '-1', '--pretty="%ci"', file])
5+
const params = ['log']
6+
if (type === 'updated')
7+
params.push('-1')
8+
params.push('--pretty="%ci"', file)
9+
if (type === 'created')
10+
params.push('|', 'tail', '-1')
11+
12+
const child = spawn('git', params)
613
let output = ''
714
child.stdout.on('data', d => (output += String(d)))
815
child.on('close', () => {

packages/valaxy/types/config.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,15 @@ export interface ValaxyConfig<T = ValaxyThemeConfig> {
8484
}
8585
}
8686

87+
date: {
88+
/**
89+
* The format of date
90+
* @default '' as 'YYYY-MM-DD HH:mm:ss'
91+
*/
92+
format: string
93+
}
8794
/**
88-
* show last updated time by git
95+
* show last updated time by git/mtime
8996
*/
9097
lastUpdated: boolean
9198

0 commit comments

Comments
 (0)