Skip to content

Instantly share code, notes, and snippets.

@kennethormandy
Last active December 28, 2015 22:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kennethormandy/7574720 to your computer and use it in GitHub Desktop.
Save kennethormandy/7574720 to your computer and use it in GitHub Desktop.
hb-snippet-sort-by-date
{
"go-out-and-make-it-your-own": {
"title": "Go Out & Make It Your Own",
"date": "2013-08-19"
},
"an-out-of-order-article": {
"title": "An out-of-order article",
"date": "2014-11-01"
},
"welcome-to-harp": {
"title": "Welcome to Harp",
"date": "2013-09-01"
},
"aaa-welcome-to-harp": {
"title": "AAA Welcome to Harp",
"date": "1999-09-01"
}
}
//- To keep the _data.json the same,
make the slug a property of the
object in a new array instead.
articles = []
for article, slug in public.articles._data
if article.date
- article.slug = slug
- articles.push(public.articles._data[slug])
//- Now iterate over the articles,
performing the desired operations.
In this case, sorting by date from newest
to olders, and stopping at the previously
defined post postlimit.
ul
for article in articles.sort(function(a,b){ a = new Date(a.date); b = new Date(b.date); return b<a?-1:b>a?1:0; }).slice(0, postlimit)
li
a(href="/articles/#{ article.slug }")
time.post-meta-date.post-date(datetime="#{ new Date(article.date }")= article.date
h2.post-item-title= article.title
//- If there are less posts than the postlimit,
show a link to wherever all of them
are show. In this case, the articles page.
if postlimit < articles.length
a(href="/articles") View all articles
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment