From 9c0d733626c54ee57395556fe527a21bc3d3f3c6 Mon Sep 17 00:00:00 2001 From: liuyib <1656081615@qq.com> Date: Sat, 14 Sep 2019 21:47:58 +0800 Subject: [PATCH] refactor: Optimizing the order of posts in timeline --- docs/zh-CN/advanced/advanced-setting.md | 4 ---- layout/_mixins/post-timeline.pug | 27 +++++++++++++++++++++++-- layout/archive.pug | 2 +- layout/category.pug | 2 +- layout/tag.pug | 2 +- 5 files changed, 28 insertions(+), 9 deletions(-) diff --git a/docs/zh-CN/advanced/advanced-setting.md b/docs/zh-CN/advanced/advanced-setting.md index 1fd307e..b9462f8 100644 --- a/docs/zh-CN/advanced/advanced-setting.md +++ b/docs/zh-CN/advanced/advanced-setting.md @@ -388,10 +388,6 @@ sidebar: width: 280px ``` -::: danger -该配置项的 `width` 属性在 `v1.2.0` 版本中移除。 -::: - ## 文章目录 修改主题配置文件: diff --git a/layout/_mixins/post-timeline.pug b/layout/_mixins/post-timeline.pug index d6d2b9d..c2313f6 100644 --- a/layout/_mixins/post-timeline.pug +++ b/layout/_mixins/post-timeline.pug @@ -1,7 +1,31 @@ mixin postTimeline(posts) section.post-timeline - var year, tmpYear - - posts.each(function (post) { + - var sortPosts = [] + + //- Sort posts by date in descending order. + each post in posts + if (sortPosts.length === 0) + - sortPosts.push(post) + else + each _post, index in sortPosts + - var isBigTime = new Date(post.date).getTime() > new Date(_post.date).getTime(); + if (post.top) + if (!_post.top || (new Date(post.date).getTime() > new Date(_post.date).getTime())) + - sortPosts.splice(index, 0, post) + - break + else if (index + 1 === sortPosts.length) + - sortPosts.push(post) + - break + else + if (!_post.top && (new Date(post.date).getTime() > new Date(_post.date).getTime())) + - sortPosts.splice(index, 0, post) + - break + else if (index + 1 === sortPosts.length) + - sortPosts.push(post) + - break + + each post in sortPosts if post.date - tmpYear = full_date(post.date, "YYYY") @@ -20,4 +44,3 @@ mixin postTimeline(posts) if post.top include ../_partials/widgets/sticky-top.pug - - }) diff --git a/layout/archive.pug b/layout/archive.pug index 53a0909..bde2fc7 100644 --- a/layout/archive.pug +++ b/layout/archive.pug @@ -6,7 +6,7 @@ block content div.archive div.archive-total= site.posts.length === 0 ? __("page.posts-empty") : _p("page.posts-total", site.posts.length) - +postTimeline(theme.post_list.paginate.archives ? page.posts : site.posts) + +postTimeline(theme.post_list.paginate.archives ? page.posts.data : site.posts.data) if theme.post_list.paginate.archives include ./_partials/widgets/pagination.pug diff --git a/layout/category.pug b/layout/category.pug index 806e62c..f7ccfb1 100644 --- a/layout/category.pug +++ b/layout/category.pug @@ -8,6 +8,6 @@ block content span= __("page.categories") + " -- " span= page.category - +postTimeline(page.posts) + +postTimeline(page.posts.data) include ./_partials/widgets/pagination.pug diff --git a/layout/tag.pug b/layout/tag.pug index 3639934..d5b3d96 100644 --- a/layout/tag.pug +++ b/layout/tag.pug @@ -8,6 +8,6 @@ block content span= __("page.tags") + " -- " span= page.tag - +postTimeline(page.posts) + +postTimeline(page.posts.data) include ./_partials/widgets/pagination.pug -- GitLab