Hexo-Butterfly进阶:瀑布流代码片段页面
🌟 功能简介
为了方便记录平时零碎的代码片段和小知识点,我们专门构建了一个“代码片段”页面。该功能特点包括:
- 卡片式布局:采用 CSS Grid 实现响应式瀑布流效果。
- 数据化驱动:通过 YAML 文件管理内容,无需频繁修改 HTML。
- 样式统一:延续全站的毛玻璃质感与深色模式适配。
🛠️ 配置步骤
1. 准备数据文件
在 source/_data/ 目录下创建 snippets.yml:
1 | - title: "示例片段" |
date: “2026-02-12”
1 |
|
3. 注册页面类型
修改 themes/butterfly/layout/page.pug,在 case page.type 中添加:
1 | when 'snippets' |
同时在文件顶部的 noCardLayout 数组中添加 'snippets' 以获得更自由的布局:
1 | - const noCardLayout = ['shuoshuo', 'snippets', '404'].includes(page.type) ? 'nc' : '' |
4. 添加分页逻辑 (核心更新)
为了防止片段过多导致页面加载缓慢,我们在 snippets.pug 中引入了与“即刻”类似的 client-side 分页逻辑。
在 snippets.pug 中添加以下 JavaScript 逻辑:
1 | script(type='application/json' id='snippets-data')!= safeJSON(site.data.snippets) |
5. 添加 CSS 样式
在 source/css/custom.css 中添加代码片段专属样式(包含瀑布流网格和卡片美化):
1 | .snippets-container { |
5. 创建 Markdown 页面
执行命令创建页面:hexo new page snippets
然后在 source/snippets/index.md 中设置:
1 | --- |
📝 维护说明
今后若要新增代码片段,只需在 source/_data/snippets.yml 中按照格式追加内容即可,页面会自动同步更新。
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 Yumi-an!
评论



