index.js
function matter(str, opts) {

  str = formatString(str);
  opts = formatOptions(opts);
  var result = {src: str, data: null, body: str};
  if (!str) {
    return result;
  }

  var strict = !opts.loose;
  var header = opts.delims[0];
  var footer = opts.delims[1];
  var dataStart, dataEnd;
  // Front matter must start from the first byte.
  if (str.substr(0, header.length) !== header ||
      // Whether the delimiter is followed by a strange character.
      (!opts.loose && header.length < str.length &&
          str[header.length] !== '\n' &&
          str[header.length] === header[header.length-1]) ||
      // Metadata and delimiters should be separated with linefeeds.
      (dataStart = str.indexOf('\n', header.length)) < 0 ||
      (dataEnd = str.indexOf('\n' + footer, dataStart)) < 0) {
    return result;
  }
  var bodyStart = dataEnd + 1 + footer.length;
  // Whether the delimiter is followed by strange characters.
  if (strict && bodyStart < str.length) {
    if (str[bodyStart] !== '\n' && str[bodyStart] === footer[footer.length-1]) {
      return result;
    }
    while (bodyStart < str.length && str[bodyStart] !== '\n') {
      if (/^[^\s]$/.test(str[bodyStart])) {
        return result;
      }
      bodyStart++;
    }
  }  // else: Tolerate the case that a linefeed is missing: <end-delimiter><body>
  if (str[bodyStart] === '\r') { bodyStart++; }
  if (str[bodyStart] === '\n') { bodyStart++; }
  var data;
  if (dataStart < dataEnd &&
      (data = str.substr(dataStart, dataEnd - dataStart).trim())) {
    var lang = str.substr(header.length, dataStart - header.length)
                  .trim().toLowerCase() || opts.lang;
    var parse;
    if (typeof opts.parsers === 'function') {
      parse = opts.parsers;
    } else if (opts.parsers != null && opts.parsers[lang]) {
      parse = opts.parsers[lang];
    } else {
      parse = matter.parsers[lang];
    }
    if (typeof parse === 'function') {
      result.data = parse(data, {loose: opts.loose});
    } else {
      throw new Error(message('No parser found for the language: ' + lang));
    }
  }
  result.body = result.body.substr(bodyStart);

  return result;
}
function matter(str, opts) {

  str = formatString(str);
  opts = formatOptions(opts);
  var result = {src: str, data: null, body: str};
  if (!str) {
    return result;
  }

  var strict = !opts.loose;
  var header = opts.delims[0];
  var footer = opts.delims[1];
  var dataStart, dataEnd;
  // Front matter must start from the first byte.
  if (str.substr(0, header.length) !== header ||
      // Whether the delimiter is followed by a strange character.
      (!opts.loose && header.length < str.length &&
          str[header.length] !== '\n' &&
          str[header.length] === header[header.length-1]) ||
      // Metadata and delimiters should be separated with linefeeds.
      (dataStart = str.indexOf('\n', header.length)) < 0 ||
      (dataEnd = str.indexOf('\n' + footer, dataStart)) < 0) {
    return result;
  }
  var bodyStart = dataEnd + 1 + footer.length;
  // Whether the delimiter is followed by strange characters.
  if (strict && bodyStart < str.length) {
    if (str[bodyStart] !== '\n' && str[bodyStart] === footer[footer.length-1]) {
      return result;
    }
    while (bodyStart < str.length && str[bodyStart] !== '\n') {
      if (/^[^\s]$/.test(str[bodyStart])) {
        return result;
      }
      bodyStart++;
    }
  }  // else: Tolerate the case that a linefeed is missing: <end-delimiter><body>
  if (str[bodyStart] === '\r') { bodyStart++; }
  if (str[bodyStart] === '\n') { bodyStart++; }
  var data;
  if (dataStart < dataEnd &&
      (data = str.substr(dataStart, dataEnd - dataStart).trim())) {
    var lang = str.substr(header.length, dataStart - header.length)
                  .trim().toLowerCase() || opts.lang;
    var parse;
    if (typeof opts.parsers === 'function') {
      parse = opts.parsers;
    } else if (opts.parsers != null && opts.parsers[lang]) {
      parse = opts.parsers[lang];
    } else {
      parse = matter.parsers[lang];
    }
    if (typeof parse === 'function') {
      result.data = parse(data, {loose: opts.loose});
    } else {
      throw new Error(message('No parser found for the language: ' + lang));
    }
  }
  result.body = result.body.substr(bodyStart);

  return result;
}
function matter ( str , opts ) { str = formatString ( str ) ; opts = formatOptions ( opts ) ; var result = { src : str , data : null , body : str } ; if ( ! str ) { return result ; } var strict = ! opts . loose ; var header = opts . delims [ 0 ] ; var footer = opts . delims [ 1 ] ; var dataStart , dataEnd ; // Front matter must start from the first byte. if ( str . substr ( 0 , header . length ) !== header || // Whether the delimiter is followed by a strange character. ( ! opts . loose && header . length < str . length && str [ header . length ] !== '\n' && str [ header . length ] === header [ header . length - 1 ] ) || // Metadata and delimiters should be separated with linefeeds. ( dataStart = str . indexOf ( '\n' , header . length ) ) < 0 || ( dataEnd = str . indexOf ( '\n' + footer , dataStart ) ) < 0 ) { return result ; } var bodyStart = dataEnd + 1 + footer . length ; // Whether the delimiter is followed by strange characters. if ( strict && bodyStart < str . length ) { if ( str [ bodyStart ] !== '\n' && str [ bodyStart ] === footer [ footer . length - 1 ] ) { return result ; } while ( bodyStart < str . length && str [ bodyStart ] !== '\n' ) { if ( / ^[^\s]$ / . test ( str [ bodyStart ] ) ) { return result ; } bodyStart ++ ; } } // else: Tolerate the case that a linefeed is missing: <end-delimiter><body> if ( str [ bodyStart ] === '\r' ) { bodyStart ++ ; } if ( str [ bodyStart ] === '\n' ) { bodyStart ++ ; } var data ; if ( dataStart < dataEnd && ( data = str . substr ( dataStart , dataEnd - dataStart ) . trim ( ) ) ) { var lang = str . substr ( header . length , dataStart - header . length ) . trim ( ) . toLowerCase ( ) || opts . lang ; var parse ; if ( typeof opts . parsers === 'function' ) { parse = opts . parsers ; } else if ( opts . parsers != null && opts . parsers [ lang ] ) { parse = opts . parsers [ lang ] ; } else { parse = matter . parsers [ lang ] ; } if ( typeof parse === 'function' ) { result . data = parse ( data , { loose : opts . loose } ) ; } else { throw new Error ( message ( 'No parser found for the language: ' + lang ) ) ; } } result . body = result . body . substr ( bodyStart ) ; return result ; }
Parse the input string with options.

@param {String} str The string to parse.
@param {Object?} opts
@param {Boolean?} opts.loose Whether to tolerate ambiguous delimiters. Default: false
@param {String?} opts.lang The name of the parser to use if opts.parsers is not a function. Default: 'yaml'
@param {(String|Array)?} opts.delims Custom delimiter(s). Default: '---' or ['---', '---']
@param {(Function|Object.<String, Function>)?} opts.parsers Custom parser(s). Default: {'yaml':yaml,'toml':toml}
@return {Object} result
@param {String} result.src The original input string.
@param {String} result.body The input string without front matter.
@param {Mixed} result.data The data returned from the parsers. Default: null
Parse the input string with options .