index.js
function parseMatter(file, options) {
  const opts = defaults(options);
  const open = opts.delimiters[0];
  const close = '\n' + opts.delimiters[1];
  let str = file.content;

  if (opts.language) {
    file.language = opts.language;
  }

  // get the length of the opening delimiter
  const openLen = open.length;
  if (!utils.startsWith(str, open, openLen)) {
    excerpt(file, opts);
    return file;
  }

  // if the next character after the opening delimiter is
  // a character from the delimiter, then it's not a front-
  // matter delimiter
  if (str.charAt(openLen) === open.slice(-1)) {
    return file;
  }

  // strip the opening delimiter
  str = str.slice(openLen);
  const len = str.length;

  // use the language defined after first delimiter, if it exists
  const language = matter.language(str, opts);
  if (language.name) {
    file.language = language.name;
    str = str.slice(language.raw.length);
  }

  // get the index of the closing delimiter
  let closeIndex = str.indexOf(close);
  if (closeIndex === -1) {
    closeIndex = len;
  }

  // get the raw front-matter block
  file.matter = str.slice(0, closeIndex);

  const block = file.matter.replace(/^\s*#[^\n]+/gm, '').trim();
  if (block === '') {
    file.isEmpty = true;
    file.empty = file.content;
    file.data = {};
  } else {

    // create file.data by parsing the raw file.matter block
    file.data = parse(file.language, file.matter, opts);
  }

  // update file.content
  if (closeIndex === len) {
    file.content = '';
  } else {
    file.content = str.slice(closeIndex + close.length);
    if (file.content[0] === '\r') {
      file.content = file.content.slice(1);
    }
    if (file.content[0] === '\n') {
      file.content = file.content.slice(1);
    }
  }

  excerpt(file, opts);

  if (opts.sections === true || typeof opts.section === 'function') {
    sections(file, opts.section);
  }
  return file;
}
function parseMatter(file, options) {
  const opts = defaults(options);
  const open = opts.delimiters[0];
  const close = '\n' + opts.delimiters[1];
  let str = file.content;

  if (opts.language) {
    file.language = opts.language;
  }

  // get the length of the opening delimiter
  const openLen = open.length;
  if (!utils.startsWith(str, open, openLen)) {
    excerpt(file, opts);
    return file;
  }

  // if the next character after the opening delimiter is
  // a character from the delimiter, then it's not a front-
  // matter delimiter
  if (str.charAt(openLen) === open.slice(-1)) {
    return file;
  }

  // strip the opening delimiter
  str = str.slice(openLen);
  const len = str.length;

  // use the language defined after first delimiter, if it exists
  const language = matter.language(str, opts);
  if (language.name) {
    file.language = language.name;
    str = str.slice(language.raw.length);
  }

  // get the index of the closing delimiter
  let closeIndex = str.indexOf(close);
  if (closeIndex === -1) {
    closeIndex = len;
  }

  // get the raw front-matter block
  file.matter = str.slice(0, closeIndex);

  const block = file.matter.replace(/^\s*#[^\n]+/gm, '').trim();
  if (block === '') {
    file.isEmpty = true;
    file.empty = file.content;
    file.data = {};
  } else {

    // create file.data by parsing the raw file.matter block
    file.data = parse(file.language, file.matter, opts);
  }

  // update file.content
  if (closeIndex === len) {
    file.content = '';
  } else {
    file.content = str.slice(closeIndex + close.length);
    if (file.content[0] === '\r') {
      file.content = file.content.slice(1);
    }
    if (file.content[0] === '\n') {
      file.content = file.content.slice(1);
    }
  }

  excerpt(file, opts);

  if (opts.sections === true || typeof opts.section === 'function') {
    sections(file, opts.section);
  }
  return file;
}
function parseMatter ( file , options ) { const opts = defaults ( options ) ; const open = opts . delimiters [ 0 ] ; const close = '\n' + opts . delimiters [ 1 ] ; let str = file . content ; if ( opts . language ) { file . language = opts . language ; } // get the length of the opening delimiter const openLen = open . length ; if ( ! utils . startsWith ( str , open , openLen ) ) { excerpt ( file , opts ) ; return file ; } // if the next character after the opening delimiter is // a character from the delimiter, then it's not a front- // matter delimiter if ( str . charAt ( openLen ) === open . slice ( - 1 ) ) { return file ; } // strip the opening delimiter str = str . slice ( openLen ) ; const len = str . length ; // use the language defined after first delimiter, if it exists const language = matter . language ( str , opts ) ; if ( language . name ) { file . language = language . name ; str = str . slice ( language . raw . length ) ; } // get the index of the closing delimiter let closeIndex = str . indexOf ( close ) ; if ( closeIndex === - 1 ) { closeIndex = len ; } // get the raw front-matter block file . matter = str . slice ( 0 , closeIndex ) ; const block = file . matter . replace ( / ^\s*#[^\n]+ / gm , '' ) . trim ( ) ; if ( block === '' ) { file . isEmpty = true ; file . empty = file . content ; file . data = { } ; } else { // create file.data by parsing the raw file.matter block file . data = parse ( file . language , file . matter , opts ) ; } // update file.content if ( closeIndex === len ) { file . content = '' ; } else { file . content = str . slice ( closeIndex + close . length ) ; if ( file . content [ 0 ] === '\r' ) { file . content = file . content . slice ( 1 ) ; } if ( file . content [ 0 ] === '\n' ) { file . content = file . content . slice ( 1 ) ; } } excerpt ( file , opts ) ; if ( opts . sections === true || typeof opts . section === 'function' ) { sections ( file , opts . section ) ; } return file ; }
Parse front matter
Parse front matter