-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
66 lines (53 loc) · 1.95 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
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
'use strict';
var stream = require('stream');
var util = require('util');
var adaptHtml = require('./lib/adapt-html');
var Transform = stream.Transform || require('readable-stream').Transform;
/**
* Transforms the piped jsdoc generated html to make it usable on github, i.e. in a Readme or wiki
* The resulting html has the following transformations:
* - all code sample links are redirected point to gihub repo blob
* - wrap in div.jsdoc-githubify for styling
*
* #### Note:
* The github blob url is derived from the remote of the github repository in the current directory
* and the currently checked out branch is used. You can override these with the following environment vars:
*
* - JSDOC_GITHUBIFY_REMOTE
* - JSDOC_GITHUBIFY_BRANCH
*
* @name gitifyTransform
* @function
* @param {String} file filename ignored by this transform
* @return {TransformStream} into which to pipe the html string generated by jsdoc
*/
module.exports = function gitifyTransform(file /* not used */) {
return new GitifyTransform();
}
util.inherits(GitifyTransform, Transform);
function GitifyTransform (opts) {
if (!(this instanceof GitifyTransform)) return new GitifyTransform(opts);
opts = opts || {};
Transform.call(this, opts);
this.original = '';
}
GitifyTransform.prototype._transform = function (chunk, encoding, cb) {
this.original += encoding === 'utf8' ? chunk : chunk.toString();
cb();
};
GitifyTransform.prototype._flush = function (cb) {
var self = this;
// if the document contains no API doc we filter it by transforming to empty string
if (!adaptHtml.hasApi(self.original)) return cb();
adaptHtml(self.original, function (err, html) {
if (err) return cb(err);
var lines = html.split('\n');
// trim lines and remove empties
var trimmedhtml = lines
.map(function (x) { return x.trim() })
.filter(function (x) { return x.length })
.join('\n');
self.push(trimmedhtml);
cb();
});
};