UNPKG

1.23 kBJavaScriptView Raw
1var markit = require('markit');
2var colors = require('ansicolors');
3var styles = require('ansistyles');
4
5var allElements = [
6 'blockcode', 'blockhtml', 'blockquote', 'codespan', 'emphasis',
7 'header', 'hrule', 'image', 'linebreak', 'link', 'list', 'listitem',
8 'paragraph', 'strikethrough', 'strong', 'table', 'tablecell', 'tablerow'];
9
10exports.fromMarkdown = function(markdown) {
11
12 var r = new markit.Renderer();
13
14 // ignore all syntax by default
15 allElements.forEach(function(e) {
16 r[e] = function() { return ''; }
17 });
18
19 // paragraphs just pass through (automatically created by new lines)
20 r.paragraph = function(text) {
21 return text;
22 };
23
24 // lists = high-level command description
25 r.list = function(body, ordered) {
26 return body + '\n';
27 }
28
29 r.listitem = function(text) {
30 return ' ' + styles.italic(styles.bright(text)) + '\n';
31 };
32
33 // description for each example
34 r.header = function(text, level) {
35 if (level == 2) {
36 return colors.green(' - ' + text) + '\n';
37 } else {
38 return '';
39 }
40 };
41
42 // example code
43 r.codespan = function(code, lang) {
44 return ' ' + colors.white(' ' + colors.bgBlack(code)) + '\n\n';
45 };
46
47 return markit(markdown, {renderer: r});
48};