hexo给next模版归档页面年份后面增加十二生肖图标

缘起

Yang的hexo的博客模版中归档页面时发现归档页面年份后面带着十二生肖的图标,感觉不错于是就搬到自己的博客中来。本文中所有代码均来自fexo模版,感谢原作者。

起步

  1. 创建year.js,并将它保存在 /theme/next/scripts文件夹下
    内容如下:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    hexo.extend.helper.register('getAnimalIcon', function (year) {
    var index = parseInt(year) % 12;
    var icon = {
    0: 'icon-hou',
    1: 'icon-ji',
    2: 'icon-gou',
    3: 'icon-zhu',
    4: 'icon-shu',
    5: 'icon-niu',
    6: 'icon-hu',
    7: 'icon-tu',
    8: 'icon-long',
    9: 'icon-she',
    10: 'icon-ma',
    11: 'icon-yang',
    }
    return icon[index]
    });
  1. 将样式文件放到/themes/next/source/css/_custom/custom.styl中,内容如下

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    // Custom styles.
    @font-face {
    font-family: 'fontello';
    src: url("/fonts/fontello.eot?58336539");
    src: url("/fonts/fontello.eot?58336539#iefix") format("embedded-opentype"), url("/fonts/fontello.woff2?58336539") format("woff2"), url("/fonts/fontello.woff?58336539") format("woff"), url("/fonts/fontello.ttf?58336539") format("truetype"), url("/fonts/fontello.svg?58336539#fontello") format("svg");
    font-weight: normal;
    font-style: normal
    }
    [class^="icon-"]:before, [class*=" icon-"]:before {
    font-family: "fontello";
    font-style: normal;
    font-weight: normal;
    speak: none;
    display: inline-block;
    text-decoration: inherit;
    width: 1em;
    margin-right: .2em;
    text-align: center;
    font-variant: normal;
    text-transform: none;
    line-height: 1em;
    margin-left: .2em;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale
    }
    .icon-feather:before {
    content: '\e800'
    }
    .icon-cc:before {
    content: '\e802'
    }
    .icon-long:before {
    content: '\e806'
    }
    .icon-angle-left:before {
    content: '\e807'
    }
    .icon-text:before {
    content: '\e808'
    }
    .icon-hu:before {
    content: '\e809'
    }
    .icon-weibo:before {
    content: '\e80a'
    }
    .icon-angle-down:before {
    content: '\e80b'
    }
    .icon-archive:before {
    content: '\e80c'
    }
    .icon-search:before {
    content: '\e80d'
    }
    .icon-rss-2:before {
    content: '\e80e'
    }
    .icon-heart:before {
    content: '\e80f'
    }
    .icon-zhu:before {
    content: '\e810'
    }
    .icon-user-1:before {
    content: '\e811'
    }
    .icon-calendar-1:before {
    content: '\e812'
    }
    .icon-ma:before {
    content: '\e813'
    }
    .icon-box:before {
    content: '\e814'
    }
    .icon-home:before {
    content: '\e815'
    }
    .icon-shu:before {
    content: '\e816'
    }
    .icon-calendar:before {
    content: '\e817'
    }
    .icon-yang:before {
    content: '\e818'
    }
    .icon-user:before {
    content: '\e819'
    }
    .icon-info-circled-1:before {
    content: '\e81a'
    }
    .icon-lsit:before {
    content: '\e81b'
    }
    .icon-rss:before {
    content: '\e81c'
    }
    .icon-info:before {
    content: '\e81d'
    }
    .icon-wechat:before {
    content: '\e81e'
    }
    .icon-comment:before {
    content: '\e81f'
    }
    .icon-she:before {
    content: '\e820'
    }
    .icon-info-with-circle:before {
    content: '\e821'
    }
    .icon-niu:before {
    content: '\e822'
    }
    .icon-mail:before {
    content: '\e823'
    }
    .icon-list:before {
    content: '\e824'
    }
    .icon-gou:before {
    content: '\e825'
    }
    .icon-tu:before {
    content: '\e826'
    }
    .icon-twitter:before {
    content: '\e827'
    }
    .icon-location:before {
    content: '\e828'
    }
    .icon-hou:before {
    content: '\e829'
    }
    .icon-qq:before {
    content: '\e82a'
    }
    .icon-tag:before {
    content: '\e82b'
    }
    .icon-angle-right:before {
    content: '\e82c'
    }
    .icon-github:before {
    content: '\e82d'
    }
    .icon-angle-up:before {
    content: '\e82e'
    }
    .icon-ji:before {
    content: '\e82f'
    }
  2. 将字体文件放入到/themes/next/source/fonts字体文件传送门. 仅需要下载fontello开头的几个字体文件就好。

  3. 更改文件/themes/next/layout/archive.swig 第37行将内容

    1
    <h2 class="archive-year motion-element" id="archive-year-{{ year }}">{{ year }}</h2>

    改为

    1
    <h2 class="archive-year motion-element" id="archive-year-{{ year }}"><a href="{{ url_for(config.archive_dir + '/' + post.year) }}">{{ year }}</a><i class="{{ getAnimalIcon(post.year) }}"></i></h2>

效果图

so 效果就是如此了