-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
27 lines (26 loc) · 1.47 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
var path = require('path');
module.exports = function metalsmithWikiWords() {
return function wikiWords(files, metalsmith, done) {
for(var file in files) {
var dirDepth = path.parse(file).dir ? path.normalize(path.parse(file).dir).split(path.sep).length : 0;
var fileContents = files[file].contents = files[file].contents.toString();
var wikiwords = new Set(fileContents.match(/[A-Z]([A-Z0-9]*[a-z][a-z0-9]*[A-Z]|[a-z0-9]*[A-Z][A-Z0-9]*[a-z])[A-Za-z0-9]*/g));
//var wikiwords = new Set(fileContents.match(/[A-Z][a-z][A-Za-z0-9]/g));
if (!wikiwords) continue;
wikiwords.forEach(function (wikiword) {
var fileLocation = Object.keys(files).find((file) => {
return path.parse(file).name.toLowerCase() === wikiword.toLowerCase() ? file : false;
});
if (!fileLocation) return;
fileLocation = path.parse(fileLocation).dir;
fileLocation += fileLocation ? '/' + wikiword : wikiword;
for (var a = 0; a < dirDepth; a++) fileLocation = '../' + fileLocation;
var t = new RegExp('([^a-zA-Z0-9]|^)'+ wikiword +'([^a-zA-Z0-9\\]]|$)', 'g');
var rep = '$1[' + wikiword + '](' + fileLocation.toLowerCase() + '.html)$2';
fileContents = fileContents.replace(t, rep);
});
files[file].contents = new Buffer(fileContents);
}
done();
}
}