Skip to content

Instantly share code, notes, and snippets.

@rapsli
Last active December 21, 2015 09:29
Show Gist options
  • Save rapsli/6285584 to your computer and use it in GitHub Desktop.
Save rapsli/6285584 to your computer and use it in GitHub Desktop.
This piece of code find all pages in a given root folder and returns them sorted by date: Root structure could be as followed: contents - articles - 2013 - page1.md - page2.md - 2012 - page1.md - page2.md
env.helpers.getSortedContentFolder = function (folder, contents) {
var out = [];
// pages in root level
if (contents[folder]._.pages.length > 0 ) {
_.each(contents[folder]._.pages, function(item){
out.push(item)
})
}
function getAllFolders(root, depth) {
if (depth === 0) { // first level is built differently
if (root.length > 0) {
_.each(root, function(item){
_.each(item, function(i){
if (i != null) {
if (i.filepath === undefined) {
getAllFolders(i, ++depth);
}
else {
out.push(i)
}
}
})
})
}
}
else {
_.each(root, function(i){
if (i != null) {
if (i.filepath === undefined) {
getAllFolders(i, ++depth);
}
else {
out.push(i)
}
}
})
}
}
getAllFolders(contents[folder]._.directories, 0);
var finalOutput = _.sortBy(out, function(item) { return -item.date });
return finalOutput;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment