1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | var fs = require('fs');
|
10 | var pagedown = require('pagedown');
|
11 | var Extra = require('./pagedown/pagedown-extra').Extra;
|
12 | var LangUtils = require('@themost/common/utils').LangUtils;
|
13 | var ArgumentError = require('@themost/common/utils').ArgumentError;
|
14 | var HttpViewEngine = require('../types').HttpViewEngine;
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | function MarkdownEngine(context) {
|
22 | MarkdownEngine.super_.bind(this)(context);
|
23 | }
|
24 | LangUtils.inherits(MarkdownEngine,HttpViewEngine);
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | MarkdownEngine.prototype.render = function(file, data, callback) {
|
31 | callback = callback || function () {};
|
32 | try {
|
33 | if (typeof file !== 'string') {
|
34 | return callback(new ArgumentError("Markdown template URI must be a string."));
|
35 | }
|
36 | fs.readFile(file, 'utf8', function (err, data) {
|
37 | if (err) {
|
38 | return callback(err);
|
39 | }
|
40 | try {
|
41 | |
42 |
|
43 |
|
44 | var converter = new pagedown.Converter();
|
45 | Extra.init(converter);
|
46 | var result = converter.makeHtml(data);
|
47 |
|
48 | return callback(null, result);
|
49 | } catch (err) {
|
50 | return callback(err);
|
51 | }
|
52 | });
|
53 | } catch (err) {
|
54 | return callback(err);
|
55 | }
|
56 | };
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | MarkdownEngine.createInstance = function(context) {
|
63 | return new MarkdownEngine(context);
|
64 | };
|
65 |
|
66 | if (typeof exports !== 'undefined') {
|
67 | module.exports.MarkdownEngine = MarkdownEngine;
|
68 | |
69 |
|
70 |
|
71 |
|
72 | module.exports.createInstance = function(context) {
|
73 | return MarkdownEngine.createInstance(context);
|
74 | };
|
75 | } |
\ | No newline at end of file |