Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: Sort comments by date ascending #3184

Merged
merged 1 commit into from
Oct 19, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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