forked from nodejitsu/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
article.js
110 lines (89 loc) · 2.54 KB
/
article.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
var findit = require('findit'),
path = require('path'),
hl = require('../../vendor/highlight/lib/highlight').Highlight,
markdown = require('markdown'),
mkdirp = require('mkdirp'),
fs = require('fs'),
docs = require('../docs'),
weld = require('weld').weld,
helpers = require('../helpers');
var article = exports;
article.weld = function(dom, articles) {
//
// Remark: Create a short-cut reference to the jQuery object
//
var $ = docs.window.$;
// load all articles
var _articles = findit.sync('./articles');
//
// Filter out all non-markdown files
//
_articles = _articles.filter(function(a){
a = a.replace('./articles', '');
if(a.match(/\./)){
return false;
} else {
return true;
}
});
var toc = helpers.filesToTree(_articles);
Object.keys(articles).forEach(function(i){
var content;
try {
content = markdown.parse(articles[i].content.toString());
} catch (err) {
content = err.message;
}
var metadata = articles[i].metadata;
dom.innerHTML = docs.content.theme['./theme/article.html'].toString();
var data = {
metadata: metadata,
content: content,
toc: toc
};
weld(dom, data, {
map: function(parent, element, key, val) {
if(element.tagName === 'A') {
$(element).attr('href', val.substr(1, val.length-1));
$(element).html(path.basename(val));
return false
}
element.innerHTML = val;
return false;
}
});
// perform code highlighting
// convert only inside <code/>
dom.innerHTML = hl(dom.innerHTML, false, true);
articles[i].content = dom.innerHTML;
});
return dom;
};
article.generate = function(output, articles) {
//
// Generate all articles
//
Object.keys(articles).forEach(function(file){
var newPath = file.replace('./articles', '/../../public/articles');
newPath = path.dirname(newPath);
newPath = path.normalize(__dirname + newPath + '/index.html');
helpers.writeFile(newPath, articles[file].content, function(){});
});
return articles;
};
article.load = function (resolve) {
// load all articles
var articles = findit.sync('./articles');
//
// Filter out all non-markdown files
//
articles = articles.filter(function(a){
a = a.replace('./articles', '');
if(a.match(/\.md/)){
return true;
} else {
return false;
}
});
return helpers.articlesArrayToObject(articles, resolve);
};