all files / lib/parse/ front-matter.js

100% Statements 20/20
75% Branches 15/20
100% Functions 3/3
100% Lines 8/8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30                          48×                    
/**
 * Interface to gray-matter for parsing files with YAML frontmatter.
 */
import matter from 'gray-matter';
import jsYaml from 'js-yaml';
 
const frontMatterOptions = {
  parser: jsYaml.safeLoad
};
 
/**
 * Parse a file with front matter.
 * @param {string} str String to parse.
 * @param {Object} options Additional options.
 * @return {JSON} JSON object.
 */
export function parse(str = '', options = {}) {
  return matter(str, Object.assign({}, frontMatterOptions, options));
}
 
/**
 * Stringify a document.
 * @param  {string} str Content to append to YAML.
 * @param  {Object} data Data to convert to YAML and prepend to document.
 * @return {string} Content with prepended YAML data.
 */
export function stringify(str = '', data = {}) {
  return matter.stringify(str, data);
}