Skip to content

Commit

Permalink
Handling edge case with empty metadata - fixes jdan#55
Browse files Browse the repository at this point in the history
  • Loading branch information
jdan committed Oct 1, 2013
1 parent e8eea82 commit a0d7a37
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
5 changes: 5 additions & 0 deletions examples/empty.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
--

### Unidentified Flying Slideshow

This slideshow contains no metadata!
15 changes: 14 additions & 1 deletion lib/cleaver.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Cleaver.prototype._loadDocument = function () {
Cleaver.prototype._renderSlides = function () {
var self = this;
var slices = this._slice(self.external.loaded.document);
this.metadata = yaml.safeLoad(slices[0]);
this.metadata = yaml.safeLoad(slices[0]) || {};

for (var i = 1; i < slices.length; i++)
this.slides.push(md(slices[i]));
Expand Down Expand Up @@ -194,6 +194,19 @@ Cleaver.prototype._slice = function(document) {
var slices = [];

for (var i = 0; i < cuts.length; i++) {

/**
* EDGE CASE
* If the slideshow begins with `--`, that is, the metadata is empty and
* the user did *not* include a blank line at the top, we will insert an
* empty slide at the beginning (to represent empty metadata), followed
* by the first slice (with the -- chopped off).
*/
if (i == 0 && cuts[i].match(/^--/)) {
slices.push("");
cuts[i] = cuts[i].slice(2);
}

slices.push(cuts[i].trim());
}

Expand Down

0 comments on commit a0d7a37

Please sign in to comment.