Hexo 插件

#Hexo 2021/11/19 08:33:10

总结一下博客使用的 Hexo 插件,以及如何使用 Hexo 的 API 写一个自己的插件。

  • hexo-abbrlink 为每一个 post 生成一个链接

    # _config.yml 文件
    abbrlink:
    alg: crc32 #support crc16(default) and crc32
    rep: hex #support dec(default) and hex
    drafts: false #(true)Process draft,(false)Do not process draft. false(default)
    # Generate categories from directory-tree
    # depth: the max_depth of directory-tree you want to generate, should > 0
    auto_category:
      enable: true #true(default)
      depth: #3(default)
      over_write: false
    auto_title: false #enable auto title, it can auto fill the title by path
    auto_date: false #enable auto date, it can auto fill the date by time today
  • hexo-all-minifier HTML,JS,CSS 以及图片压缩

  • hexo-filter-nofollow 自动为文章里的链接添加 nofollow 属性

  • hexo-generator-search 为文章生成搜索索引文件,不需要索引的文章在 markdown 里添加 indexing: false

  • 自定义插件的方法 因为 Hexo 在启动时会自动加载主题目录下 scripts 文件夹的 JS 文件,因此可以在这里直接使用 Hexo 提供的 API.

    const pagination = require('hexo-pagination')
    hexo.extend.generator.register('words', (locals) => {
      const posts = locals.posts
      posts.data.sort((a, b) => b.date.unix() - a.date.unix())
      const words = posts.filter((post) => {
        const cate = post.categories.filter((cate) => cate.name === 'words')
        if (cate.length !== 0) {
          return post
        }
      })
    
      return pagination('words', words, {
        perPage: 10,
        layout: ['index'],
        data: {},
      })
    })

    上面的代码注册了指向 /words 的路由,并且在该路由下仅显示分类名称为 words 的文章,使用分页工具将每页文章数量设置为 10,页面样式套用 index 模板。


参考链接:

Hexo API 文档