var jsYaml = require('js-yaml')
  , path = require('path')
  , fs = require('fs')

jsYaml.parse = function (text, name) {
  name = name || '__content';
  var re = /^(-{3}(?:\n|\r)([\w\W]+)-{3})?([\w\W]*)*/
    , results = re.exec(text)
    , conf = {}
    , yamlOrJson;

  if((yamlOrJson = results[2])) {
    if(yamlOrJson.charAt(0) === '{') { 
      conf = JSON.parse(yamlOrJson);
    } else {
      conf = jsYaml.load(yamlOrJson);
    }
  }

  conf[name] = results[3] ? results[3] : '';

  return conf;
};

jsYaml.loadFront = function (context, name) {
  var contents;
  if(fs.existsSync(context)) {
    contents = fs.readFileSync(context, 'utf8');
    if (contents instanceof Error) return contents;
    return jsYaml.parse(contents, name);
  } else if (Buffer.isBuffer(context)) {
    return jsYaml.parse(context.toString(), name);
  } else {
    return jsYaml.parse(context, name);
  }
  return false;
};

module.exports = jsYaml;

var jsYaml = require('js-yaml')
  , path = require('path')
  , fs = require('fs')

jsYaml.parse = function (text, name) {
  name = name || '__content';
  var re = /^(-{3}(?:\n|\r)([\w\W]+?)-{3})?([\w\W]*)*/
    , results = re.exec(text)
    , conf = {}
    , yamlOrJson;

  if((yamlOrJson = results[2])) {
    if(yamlOrJson.charAt(0) === '{') { 
      conf = JSON.parse(yamlOrJson);
    } else {
      conf = jsYaml.load(yamlOrJson);
    }
  }

  conf[name] = results[3] ? results[3] : '';

  return conf;
};

jsYaml.loadFront = function (context, name) {
  var contents;
  if(fs.existsSync(context)) {
    contents = fs.readFileSync(context, 'utf8');
    if (contents instanceof Error) return contents;
    return jsYaml.parse(contents, name);
  } else if (Buffer.isBuffer(context)) {
    return jsYaml.parse(context.toString(), name);
  } else {
    return jsYaml.parse(context, name);
  }
  return false;
};

module.exports = jsYaml;

Make the front matter closing dashes matcher lazy
Make the front matter closing dashes matcher lazy

I was having a bit of trouble parsing several Markdown files with front matter, getting errors like these:

> Fatal error: JS-YAML: expected a single document in the stream, but found more

This was caused by Markdown headers like these:

    Chapter IV: A new acquaintance
    ------------------------------

Since the plus in the `([\w\W]+)` is greedy, it eat the document all the way down to last instance of `---` in our document, which would the header.

The fix is to add a question mark, making the plus lazy, like this `([\w\W]+?)`. That causes only the front matter to be extracted.