mdview.js 668 Bytes
"use strict";
const fs = require('fs');

function MarkdownIt (opts) {
    if (!(this instanceof MarkdownIt)) {
        return new MarkdownIt(opts);
    }

    opts = opts || {};

    const md = require('markdown-it')(opts.options);

    this.renderFile = function (filename) {
        if (!filename) {
            throw new Error('filename can not be null!')
        }

        let result;
        try {
            const content = fs.readFileSync(filename).toString();
            result = md.render(content);
        } catch (e) {
            result = md.render(`
# 404
*文件找不到!*
`)
        }

        return result;
    }
}

module.exports = MarkdownIt;