Jekyll x Liquid: 文章置顶功能的实现

2017-02-05 15:55:34 +08:00
 szhshp

今天突然闲的蛋疼想把一些文章置顶, 于是就研究了一下如何实现, 还顺便把功能塞到了前段时间发布的- Jekyll 主题 里面

两个方案:

Server 端实现

其实很简单, 一开始以为无法对 Post 进行操作, 后来发现可以从 post 的 header 里面进行参数设置

layout: post
title: 《 10101 》 EP0 :我太受欢迎了该怎么办
category : Comic
tags : [Comic, 10101]
stickie: true

然后 liquid 进行判断

{* for post in site.posts *}
    {* if post.stickie != true *}
        {* continue *}
    {* endif *}
    <div>置顶文章细节</div>
{* endfor *}

{* for post in site.posts *}
    {* if post.stickie == true *}
        {* continue *}
    {* endif *}
    <div>普通文章细节</div>
{* endfor *}

当然这个可以进行多种扩展, 比如只输出特定类别的文章等等,这里就是个只输出带有 comic 这个 tag 的 post

{% for post in site.posts %}

    {% assign isComic = 0 %}

    {% for tag in post.tags %}
        {% if tag == comic %}
           {% assign isComic = 1 %}
        {% endif %}
    {% endfor %}

    {% if isComic == 0 %}
      {% continue %}
    {% endif %}

{% endfor %}

Client 端实现

我太懒了没去研究 o( ̄▽ ̄)o

用 JQuery.sort()给 DOM 排序再渲染一下即可

这个排序还要额外对时间进行比较 //果然懒一点是没错的 o( ̄▽ ̄)o

演示页面

szhshp 的第三边境研究所

参考

4116 次点击
所在节点    Jekyll
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/338243

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX