forked from sindresorhus/gulp-markdown
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
124 lines (99 loc) · 2.76 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
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
'use strict';
var PLUGIN_NAME = 'gulp-markdown2html';
var gutil = require('gulp-util');
var through = require('through2');
var marked = require('marked');
var renderer = new marked.Renderer();
var jade = require('jade');
var path = require('path');
var merge = require('merge')
var toc = [];
//
// Block level renderer methods
//
// renderer.code = (code, language, escaped) => {};
renderer.blockquote = (quote) => {
return '<blockquote>\n<p>' + quote + '</p>\n</blockquote>\n';
};
// renderer.html = (html) => {};
renderer.heading = (text, level, raw) => {
var escapedText = text.toLowerCase().replace(/[^\w]+/g, '-');
if(level == 1) {
toc.push({
anchor: escapedText,
level: level,
text: text
});
}
return '<a class="anchor" id="' + escapedText + '"></a>\n' +
'<h' + level + '><a name="' +
escapedText +
'" class="text-muted" href="#' +
escapedText +
'"><button type="button" class="btn btn-link btn-xs"><span class="glyphicon glyphicon-link"></span></button></a>' +
text + '</h' + level + '>';
};
// renderer.hr = () => {};
// renderer.list = function(body, ordered) {};
// renderer.listitem = (text) => {};
// renderer.paragraph = (text) => {};
renderer.table = (header, body) => {
return '<table class="table table-bordered">\n' +
'<thead>\n' +
header +
'</thead>\n' +
'<tbody>\n' +
body +
'</tbody>\n' +
'</table>\n';
};
// renderer.tablerow = (content) => {};
// renderer.tablecell = (content, flags) => {};
//
// Inline level renderer methods
//
// renderer.strong = (text) => {};
// renderer.em = (text) => {};
// renderer.codespan = (code) => {};
// renderer.br = () => {};
// renderer.del = (text) => {};
// renderer.link = (href, tilte, text) => {};
// renderer.image = (href, title, text) => {};
// renderer.text = (text) => {};
module.exports = function(options) {
options = options || {};
options.highlightTheme = options.highlightTheme || 'github';
options.highlight = options.highlight || function(code) {
return require('highlight.js').highlightAuto(code).value;
};
return through.obj(function(file, enc, cb) {
if (file.isNull()) {
cb(null, file);
return;
}
if (file.isStream()) {
cb(new gutil.PluginError(PLUGIN_NAME, 'Streaming not supported'));
return;
}
marked(file.contents.toString(), merge(options, {
renderer: renderer
}), function(err, data) {
if (err) {
cb(new gutil.PluginError(PLUGIN_NAME, err, {
fileName: file.path
}));
return;
}
var locals = {
pkg: require('./package.json'),
toc: toc,
content: data
};
file.contents = new Buffer(jade.renderFile(__dirname + '/template/layout.jade', merge(options, locals)));
file.path = gutil.replaceExtension(file.path, '.html');
toc = [];
cb(null, file);
});
});
};
module.exports.marked = marked;