问题背景

在维护技术博客时,需要更新侧边栏作者卡片中的”Follow Me”按钮链接。原链接需要更改为新的网址:https://cnb.cool/u/cnb.a8eeieHHHyAA

技术实现

1. 配置文件定位

Butterfly主题的社交链接配置位于主题配置文件_config.butterfly.yml中。通过分析配置文件结构,确定需要修改的具体路径为:

1
2
3
4
5
6
7
8
card_author:
enable: true
description: null
button:
enable: true
icon: fab fa-github
text: Follow Me
link: # 需要修改的目标字段

2. 修改过程

使用Python的ruamel.yaml库直接修改配置文件:

1
2
3
4
5
6
7
8
9
10
11
from ruamel.yaml import YAML
yaml = YAML()

with open('_config.butterfly.yml', 'r') as f:
config = yaml.load(f)

# 更新Follow Me链接
config['aside']['card_author']['button']['link'] = 'https://cnb.cool/u/cnb.a8eeieHHHyAA'

with open('_config.butterfly.yml', 'w') as f:
yaml.dump(config, f)

3. 技术挑战

执行过程中遇到ruamel.yaml版本兼容性问题(v0.17.21),表现为类型转换异常。通过以下方式验证修改结果:

1
cat _config.butterfly.yml | grep -A 5 "card_author:"

4. 验证结果

成功更新后的配置片段:

1
2
3
4
5
6
7
8
card_author:
enable: true
description: null
button:
enable: true
icon: fab fa-github
text: Follow Me
link: https://cnb.cool/u/cnb.a8eeieHHHyAA

经验总结

  1. 配置定位:Butterfly主题的UI元素配置通常位于aside模块下
  2. 版本管理:操作YAML文件时需注意库版本兼容性
  3. 验证方法
    • 命令行快速验证:grep -A 5 查看上下文
    • 本地启动Hexo服务预览效果:hexo s

本次修改耗时:15分钟 | 技术点:YAML文件操作 | 相关工具:ruamel.yaml