-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
107 lines (86 loc) · 1.83 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
/**
* Module Dependencies
*/
var highlight = require('highlight.js')
var Remarkable = require('remarkable')
var extname = require('path').extname
var s2j = require('string-to-js')
var through = require('through')
/**
* Export `markdown`
*/
module.exports = markdown
/**
* Default regexp
*/
var rtype = /^(md|markdown)$/
/**
* Mappings between remarkable and highlight.js
*/
var language = {
'js': 'javascript',
'html': 'xml',
'shell': 'bash'
}
/**
* Highlight configuration
*/
highlight.configure({
tabReplace: ' '
})
/**
* Default remarkable configuration
*/
var md = new Remarkable({
html: true, // Enable HTML tags in source
xhtmlOut: true, // Use '/' to close single tags (<br />)
breaks: true, // Convert '\n' in paragraphs into <br>
langPrefix: 'lang ', // CSS language prefix for fenced blocks
highlight: function (code, lang) {
// differences between remarkable and highlight.js
lang = (language[lang]) ? language[lang] : lang
// Let's not let syntax highlighting kill anything
try {
return lang
? highlight.highlight(lang, code).value
: highlight.highlightAuto(code).value
} catch(e) {}
return ''
}
})
/**
* Initialize `transform`
*
* @param {Object} opts
*/
function markdown (file, opts) {
if (opts) md.set(opts)
var type = extension(file)
if (!rtype.test(type)) return through()
var data = ''
return through(write, end)
// write
function write (buf) {
data += buf
}
// end
function end () {
try {
var src = s2j(md.render(data))
} catch (e) {
this.emit('error', e)
return
}
this.queue(src)
this.queue(null)
}
}
/**
* Get the file extension
*
* @param {String} file
* @return {String}
*/
function extension (file) {
return extname(file).slice(1)
}