UNPKG

2.25 kBJavaScriptView Raw
1/**
2 * @license
3 * MOST Web Framework 2.0 Codename Blueshift
4 * Copyright (c) 2017, THEMOST LP All rights reserved
5 *
6 * Use of this source code is governed by an BSD-3-Clause license that can be
7 * found in the LICENSE file at https://themost.io/license
8 */
9var fs = require('fs');
10var pagedown = require('pagedown');
11var Extra = require('./pagedown/pagedown-extra').Extra;
12var LangUtils = require('@themost/common/utils').LangUtils;
13var ArgumentError = require('@themost/common/utils').ArgumentError;
14var HttpViewEngine = require('../types').HttpViewEngine;
15/**
16 * @class
17 * @param {HttpContext=} context
18 * @constructor
19 * @augments {HttpViewEngine}
20 */
21function MarkdownEngine(context) {
22 MarkdownEngine.super_.bind(this)(context);
23}
24LangUtils.inherits(MarkdownEngine,HttpViewEngine);
25/**
26 * @param {string} file
27 * @param {*} data
28 * @param {Function} callback
29 */
30MarkdownEngine.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 * @type {Markdown.Converter|*}
43 */
44 var converter = new pagedown.Converter();
45 Extra.init(converter);
46 var result = converter.makeHtml(data);
47 //return the converted HTML markup
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 * @static
59 * @param {HttpContext=} context
60 * @returns {MarkdownEngine}
61 */
62MarkdownEngine.createInstance = function(context) {
63 return new MarkdownEngine(context);
64};
65
66if (typeof exports !== 'undefined') {
67 module.exports.MarkdownEngine = MarkdownEngine;
68 /**
69 * @param {HttpContext=} context
70 * @returns {MarkdownEngine}
71 */
72 module.exports.createInstance = function(context) {
73 return MarkdownEngine.createInstance(context);
74 };
75}
\No newline at end of file