Perhaps the funnest part about this blog was writing the commenting system. One may want to comment on a comment. I found a way to support this behaviour with recursion.
In the layout, using Jade mixins, we have something like
mixin commentView(comment)
- var children = comments.filter(function(child) { return comment.id === child.commentId; })
.wmd-preview!= comment.bodyHtml
.children
.inner-children
each child in children
+commentView(child)
We need the inner-children
div to create the smooth transitions when you expand the sub-comments. Apparently, CSS transitions only work when the height is specified, so you can calculate the new height of children
with inner-children
.
We can also expand comments recursively with JavaScript:
function expandParent(node) {
if (node.classList.contains('comments')) return; // we've reached the top level
if (node.classList.contains('children')) {
node.parentNode.querySelector('.reply-expander').classList.add('expanded');
node.classList.add('expanded');
}
expandParent(node.parentNode);
}
I've left some sample comments below to show off the features. Apparently, transitions are only smooth in Chrome and Internet Explorer. Leave a comment below and try it out!
New Comment
Comments
It gets a little lonely when no one comments on your post.
Aww, don't be sad Phil. I'll be your friend.
Thanks, Lawrence. You're my best friend!
No problem, Phil!
I'll give you a hug, Phil.
Aren't I really cute? Someone say yes. Please.
Hi Phil. Nice work! I'm making my own blog now and your blog is absolutely inspirational! :)
Hi Sujin! Great to hear from you. Thanks. It was definitely quite a bit of work. Keep me updated on how it goes. Maybe we can compare notes.