mermaid
Version:
Markdownish syntax for generating flowcharts
68 lines (56 loc) • 2.23 kB
JavaScript
/**
* Created by knut on 14-12-10.
*/
// through2 is a thin wrapper around node transform streams
var through = require('through2');
var gutil = require('gulp-util');
// consts
const PLUGIN_NAME = 'gulp-dox2Md';
module.exports = function () {
return through.obj(function (file, enc, cb) {
if (file.isNull()) {
cb(null, file);
}
if (file.isStream()) {
cb(new gutil.PluginError(PLUGIN_NAME, 'Streaming not supported'));
return;
}
try {
var fileTxt = file.contents.toString();
var obj = JSON.parse(file.contents.toString());
var res = '';
obj.forEach(function(item){
if(typeof item.ctx === 'object'){
res = res + '#' + item.ctx.type + ' ' + item.ctx.name + '\n';
res = res + item.description.full + '\n\n';
}
if(typeof item.tags === 'object'){
var paramBlock = '';
var returnBlock = '';
item.tags.forEach(function(tag){
// Add the params
if(tag.type === 'param'){
paramBlock = paramBlock + '* ' + tag.name + '-' + tag.description + '\n';
}
// Add data about the return statement
if(tag.type === 'returns'){
returnBlock = returnBlock + '* ' + tag.string + '\n';
}
});
// Only apply header of there actually are something to display
if(paramBlock.length >0){
res = res + '**Parameters**\n\n'+ paramBlock + '\n\n';
}
if(returnBlock.length >0){
res = res + '**Returns**\n\n'+ returnBlock + '\n\n';
}
}
});
file.contents = new Buffer(res);
this.push(file);
} catch (err) {
this.emit('error', new gutil.PluginError('gulp-strip-json-comments', err, {fileName: file.path}));
}
cb();
});
};