var fs = require("fs")
var markdown = (require("markdown-it")({html: true})).use(require("markdown-it-deflist"))
var Mold = require("mold-template")

module.exports = function loadTemplates(config) {
  var mold = new Mold(config.env || {})
  fs.readdirSync(config.dir).forEach(function(filename) {
    var match = /^(.*?)\.html$/.exec(filename)
    if (match)
      mold.bake(match[1], fs.readFileSync(config.dir + match[1] + ".html", "utf8").trim())
  })
  mold.defs.markdown = function(options) {
    if (typeof options == "string") options = {text: options}
    let text = markdown.render(config.markdownFilter ? config.markdownFilter(options.text) : options.text)
    if (options.shiftHeadings) text = text.replace(/<h(\d)\b/ig, (_, d) => "<h" + (+d + options.shiftHeadings))
    return text
  }
  mold.defs.markdownFile = function(options) {
    if (typeof options == "string") options = {file: options}
    options.text = fs.readFileSync(config.markdownDir + options.file + ".md", "utf8")
    return mold.defs.markdown(options)
  }
  return mold
}

@@ -714,16 +714,21 @@
 place(/%3C
+(%5C/?)
 h(%5Cd)%5Cb/
@@ -734,16 +734,20 @@
 /ig, (_,
+ cl,
  d) =%3E %22
@@ -747,16 +747,26 @@
 d) =%3E %22%3C
+%22 + cl + %22
 h%22 + (+d

Fix header shifting to also shift closing tags