Skip to content

Commit

Permalink
feat: Sort comments by date ascending (#3184)
Browse files Browse the repository at this point in the history
  • Loading branch information
deadlydog committed Oct 19, 2021
1 parent 6bd64ff commit dbc9479
Showing 1 changed file with 30 additions and 12 deletions.
42 changes: 30 additions & 12 deletions _includes/comments.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,23 @@ <h4 class="page__comments-title">{{ comments_label }}</h4>
<div class="js-comments">
{% if site.data.comments[page.slug] %}
<h4 class="page__comments-title">{{ site.data.ui-text[site.locale].comments_title | default: "Comments" }}</h4>
{% assign comments = site.data.comments[page.slug] | sort %}
{% assign comments = site.data.comments[page.slug] %}

<!-- In order to sort by date we must have an array of objects, not an array of arrays, so
create a new array of plain comment objects and then sort by the comment date. -->
{% assign commentObjects = '' | split: '' %}
{% for comment in comments %}
{% assign email = comment[1].email %}
{% assign name = comment[1].name %}
{% assign url = comment[1].url %}
{% assign date = comment[1].date %}
{% assign message = comment[1].message %}
{% assign commentObject = comment[1] %}
{% assign commentObjects = commentObjects | push: commentObject %}
{% endfor %}
{% assign comments = commentObjects | sort: "date" %}

{% for comment in comments %}
{% assign email = comment.email %}
{% assign name = comment.name %}
{% assign url = comment.url %}
{% assign date = comment.date %}
{% assign message = comment.message %}
{% include comment.html index=forloop.index email=email name=name url=url date=date message=message %}
{% endfor %}
{% endif %}
Expand Down Expand Up @@ -91,14 +100,23 @@ <h4 class="page__comments-title">{{ site.data.ui-text[site.locale].comments_labe
<div class="js-comments">
{% if site.data.comments[page.slug] %}
<h4 class="page__comments-title">{{ site.data.ui-text[site.locale].comments_title | default: "Comments" }}</h4>
{% assign comments = site.data.comments[page.slug] | sort %}
{% assign comments = site.data.comments[page.slug] %}

<!-- In order to sort by date we must have an array of objects, not an array of arrays, so
create a new array of plain comment objects and then sort by the comment date. -->
{% assign commentObjects = '' | split: '' %}
{% for comment in comments %}
{% assign commentObject = comment[1] %}
{% assign commentObjects = commentObjects | push: commentObject %}
{% endfor %}
{% assign comments = commentObjects | sort: "date" %}

{% for comment in comments %}
{% assign email = comment[1].email %}
{% assign name = comment[1].name %}
{% assign url = comment[1].url %}
{% assign date = comment[1].date %}
{% assign message = comment[1].message %}
{% assign email = comment.email %}
{% assign name = comment.name %}
{% assign url = comment.url %}
{% assign date = comment.date %}
{% assign message = comment.message %}
{% include comment.html index=forloop.index email=email name=name url=url date=date message=message %}
{% endfor %}
{% endif %}
Expand Down

0 comments on commit dbc9479

Please sign in to comment.