-
Notifications
You must be signed in to change notification settings - Fork 67
/
index.js
187 lines (156 loc) · 4.08 KB
/
index.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
var debug = require('debug')('metalsmith-permalinks');
var moment = require('moment');
var path = require('path');
var slug = require('slug-component');
var substitute = require('substitute');
var basename = path.basename;
var dirname = path.dirname;
var extname = path.extname;
var join = path.join;
/**
* Expose `plugin`.
*/
module.exports = plugin;
/**
* Metalsmith plugin that renames files so that they're permalinked properly
* for a static site, aka that `about.html` becomes `about/index.html`.
*
* @param {Object} options
* @property {String} pattern
* @property {String or Function} date
* @return {Function}
*/
function plugin(options){
options = normalize(options);
var pattern = options.pattern;
var dupes = {};
return function(files, metalsmith, done){
setImmediate(done);
Object.keys(files).forEach(function(file){
debug('checking file: %s', file);
if (!html(file)) return;
var data = files[file];
if (data['permalink'] === false) return;
var path = replace(pattern, data, options) || resolve(file);
var fam = family(file, files);
if (options.relative) {
// track duplicates for relative files to maintain references
for (var key in fam) {
var rel = join(path, key);
dupes[rel] = fam[key];
}
}
// add to path data for use in links in templates
data.path = '.' == path ? '' : path;
var out = join(path, options.indexFile || 'index.html');
delete files[file];
files[out] = data;
});
// add duplicates for relative files after processing to avoid double-dipping
// note: `dupes` will be empty if `options.relative` is false
Object.keys(dupes).forEach(function(dupe){
files[dupe] = dupes[dupe];
})
};
}
/**
* Normalize an options argument.
*
* @param {String or Object} options
* @return {Object}
*/
function normalize(options){
if ('string' == typeof options) options = { pattern: options };
options = options || {};
options.date = options.date ? format(options.date) : format('YYYY/MM/DD');
options.relative = options.hasOwnProperty('relative') ? options.relative : true;
return options;
}
/**
* Return a formatter for a given moment.js format `string`.
*
* @param {String} string
* @return {Function}
*/
function format(string){
return function(date){
return moment(date).utc().format(string);
};
}
/**
* Get a list of sibling and children files for a given `file` in `files`.
*
* @param {String} file
* @param {Object} files
* @return {Object}
*/
function family(file, files){
var dir = dirname(file);
var ret = {};
if ('.' == dir) dir = '';
for (var key in files) {
if (key == file) continue;
if (key.indexOf(dir) != 0) continue;
if (html(key)) continue;
var rel = key.slice(dir.length);
ret[rel] = files[key];
}
return ret;
}
/**
* Resolve a permalink path string from an existing file `path`.
*
* @param {String} path
* @return {String}
*/
function resolve(path){
var ret = dirname(path);
var base = basename(path, extname(path));
if (base != 'index') ret = join(ret, base).replace('\\', '/');
return ret;
}
/**
* Replace a `pattern` with a file's `data`.
*
* @param {String} pattern (optional)
* @param {Object} data
* @param {Object} options
* @return {String or Null}
*/
function replace(pattern, data, options){
if (!pattern) return null;
var keys = params(pattern);
var ret = {};
for (var i = 0, key; key = keys[i++];) {
var val = data[key];
if (val == null) return null;
if (val instanceof Date) {
ret[key] = options.date(val);
} else {
ret[key] = slug(val.toString());
}
}
return substitute(pattern, ret);
}
/**
* Get the params from a `pattern` string.
*
* @param {String} pattern
* @return {Array}
*/
function params(pattern){
var matcher = /:(\w+)/g;
var ret = [];
var m;
while (m = matcher.exec(pattern)) ret.push(m[1]);
return ret;
}
/**
* Check whether a file is an HTML file.
*
* @param {String} path
* @return {Boolean}
*/
function html(path){
return /.html/.test(extname(path));
}