forked from hexojs/hexo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
post_permalink.js
70 lines (55 loc) · 1.82 KB
/
post_permalink.js
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
'use strict';
const { createSha1Hash, Permalink, slugize } = require('hexo-util');
const { basename } = require('path');
let permalink;
function postPermalinkFilter(data) {
const { config } = this;
const { id, _id, slug, title, date, __permalink } = data;
if (__permalink) {
if (!__permalink.startsWith('/')) return `/${__permalink}`;
return __permalink;
}
const hash = slug && date
? createSha1Hash().update(slug + date.unix().toString()).digest('hex').slice(0, 12)
: null;
const meta = {
id: id || _id,
title: slug,
name: typeof slug === 'string' ? basename(slug) : '',
post_title: slugize(title, {transform: 1}),
year: date.format('YYYY'),
month: date.format('MM'),
day: date.format('DD'),
hour: date.format('HH'),
minute: date.format('mm'),
second: date.format('ss'),
i_month: date.format('M'),
i_day: date.format('D'),
hash
};
if (!permalink || permalink.rule !== config.permalink) {
permalink = new Permalink(config.permalink);
}
const { categories } = data;
if (categories.length) {
meta.category = categories.last().slug;
} else {
meta.category = config.default_category;
}
const keys = Object.keys(data);
for (const key of keys) {
if (Object.prototype.hasOwnProperty.call(meta, key)) continue;
// Use Object.getOwnPropertyDescriptor to copy getters to avoid "Maximum call
// stack size exceeded" error
Object.defineProperty(meta, key, Object.getOwnPropertyDescriptor(data, key));
}
if (config.permalink_defaults) {
const keys2 = Object.keys(config.permalink_defaults);
for (const key of keys2) {
if (Object.prototype.hasOwnProperty.call(meta, key)) continue;
meta[key] = config.permalink_defaults[key];
}
}
return permalink.stringify(meta);
}
module.exports = postPermalinkFilter;