forked from metalsmith/permalinks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
164 lines (143 loc) · 4.76 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
var rimraf = require('rimraf');
var assert = require('assert');
var equal = require('assert-dir-equal');
var Metalsmith = require('metalsmith');
var permalinks = require('..');
var collections = require('metalsmith-collections');
describe('metalsmith-permalinks', function(){
before(function(done){
rimraf('test/fixtures/*/build', done);
});
it('should change files even with no pattern', function(done){
Metalsmith('test/fixtures/no-pattern')
.use(permalinks())
.build(function(err){
if (err) return done(err);
equal('test/fixtures/no-pattern/expected', 'test/fixtures/no-pattern/build');
done();
});
});
it('should replace a pattern', function(done){
Metalsmith('test/fixtures/pattern')
.use(permalinks({ pattern: ':title' }))
.build(function(err){
if (err) return done(err);
equal('test/fixtures/pattern/expected', 'test/fixtures/pattern/build');
done();
});
});
it('should accepts a shorthand string', function(done){
Metalsmith('test/fixtures/shorthand')
.use(permalinks(':title'))
.build(function(err){
if (err) return done(err);
equal('test/fixtures/shorthand/expected', 'test/fixtures/shorthand/build');
done();
});
});
it('should copy relative files to maintain references', function(done){
Metalsmith('test/fixtures/relative')
.use(permalinks())
.build(function(err){
if (err) return done(err);
equal('test/fixtures/relative/expected', 'test/fixtures/relative/build');
done();
});
});
it('should not copy relative files', function(done){
Metalsmith('test/fixtures/no-relative')
.use(permalinks({
relative: false
}))
.build(function(err){
if (err) return done(err);
equal('test/fixtures/no-relative/expected', 'test/fixtures/no-relative/build');
done();
});
});
it('should copy relative files even with patterns', function(done){
Metalsmith('test/fixtures/relative-pattern')
.use(permalinks(':title'))
.build(function(err){
if (err) return done(err);
equal('test/fixtures/relative-pattern/expected', 'test/fixtures/relative-pattern/build');
done();
});
});
it('should copy relative files once per output file', function(done){
Metalsmith('test/fixtures/relative-multiple')
.use(permalinks(':title'))
.build(function(err){
if (err) return done(err);
equal('test/fixtures/relative-multiple/expected', 'test/fixtures/relative-multiple/build');
done();
});
});
it('should format a date', function(done){
Metalsmith('test/fixtures/date')
.use(permalinks(':date'))
.build(function(err){
if (err) return done(err);
equal('test/fixtures/date/expected', 'test/fixtures/date/build');
done();
});
});
it('should format a date with a custom formatter', function(done){
Metalsmith('test/fixtures/custom-date')
.use(permalinks({
pattern: ':date',
date: 'YYYY/MM'
}))
.build(function(err){
if (err) return done(err);
equal('test/fixtures/custom-date/expected', 'test/fixtures/custom-date/build');
done();
});
});
it('should replace any backslashes in paths with slashes', function(done){
Metalsmith('test/fixtures/backslashes')
.use(permalinks())
.use(function (files, metalsmith, pluginDone) {
Object.keys(files).forEach(function(file){
assert.equal(files[file].path.indexOf('\\'), -1);
});
pluginDone();
done();
})
.build(function(err){
if (err) return done(err);
});
});
it('should ignore any files with permalink equal to false option', function(done){
Metalsmith('test/fixtures/false-permalink')
.use(permalinks(':title'))
.build(function(err){
if (err) return done(err);
equal('test/fixtures/false-permalink/expected', 'test/fixtures/false-permalink/build');
done();
});
});
it('should ignore files not in a collection', function(done){
Metalsmith('test/fixtures/collections')
.use(collections({
posts: {
pattern: 'in_collection.html'
}
}))
.use(permalinks({
pattern: ':title',
collection: 'posts'
}))
.use(function (files, metalsmith, done) {
assert.equal(2, Object.keys(files).length);
assert.equal(true, 'not_in_collection.html' in files);
assert.equal(true, 'in-collection/index.html' in files);
done();
})
.build(function(err){
if (err) return done(err);
equal('test/fixtures/pattern/expected', 'test/fixtures/pattern/build');
done();
});
});
});