'use strict';

const path      = require('path');
const HTMLRenderer = require('./HTMLRenderer');
const render   = require('./render');

const log   = require('debug')('akasha:markdownRenderer');
const error = require('debug')('akasha:error-markdownRenderer');

// TODO can this configuration be injected?
const mditConfig = {
  html:         true,         // Enable html tags in source
  xhtmlOut:     false,        // Use '/' to close single tags (<br />)
  breaks:       false,        // Convert '\n' in paragraphs into <br>
  // langPrefix:   'language-',  // CSS language prefix for fenced blocks
  linkify:      true,         // Autoconvert url-like texts to links
  typographer:  false,        // Enable smartypants and other sweet transforms

  // Highlighter function. Should return escaped html,
  // or '' if input not changed
  highlight: function (/*str, , lang*/) { return ''; }
};
const mdit = require('markdown-it');
var md;

class MarkdownRenderer extends HTMLRenderer {
    constructor() {
        super(".html.md", /^(.*\.html)\.(md)$/);
        md = mdit(mditConfig);
    }
    
    renderSync(text, metadata) {
        // console.log('MarkdownRenderer renderSync '+ text);
        var ret = md.render(text);
        // console.log(ret);
        return ret;
    }
    
    render(text, metadata) {
        // console.log('MarkdownRenderer render');
        return new Promise((resolve, reject) => {
            try { 
                resolve(md.render(text)); 
            } catch(e) {
                reject(e);
            }
        });
    }
}

module.exports = new MarkdownRenderer();
@@ -392,30 +392,30 @@
 tmlOut:     
-fals
+tru
 e,
+ 
         // U
@@ -1079,37 +1079,33 @@
 tConfig);%0A    %7D%0A
-    
 %0A
+
     renderSync(t
@@ -1277,13 +1277,9 @@
   %7D%0A
-    
 %0A
+
     
@@ -1421,17 +1421,16 @@
    try %7B
- 
 %0A       
@@ -1463,17 +1463,16 @@
 (text));
- 
 %0A       
@@ -1551,16 +1551,16 @@
    %7D%0A%7D%0A%0A
-
 module.e
@@ -1591,8 +1591,9 @@
 derer();
+%0A

Use xhtmlOut so tags in EPUBs are rendered correctly