Last active
December 29, 2015 01:29
-
-
Save RyanHirsch/7593712 to your computer and use it in GitHub Desktop.
An attempt at a paging implementation for use with embersmith
This file contains hidden or 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
contents | |
\posts | |
\2013 | |
\first-post.md | |
\second-post.md | |
\2014 | |
\third-post.md | |
\fourth-post.md |
This file contains hidden or 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
var _ = require('lodash'), | |
moment = require('moment'); | |
var getPost = function getPost(postArray, index) { | |
if(index < 0 || index >= postArray.length) | |
return null; | |
else | |
return postArray[index]; | |
}; | |
module.exports = function( item, currentTitle, currentDate ) { | |
var posts = []; | |
_.forEach(item, function(yearContents, year) { | |
var postsFromYear = _(item[year]).keys().remove(function(item) { | |
return "index.md" !== item; | |
}); | |
postsFromYear.forEach(function(postFileName) { | |
var postObj = item[year][postFileName]; | |
var meta = postObj.metadata; | |
var rtn = { | |
date: new moment(meta.date), | |
title: meta.title, | |
url: postObj.url | |
}; | |
posts.push(rtn); | |
}); | |
}); | |
var sortedPosts = _(posts).sortBy(function(item) { | |
return item.date.unix(); | |
}); | |
var foundIndex = sortedPosts.findIndex(function(post) { | |
var isSameTitle = post.title === currentTitle; | |
var isSameDate = (new moment(currentDate)).unix() === post.date.unix(); | |
return isSameTitle && isSameDate; | |
}).valueOf(); | |
sortedPosts = sortedPosts.valueOf(); | |
var previousPost = getPost(sortedPosts, foundIndex - 1); | |
var nextPost = getPost(sortedPosts, foundIndex + 1); | |
var rtnMarkup = []; | |
rtnMarkup.push('<ul class="pager">'); | |
if (previousPost) { | |
rtnMarkup.push('<li class="previous"><a href="' + previousPost.url + '">← ' + previousPost.title + '</a></li>'); | |
} | |
if (nextPost) { | |
rtnMarkup.push('<li class="next"><a href="' + nextPost.url + '">' + nextPost.title + ' →</a></li>'); | |
} | |
rtnMarkup.push('</ul>'); | |
return rtnMarkup.join('\n'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment