Skip to content

Commit 1492eab

Browse files
authored
feat: add reverse_sort input option for sorting functionality (#281)
* feat: add reverse_sort input option for sorting functionality - Added `reverse_sort` input to `action.yml` to allow reversing the sorting order (oldest first) when sorting is enabled. - Updated `README.md` to document the new `reverse_sort` option. - Modified `blog-post-workflow.js` to implement reverse sorting logic. - Introduced tests for reverse sorting functionality in `reverse-sort.js`. Signed-off-by: Kemal Akkoyun <kakkoyun@gmail.com> * feat: Implement sort_order parameter for flexible post sorting This commit introduces a `sort_order` parameter to control the display order of blog posts, based on the feedback. The `sort_order` parameter accepts: - `desc` (default): Sorts posts newest first when date sorting is enabled. - `asc`: Sorts posts oldest first when date sorting is enabled. If date sorting is disabled (`disable_sort: 'true'`), the `sort_order` parameter has no effect, and posts will be displayed in their fetched order. This differs slightly from the initial suggestion by ensuring `disable_sort` retains its primary role in controlling whether any sorting occurs. The implementation sorts posts directly in the specified order in a single step for efficiency. Items without publication dates are consistently placed at the end of the list. Signed-off-by: Kemal Akkoyun <kakkoyun@gmail.com> --------- Signed-off-by: Kemal Akkoyun <kakkoyun@gmail.com>
1 parent a438c19 commit 1492eab

File tree

6 files changed

+80
-26
lines changed

6 files changed

+80
-26
lines changed

README.md

Lines changed: 22 additions & 21 deletions
Large diffs are not rendered by default.

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ inputs:
2323
description: "Disables the sorting of list based on publish date"
2424
default: "false"
2525
required: false
26+
sort_order:
27+
description: "Specifies the sort order of posts when date sorting is enabled (disable_sort is 'false'). Supports 'asc' (ascending, oldest first) and 'desc' (descending, newest first). Has no effect if disable_sort is 'true'."
28+
default: "desc"
29+
required: false
2630
filter_comments:
2731
description: "Comma separated list of platforms you want to enable the comment filter"
2832
default: "stackoverflow/Comment by $author/,stackexchange/Comment by $author/"

src/blog-post-workflow.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ const TOTAL_POST_COUNT = Number.parseInt(core.getInput('max_post_count'));
3434
// Disables sort
3535
const ENABLE_SORT = core.getInput('disable_sort') === 'false';
3636

37+
// Specifies sort order
38+
const SORT_ORDER = core.getInput('sort_order'); // 'asc' or 'desc'
39+
40+
// Reverses sort order when enabled
41+
const REVERSE_SORT = core.getInput('reverse_sort') === 'true';
42+
3743
// Disables validation checks
3844
const ENABLE_VALIDATION = core.getInput('disable_item_validation') === 'false';
3945

@@ -140,7 +146,7 @@ feedList.forEach((siteUrl) => {
140146
if (ENABLE_SORT && ENABLE_VALIDATION && !item.pubDate) {
141147
reject('Cannot read response->item->pubDate');
142148
}
143-
149+
144150
// Handle missing titles gracefully
145151
if (ENABLE_VALIDATION && !item.title) {
146152
// Either skip the item by returning null
@@ -150,11 +156,11 @@ feedList.forEach((siteUrl) => {
150156
return null; // This item will be filtered out later
151157
}
152158
// Use URL as fallback or a default text
153-
item.title = item.link ?
154-
`[No Title] - ${item.link.split('/').pop() || 'Post'}` :
159+
item.title = item.link ?
160+
`[No Title] - ${item.link.split('/').pop() || 'Post'}` :
155161
'Post without title';
156162
}
157-
163+
158164
if (ENABLE_VALIDATION && !item.link) {
159165
reject('Cannot read response->item->link');
160166
}
@@ -274,9 +280,24 @@ const runWorkflow = async () => {
274280
// Sorting posts based on date
275281
if (ENABLE_SORT) {
276282
postsArray.sort(function (a, b) {
277-
return b.date - a.date;
283+
// Handle items without dates consistently by moving them to the end
284+
const aHasDate = !!a.date;
285+
const bHasDate = !!b.date;
286+
287+
if (!aHasDate && !bHasDate) return 0; // Keep original relative order for items without dates
288+
if (!aHasDate) return 1; // 'a' (no date) comes after 'b' (has date)
289+
if (!bHasDate) return -1; // 'b' (no date) comes after 'a' (has date)
290+
291+
// Both items have dates, sort according to SORT_ORDER
292+
if (SORT_ORDER === 'asc') {
293+
return a.date - b.date; // Sorts ascending by date (oldest first)
294+
} else {
295+
return b.date - a.date; // Sorts descending by date (newest first) by default
296+
}
278297
});
279298
}
299+
// If ENABLE_SORT is false (i.e., disable_sort is true), postsArray remains in fetched order.
300+
280301
// Slicing with the max count
281302
postsArray = postsArray.slice(0, TOTAL_POST_COUNT);
282303
if (postsArray.length > 0) {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Readme test
2+
Post list example:
3+
<!-- BLOG-POST-LIST:START -->
4+
- [DuckDuckGoThe search engine redefined](https://dev.to/gautamkrishnar/duckduckgo-the-search-engine-redefined-4c7d)
5+
- [Hi, I&#39;m Gautam krishna.R](https://dev.to/gautamkrishnar/hi-im-gautam-krishnar)
6+
- [An Introduction to NumPy](https://dev.to/gautamkrishnar/an-introduction-to-numpy)
7+
- [Microsoft Student PartnersGeek is the new rockstar](https://dev.to/gautamkrishnar/microsoft-student-partners--geek-is-the-new-rockstar)
8+
- [Skipping the Chrome &quot;Your connection is not private&quot; warning](https://dev.to/gautamkrishnar/quickbits-1-skipping-the-chrome-your-connection-is-not-private-warning-4kp1)
9+
- [God Mode in browsers: document.designMode = &quot;on&quot;](https://dev.to/gautamkrishnar/god-mode-in-browsers-document-designmode-on-2pmo)
10+
<!-- BLOG-POST-LIST:END -->
11+
12+
# Other contents
13+
Test content

test/sort-order-asc.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const {DEFAULT_TEST_ENV} = require('./testUtils/default-env');
2+
const {runAndCompareSnap} = require('./testUtils/testUtils');
3+
4+
describe('Ascending sort order readme (sort_order: asc)', function () {
5+
it('should be equal to the saved snapshot', async function () {
6+
const envObj = {
7+
...process.env,
8+
...DEFAULT_TEST_ENV,
9+
INPUT_SORT_ORDER: 'asc' // Changed from REVERSE_SORT: 'true'
10+
};
11+
// Snapshot name will also need to be changed to Readme.sort-order-asc.md
12+
await runAndCompareSnap('Readme.sort-order-asc.md', envObj);
13+
});
14+
});

test/testUtils/default-env.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const DEFAULT_TEST_ENV = {
22
INPUT_MAX_POST_COUNT: '10',
33
INPUT_FEED_LIST: 'http://localhost:8080',
44
INPUT_DISABLE_SORT: 'false',
5+
INPUT_SORT_ORDER: 'desc',
56
INPUT_TEMPLATE: 'default',
67
INPUT_FILTER_COMMENTS: 'medium,stackoverflow/Comment by $author/,stackexchange/Comment by $author/',
78
INPUT_USER_AGENT: 'rss-parser',

0 commit comments

Comments
 (0)