-
Notifications
You must be signed in to change notification settings - Fork 270
/
Copy pathfixLinks.js
40 lines (33 loc) · 1015 Bytes
/
fixLinks.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
'use strict';
/* Fixes the previous/next links on collections to avoid linking to a page that doesn't exist for a device.
* For example, there's no billing guide for the Photon, so the next link on the previous page (code examples) should
* skip billing and go directly to the next page (open source).
*/
module.exports = function(options) {
var key = options.key;
var keySingular = key.replace(/s$/, "") + 'Value';
return function(files, metalsmith, done) {
Object.keys(files).forEach(function (fileName) {
var file = files[fileName];
var forks = file[key];
var thisFork = file[keySingular];
if (!forks) {
return;
}
if (file.next) {
file.next = updateLinks('next');
}
if (file.previous) {
file.previous = updateLinks('previous');
}
function updateLinks(direction) {
var target = file[direction];
while (target && target[key] && !target[key].includes(thisFork)) {
target = target[direction];
}
return target;
}
});
done();
};
};