Hexo与安知鱼主题教程

Hexo

实现RSS订阅

安装RSS插件

1
npm install hexo-generator-feed --save

安装完成后,插件会自动生成 atom.xml 或 rss.xml 文件。

配置RSS插件

在 Hexo 的配置文件 _config.yml 中,添加以下配置来启用 RSS 插件:

1
2
3
4
5
6
7
8
9
10
11
# RSS 插件配置
feed:
type: atom # 或者 rss2
path: atom.xml # 输出文件名(建议使用 atom.xml 或 rss.xml)
limit: 20 # 最大文章数量(可选)
hub: # PubSubHubbub hub 地址(可选)
content: true # 是否包含文章全文(可选)
content_limit: 140 # 包含摘要时的字数限制(可选)
content_limit_delim: ' ' # 分隔符(可选)
order_by: -date # 按发布时间排序(可选)
icon: icon.png # RSS 图标路径(可选)

触发RSS文章更新

手动添加updated字段

在Front Matter添加字段

1
updated: 2023-01-02 # 更新时修改此处时间
通过脚本实现updated字段更新
  • 在博客根目录下新建文件夹scripts

  • 新建脚本文件auto-update-frontmatters.js

  • 添加gray-matter依赖

    1
    npm install gray-matter --save
  • 添加以下代码

    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
    const fs = require('fs');
    const path = require('path');
    const matter = require('gray-matter');
    const crypto = require('crypto');

    // 仅计算正文内容的哈希(自动排除 Front Matter)
    function computeHash(content) {
    return crypto.createHash('md5').update(content).digest('hex');
    }

    hexo.extend.filter.register('before_post_render', function(data) {
    const filePath = data.full_source;
    if (!filePath || path.extname(filePath) !== '.md') return data;

    try {
    const content = fs.readFileSync(filePath, 'utf8');
    const stats = fs.statSync(filePath);
    const frontmatter = matter(content);
    const fileMtime = new Date(stats.mtime);

    // 🔥 关键修改:直接用正文内容计算哈希(忽略整个 Front Matter)
    const bodyContent = frontmatter.content.trim(); // 去除前后空白
    const currentHash = computeHash(bodyContent);

    // 比对已有哈希(存储在文件中的历史哈希)
    let needUpdate = true;
    if (frontmatter.data.updated) {
    // 检查是否存在记录的上次哈希(需自行维护)
    const lastHash = frontmatter.data.contentHash || '';
    // 哈希一致且未手动干预过时间 → 跳过
    if (lastHash === currentHash) {
    needUpdate = false;
    }
    }

    if (needUpdate) {
    // 更新时间并记录当前哈希
    frontmatter.data.updated = fileMtime.toISOString();
    frontmatter.data.contentHash = currentHash; // 可选:保存哈希供下次比较
    const updatedContent = matter.stringify(frontmatter);
    fs.writeFileSync(filePath, updatedContent, 'utf8');
    console.log(`内容变化,已更新: ${filePath}`);
    } else {
    console.log(`内容未变化,跳过: ${filePath}`);
    }

    } catch (err) {
    console.error('处理失败:', filePath, err);
    }

    return data;
    });

常用命令

  • 创建新文章

    1
    hexo new "My New Post"
  • 本地预览

    1
    hexo cl; hexo g; hexo s
  • 推送更新

    1
    hexo cl; hexo g; hexo d

anzhiyu变量说明

_config.anzhiyu.yml变量

变量名 含义 用法
favicon 网站图标 favicon: 图标地址
post_copyright.location 文章标题下方地点位置 location: 地点名
post_copyright.license 应用于网站上文本/内容的许可协议 license: CC BY-NC-SA 4.0
reward.QR_code 文章底部打赏二维码 img: 二维码地址
footer.owner.since 页脚部分网站创建年份 since: 2025
footer.bdageitem.list 网页底部徽标 yml代码下方注释

文章

添加标签

在文章的Front Matter部分,添加或修改tags字段。

  • 单标签

    1
    2
    tags:
    - 标签1
  • 多标签

    1
    tags: [标签1, 标签2]

添加分类

在文章的Front Matter部分,添加或修改categories字段。

1
2
3
4
---
categories:
- 分类1
---

转载他人文章,添加转载标记

在文章的Front Matter部分,添加copyright_author字段。

1
2
3
---
copyright_author: "原作者姓名"
---

注意

原作者姓名将替代文章末尾你名字的位置但其余不变。