Skip to content

Commit dbc9479

Browse files
authoredOct 19, 2021
feat: Sort comments by date ascending (mmistakes#3184)
1 parent 6bd64ff commit dbc9479

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed
 

‎_includes/comments.html

+30-12
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,23 @@ <h4 class="page__comments-title">{{ comments_label }}</h4>
1717
<div class="js-comments">
1818
{% if site.data.comments[page.slug] %}
1919
<h4 class="page__comments-title">{{ site.data.ui-text[site.locale].comments_title | default: "Comments" }}</h4>
20-
{% assign comments = site.data.comments[page.slug] | sort %}
20+
{% assign comments = site.data.comments[page.slug] %}
2121

22+
<!-- In order to sort by date we must have an array of objects, not an array of arrays, so
23+
create a new array of plain comment objects and then sort by the comment date. -->
24+
{% assign commentObjects = '' | split: '' %}
2225
{% for comment in comments %}
23-
{% assign email = comment[1].email %}
24-
{% assign name = comment[1].name %}
25-
{% assign url = comment[1].url %}
26-
{% assign date = comment[1].date %}
27-
{% assign message = comment[1].message %}
26+
{% assign commentObject = comment[1] %}
27+
{% assign commentObjects = commentObjects | push: commentObject %}
28+
{% endfor %}
29+
{% assign comments = commentObjects | sort: "date" %}
30+
31+
{% for comment in comments %}
32+
{% assign email = comment.email %}
33+
{% assign name = comment.name %}
34+
{% assign url = comment.url %}
35+
{% assign date = comment.date %}
36+
{% assign message = comment.message %}
2837
{% include comment.html index=forloop.index email=email name=name url=url date=date message=message %}
2938
{% endfor %}
3039
{% endif %}
@@ -91,14 +100,23 @@ <h4 class="page__comments-title">{{ site.data.ui-text[site.locale].comments_labe
91100
<div class="js-comments">
92101
{% if site.data.comments[page.slug] %}
93102
<h4 class="page__comments-title">{{ site.data.ui-text[site.locale].comments_title | default: "Comments" }}</h4>
94-
{% assign comments = site.data.comments[page.slug] | sort %}
103+
{% assign comments = site.data.comments[page.slug] %}
104+
105+
<!-- In order to sort by date we must have an array of objects, not an array of arrays, so
106+
create a new array of plain comment objects and then sort by the comment date. -->
107+
{% assign commentObjects = '' | split: '' %}
108+
{% for comment in comments %}
109+
{% assign commentObject = comment[1] %}
110+
{% assign commentObjects = commentObjects | push: commentObject %}
111+
{% endfor %}
112+
{% assign comments = commentObjects | sort: "date" %}
95113

96114
{% for comment in comments %}
97-
{% assign email = comment[1].email %}
98-
{% assign name = comment[1].name %}
99-
{% assign url = comment[1].url %}
100-
{% assign date = comment[1].date %}
101-
{% assign message = comment[1].message %}
115+
{% assign email = comment.email %}
116+
{% assign name = comment.name %}
117+
{% assign url = comment.url %}
118+
{% assign date = comment.date %}
119+
{% assign message = comment.message %}
102120
{% include comment.html index=forloop.index email=email name=name url=url date=date message=message %}
103121
{% endfor %}
104122
{% endif %}

0 commit comments

Comments
 (0)
Please sign in to comment.