-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbuild.js
More file actions
executable file
·128 lines (109 loc) · 3.2 KB
/
build.js
File metadata and controls
executable file
·128 lines (109 loc) · 3.2 KB
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
var dir = require('node-dir'),
async = require('async'),
fs = require('fs'),
fse = require('fs-extra'),
reactdown = require('reactdown'),
src = './metaphors';
root = './terms';
var markdown = {};
function saveFiles(terms, callback) {
var metaphors = Object.keys(terms);
var jsx = fs.readdir('./public/components', function(err, files) {
if (err) {
return callback(err);
};
files = files.map(function(file) {
return file.split('/').pop().split('.jsx')[0];
});
metaphors.forEach(function(scanTerm) {
var content = markdown[scanTerm];
metaphors.forEach(function(libTerm) {
if (libTerm === scanTerm) {
return;
}
var libFile = terms[libTerm];
var search = libFile.title.replace(' ', '[ |\n]'),
regex = new RegExp('(' + search + ')+', 'gi'),
found = false;
content = content.replace(regex, function(match) {
found = true;
return '[' + match + '][term-' + libFile.id + ']';
});
if (found) {
content += '\n[term-' + libFile.id + ']:' + libFile.href;
}
});
files.forEach(function(file) {
if (file !== 'index') {
content = content.replace(new RegExp('<' + file), function(match) {
return match += ' term={props.term} summary={props.summary}';
});
}
});
// Add Component definitions
var lines = content.split('\n'),
front = [lines.shift()];
front.push('scope:');
files.forEach(function(file) {
if (file !== 'Root' && file != 'index') {
front.push(' ' + file + ': ./../../public/components/' + file);
}
});
lines = front.concat(lines);
markdown[scanTerm] = lines.join('\n');
});
async.each(metaphors, function(metaphor, callback) {
var md = markdown[metaphor];
var js = reactdown(md, {}).code;
fse.outputFile(root + '/md/' + metaphor + '.md', md);
fse.outputFile(root + '/js/' + metaphor + '.js', js);
}, function(err) {
callback(err);
});
});
}
function buildMarkdown(callback) {
var terms = {};
dir.readFiles('./metaphors', {
match: /.md$/,
exclude: /^\./,
recursive: false
}, function(err, content, filename, next) {
if (err) {
return callback(err);
}
// Hacky, hacky...
content = content.replace(/(<\/?[A-Z]\S[^>]*\/?>)/gm, function(match) {
return '\n\n' + match + '\n\n';
});
var id = filename.split('/').pop().split('.md')[0],
contents = reactdown(content, {}),
term = contents.meta;
term.id = id;
term.path = {
'md': '/terms/md/' + id + '.md',
'js': '/terms/js/' + id + '.js'
};
term.href = '/term/' + id;
terms[id] = term;
markdown[id] = content;
next();
},
function() {
fse.outputFile(root + '/js/manifest.json', JSON.stringify(terms), function(err) {
saveFiles(terms, function(err) {
return callback(err);
});
});
}
);
}
function go(opts, callback) {
buildMarkdown(function(err, terms) {
if (err) {
return callback(err);
}
});
}
go({}, function() {});
module.exports = go;