-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnpm.js
122 lines (103 loc) · 2.83 KB
/
npm.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
'use strict';
var fs = require('fs')
, path = require('path')
, renderme = require('renderme')
, npmdir = path.join(require.resolve('npm'), '../..', 'doc')
, jitsudir = path.join(require.resolve('nodejitsu-handbook'), '..', 'content/npm');
/**
* Representation of a single HELP document.
*
* @constructor
* @param {String} filename The name of the help file.
* @param {String} path The absolute location of the file.
* @param {Object} github Github user/repo of the help files.
* @param {Function} preprocess Optional content pre-processor.
* @api public
*/
function Help(filename, path, github, preprocess) {
if (!(this instanceof Help)) return new Help(filename, path, preprocess);
this.html = '';
this.path = path;
this.github = github;
this.filename = filename;
this.preprocess = preprocess;
this.url = filename.slice(0, -3);
this.title = filename.slice(0, -3).replace(/\-/g, ' ');
if (this.title in Help.rename) {
this.title = Help.rename[this.title];
}
}
/**
* Update the titles to something useful.
*
* @type {Object}
* @api private
*/
Help.rename = {
index: 'Getting started'
};
/**
* Parse the markdown files.
*
* @param {Function} fn Callback
* @api public
*/
Help.prototype.render = function render(fn) {
if (this.html) return fn(undefined, this.html);
var content = fs.readFileSync(this.path, 'utf-8')
, help = this;
if (this.preprocess) content = this.preprocess(content);
renderme({
readmeFilename: this.filename,
readme: content
}, {
trimmed: Infinity,
github: this.github
}, function rendered(err, html) {
if (err) return fn(err);
fn(err, help.html = html);
});
};
//
// Expose the items.
//
var items = Object.create(null);
items.private = Object.create(null);
//
// Find all the different categories from the directly and walk each sub file to
// populate the items object.
//
fs.readdirSync(npmdir).forEach(function each(folder) {
items[folder] = Object.create(null);
fs.readdirSync(path.join(npmdir, folder)).filter(function filter(file) {
return '.md' === path.extname(file);
}).forEach(function each(item) {
var help = new Help(item, path.join(npmdir, folder, item), {
user: 'npm',
repo: 'npm'
}, function preprocess(content) {
var index;
if (~(index = content.indexOf('## SEE ALSO'))) {
content = content.slice(0, index).trim();
}
return content;
});
items[folder][help.url] = help;
});
});
//
// Read the documentation from nodejitsu-handbook.
//
fs.readdirSync(jitsudir).filter(function filter(file) {
return '.md' === path.extname(file);
}).forEach(function each(item) {
var help = new Help(item, path.join(jitsudir, item), {
user: 'nodejitsu',
repo: 'handbook/content/npm'
});
items.private[help.url] = help;
});
//
// Expose the items.
//
module.exports = items;