Skip to content

Instantly share code, notes, and snippets.

@kisenka
Last active May 6, 2016 22:54
Show Gist options
  • Save kisenka/b7aed849f54cb335824da58ef6317848 to your computer and use it in GitHub Desktop.
Save kisenka/b7aed849f54cb335824da58ef6317848 to your computer and use it in GitHub Desktop.
Parse YAML front matter
/**
* @param {string} src Markdown source.
* @param {string} [delimiter='---'] Front matter delimiter. --- by default.
* @returns {{meta: Object, body: string}}
*/
function parseYAMLFrontMatter(src, delimiter) {
var delimiter = delimiter || '---';
var meta = null;
var body = src;
if (src.indexOf(delimiter) === 0) {
// Identify end of YAML front matter
var end = src.indexOf(delimiter, delimiter.length);
if (end !== -1) {
meta = src.substring(delimiter.length, end).trim();
body = src.substring(end + delimiter.length).trim();
}
}
return {
meta: meta,
body: body
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment