-
Notifications
You must be signed in to change notification settings - Fork 1
/
.eleventy.js
133 lines (112 loc) · 3.96 KB
/
.eleventy.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
const fs = require('node:fs');
const path = require('node:path');
const {
runCommand,
runCommandSync,
getFolderCreatedDate,
getFolderModifiedDate,
getFolderCreatedDateSync,
getFolderModifiedDateSync
} = require('./utils.js');
const TIMESTAMPS = {
'FS_CREATED': 'Date. FS. Created',
'FS_LAST_MODIFIED': 'Date. FS. Last Modified',
'GIT_CREATED': 'Date. Git. Created',
'GIT_LAST_MODIFIED': 'Date. Git. Last Modified',
};
const MODE = {
'ASYNC': 'ASYNC',
'SYNC': 'SYNC',
};
const strategiesSync = {
[TIMESTAMPS.FS_LAST_MODIFIED](contentPath) {
const stats = fs.statSync(contentPath);
return stats.isFile() ? stats.mtime : getFolderModifiedDateSync(contentPath);
},
[TIMESTAMPS.FS_CREATED](contentPath) {
const stats = fs.statSync(contentPath);
return stats.isFile() ? stats.birthtime : getFolderCreatedDateSync(contentPath);
},
[TIMESTAMPS.GIT_LAST_MODIFIED](contentPath) {
const args = ['--no-pager', 'log', '-n', '1', '--format="%ci"', contentPath];
const date = runCommandSync('git', args).trim();
return date ? new Date(date) : null;
},
[TIMESTAMPS.GIT_CREATED](contentPath) {
const args = ['--no-pager', 'log', '--diff-filter=A', '--follow', '-1', '--format="%ci"', contentPath];
const date = runCommandSync('git', args).trim();
return date ? new Date(date) : null;
}
}
const strategiesAsync = {
async [TIMESTAMPS.FS_LAST_MODIFIED](contentPath) {
const stats = await fs.promises.stat(contentPath);
return stats.isFile() ? stats.mtime : await getFolderModifiedDate(contentPath);
},
async [TIMESTAMPS.FS_CREATED](contentPath) {
const stats = await fs.promises.stat(contentPath);
return stats.isFile() ? stats.birthtime : await getFolderCreatedDate(contentPath);
},
async [TIMESTAMPS.GIT_LAST_MODIFIED](contentPath) {
const args = ['--no-pager', 'log', '-n', '1', '--format="%ci"', contentPath];
const date = (await runCommand('git', args)).trim();
return date ? new Date(date) : null;
},
async [TIMESTAMPS.GIT_CREATED](contentPath) {
const args = ['--no-pager', 'log', '--diff-filter=A', '--follow', '-1', '--format="%ci"', contentPath];
const date = (await runCommand('git', args)).trim();
return date ? new Date(date) : null;
}
}
function getContentFolderPath(data) {
return path.dirname(data.page.inputPath);
}
function getContentFilePath(data) {
return data.page.inputPath;
}
function computeDate(options) {
const {
strategy: strategyKey,
contentPath,
mode = MODE.ASYNC
} = options;
const strategy = (mode === MODE.ASYNC ? strategiesAsync : strategiesSync)[strategyKey];
if (!strategy) {
return;
}
if (!contentPath) {
return;
}
return strategy(contentPath);
}
const EFFECT_FIELD_NAME = '__EFFECT_COMPUTED_DATE_FIELD__';
const timestampsSet = new Set(Object.values(TIMESTAMPS));
function EleventyPlugin(eleventyConfig, userOptions = {}) {
const getContentPathFunction = userOptions.getContentPath ?? getContentFilePath;
const mode = userOptions.mode ?? MODE.ASYNC;
eleventyConfig.addGlobalData(`eleventyComputed.${EFFECT_FIELD_NAME}`, () => {
return async function(data) {
for (const [key, value] of Object.entries(data)) {
if (timestampsSet.has(value)) {
data[key] = await computeDate({
strategy: value,
contentPath: await getContentPathFunction(data),
mode
});
}
}
}
});
}
module.exports = EleventyPlugin;
module.exports.EleventyPlugin = EleventyPlugin;
module.exports.eleventyPlugin = EleventyPlugin;
module.exports.EleventyPluginContentDates = EleventyPlugin;
module.exports.eleventyPluginContentDates = EleventyPlugin;
module.exports.TIMESTAMPS = TIMESTAMPS;
module.exports.MODE = MODE;
module.exports.strategiesAsync = strategiesAsync;
module.exports.strategiesSync = strategiesSync;
module.exports.getContentFilePath = getContentFilePath;
module.exports.getContentFolderPath = getContentFolderPath;
module.exports.computeDate = computeDate;