forked from alexseman/markdown-render
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
73 lines (58 loc) · 1.38 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
/*
* Module dependencies
*/
var highlight = require('highlight.js')
var remarkable = require('remarkable')
var toc = require('markdown-toc')
/**
* Export render
*/
module.exports = render
/**
* Mappings between remarkable and highlight.js
*/
var language = {
'js': 'javascript',
'html': 'xml',
'shell': 'bash'
}
/**
* Highlight configuration
*/
highlight.configure({
tabReplace: ' '
})
/**
* Configure MD
*/
var md = new remarkable.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(code, {language: lang}).value
: highlight.highlightAuto(code).value
} catch (e) {}
return ''
}
})
/**
* Add in header anchors
*/
md.use(function (remarkable) {
remarkable.renderer.rules.heading_open = function (tokens, idx) {
return '<h' + tokens[idx].hLevel + ' id=' + toc.slugify(tokens[idx + 1].content) + '>'
}
})
/**
* Export `render`
*/
function render (src) {
return md.render(src)
}