-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcommon.ts
More file actions
63 lines (48 loc) · 1.74 KB
/
common.ts
File metadata and controls
63 lines (48 loc) · 1.74 KB
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
import moment from 'moment-timezone';
import micromatch from 'micromatch';
const DURATION_MINUTE = 1000 * 60;
function isMatch(path: string, patterns?: string| string[]) {
if (!patterns) return false;
return micromatch.isMatch(path, patterns);
}
function isTmpFile(path: string) {
return path.endsWith('%') || path.endsWith('~');
}
function isHiddenFile(path: string) {
return /(^|\/)[_.]/.test(path);
}
function isExcludedFile(path: string, config) {
if (isTmpFile(path)) return true;
if (isMatch(path, config.exclude)) return true;
if (isHiddenFile(path) && !isMatch(path, config.include)) return true;
return false;
}
export {isTmpFile};
export {isHiddenFile};
export {isExcludedFile};
// This function is used by `asset.ts` and `post.ts`
// To handle dates like `date: Apr 24 2014` in front-matter
export function toDate(date?: string | number | Date | moment.Moment): Date | undefined | moment.Moment {
if (!date || moment.isMoment(date)) return date as any;
if (!(date instanceof Date)) {
// hexo-front-matter now always returns date in UTC
// but `new Date()` uses local timezone by default
// We have to reset offset
// to make the behavior consistent with hexo-front-matter
date = new Date(date);
const ms = date.getTime();
const offset = date.getTimezoneOffset();
const diff = offset * DURATION_MINUTE;
date = new Date(ms - diff);
}
if (isNaN(date.getTime())) return;
return date;
}
export function adjustDateForTimezone(date: Date | moment.Moment, timezone: string) {
if (moment.isMoment(date)) date = date.toDate();
const ms = date.getTime();
const target = moment.tz.zone(timezone).utcOffset(ms);
const diff = target * DURATION_MINUTE;
return new Date(ms + diff);
}
export {isMatch};