-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
Copy pathgetPermalink.ts
61 lines (54 loc) · 1.58 KB
/
getPermalink.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
import ensureEndingSlash from './ensureEndingSlash'
import ensureLeadingSlash from './ensureLeadingSlash'
interface PermalinkOption {
pattern: string;
slug: string;
date: string;
regularPath: string;
localePath: string;
}
function removeLeadingSlash (path: string) {
return path.replace(/^\//, '')
}
// e.g.
// filename: docs/_posts/evan you.md
// content: # yyx 990803
// date: 2018-08-14 11:22:33
// :year/:month/:day/:slug/ => 2018/08/14/evan-you/
// :year-:month-:day-:slug/ => 2018-08-14-evan-you/
// :year/:month/:day/:title/ => 2018/08/14/yyx 990803/
// :year/:month/:day/:original/ => 2018/08/14/_posts/evan you.html
export = function getPermalink ({
pattern,
slug,
date,
regularPath,
localePath = '/'
}: PermalinkOption) {
if (!pattern) {
return
}
slug = encodeURI(slug)
const d = new Date(date)
const year = d.getFullYear()
const iMonth = d.getMonth() + 1
const iDay = d.getDate()
const minutes = d.getMinutes()
const seconds = d.getSeconds()
const month = iMonth < 10 ? `0${iMonth}` : iMonth
const day = iDay < 10 ? `0${iDay}` : iDay
pattern = removeLeadingSlash(pattern)
const link =
localePath +
pattern
.replace(/:year/, String(year))
.replace(/:month/, String(month))
.replace(/:i_month/, String(iMonth))
.replace(/:i_day/, String(iDay))
.replace(/:day/, String(day))
.replace(/:minutes/, String(minutes))
.replace(/:seconds/, String(seconds))
.replace(/:slug/, slug)
.replace(/:regular/, removeLeadingSlash(regularPath))
return ensureLeadingSlash(ensureEndingSlash(link))
}