1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | var fs = require('fs'),
|
11 | preprocessors = require('./preprocessors'),
|
12 | postprocessors = require('./postprocessors'),
|
13 | vm = require('vm');
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | var Showdown = (function () {
|
20 | var sandbox = {};
|
21 | var filename = __dirname + '/vendor/showdown/src/showdown.js';
|
22 | var content = fs.readFileSync(filename, 'utf8');
|
23 | vm.runInNewContext(content, sandbox);
|
24 | return sandbox.Showdown;
|
25 | })();
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | var exports = module.exports = function (content, options) {
|
38 | var doc = {raw: content, markdown: content};
|
39 | var opt = exports.readOptions(options);
|
40 | exports.preprocess(doc, opt);
|
41 | doc.html = exports.processor(doc.markdown);
|
42 | exports.postprocess(doc, opt);
|
43 | doc.toString = function () {
|
44 | return doc.html;
|
45 | };
|
46 | return doc;
|
47 | };
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | exports.processor = new Showdown.converter().makeHtml;
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | exports.preprocessors = preprocessors;
|
65 | exports.postprocessors = postprocessors;
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | exports.readOptions = function (options) {
|
76 | var obj = {
|
77 | preprocessors: [
|
78 | exports.preprocessors.metadata,
|
79 | exports.preprocessors.underscores
|
80 | ],
|
81 | postprocessors: []
|
82 | };
|
83 | for (var k in options) {
|
84 | obj[k] = options[k];
|
85 | }
|
86 | return obj;
|
87 | };
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 | exports.preprocess = function (doc, options) {
|
101 | return options.preprocessors.reduce(function (doc, fn) {
|
102 | return fn(doc);
|
103 | }, doc);
|
104 | };
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 | exports.postprocess = function (doc, options) {
|
118 | return options.postprocessors.reduce(function (doc, fn) {
|
119 | return fn(doc);
|
120 | }, doc);
|
121 | };
|