Last active
May 6, 2016 22:54
-
-
Save kisenka/b7aed849f54cb335824da58ef6317848 to your computer and use it in GitHub Desktop.
Parse YAML front matter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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