【博客魔改】Hexo添加 Github alerts 支持

Wednesday, Dec 3, 2025 | 4 minute read | Updated at Wednesday, Dec 3, 2025

@

Tip

一直很喜欢 Github 上的 Alerts 书写格式,非常简洁快捷,比hexo各类主题带的外挂标签方便很多,然后发现现在用的 hexo 主题并不支持解析该格式的语法。

给个示例:

1
2
> [!NOTE]
> 这是一个注意事项提示框。这种提示框通常用于展示一般性的提示信息。

通过简单的引用加调用预设语法既可以写出好看的外挂标签的效果:

Note

这是一个注意事项提示框。这种提示框通常用于展示一般性的提示信息。

参考Hexo 美化:添加 Github alerts 支持 | Dogxi 的狗窝 的博客做了一点魔改,在此记录一下,方便自己后续潜移修改。

配置步骤

1. 更换 Markdown 渲染器

Hexo 默认的 marked渲染器不支持此语法,需要更换为 markdown-it

1
2
3
4
5
# 卸载默认渲染器
npm uninstall hexo-renderer-marked --save

# 安装 markdown-it 渲染器
npm install hexo-renderer-markdown-it --save

2. 创建解析脚本

在 Hexo 根目录创建脚本文件:scripts/github-alerts.js

 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
// GitHub Alerts 解析脚本
hexo.extend.filter.register('markdown-it:renderer', function (md) {
  md.core.ruler.after('block', 'github-alert', function (state) {
    const tokens = state.tokens
    for (let i = 0; i < tokens.length; i++) {
      if (tokens[i].type === 'blockquote_open') {
        // 找到 blockquote 的内容
        let j = i + 1
        // 只处理第一个段落
        if (
          tokens[j] &&
          tokens[j].type === 'paragraph_open' &&
          tokens[j + 1] &&
          tokens[j + 1].type === 'inline'
        ) {
          let content = tokens[j + 1].content.trim()
          // 兼容 [!NOTE]、[!NOTE]<br>、[!NOTE]\n
          const match = content.match(
            /^\[!(NOTE|WARNING|TIP|IMPORTANT|CAUTION|INFO|SUCCESS|ERROR)\][\s::-]*(.*)$/i,
          )
          if (match) {
            const alertType = match[1].toLowerCase()
            let restContent = match[2].trim()

            // 给 blockquote_open 加 class
            let className = tokens[i].attrGet('class') || ''
            className += (className ? ' ' : '') + `alert alert-${alertType}`
            tokens[i].attrSet('class', className)

            if (restContent) {
              // [!NOTE] 和内容在同一行
              tokens[j + 1].content = restContent
            } else {
              // [!NOTE] 单独一行,移除该段
              tokens.splice(j, 3)
            }
          }
        }
      }
    }
  })
})

3. 创建样式文件

source/css/github-alerts.css创建样式文件:

  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
/* GitHub Alerts 样式 */
.alert {
  position: relative;
  margin: 1.5rem 0;
  padding: 1rem 1.5rem;
  border-left: 4px solid;
  border-radius: 8px;
  background: var(--card-bg);
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
}

.alert::before {
  font-weight: 600;
  font-size: 0.95rem;
  margin-bottom: 0.5rem;
  display: block;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Lato, Roboto, "PingFang SC", "Microsoft YaHei", sans-serif;
}

.alert p {
  margin: 0.5rem 0;
  line-height: 1.6;
}

/* 各类型样式 */
.alert-note {
  border-color: #1e88e5;
  background: rgba(30, 136, 229, 0.05);
}
.alert-note::before {
  content: "💡 NOTE";
  color: #1e88e5;
}

.alert-warning {
  border-color: #ff9800;
  background: rgba(255, 152, 0, 0.08);
}
.alert-warning::before {
  content: "⚠️ WARNING";
  color: #ff9800;
}

.alert-tip {
  border-color: #00bcd4;
  background: rgba(0, 188, 212, 0.05);
}
.alert-tip::before {
  content: "💡 TIP";
  color: #00bcd4;
}

.alert-important {
  border-color: #e91e63;
  background: rgba(233, 30, 99, 0.05);
}
.alert-important::before {
  content: "❗ IMPORTANT";
  color: #e91e63;
}

.alert-caution {
  border-color: #ff5722;
  background: rgba(255, 87, 34, 0.08);
}
.alert-caution::before {
  content: "🔥 CAUTION";
  color: #ff5722;
}

.alert-info {
  border-color: #2196f3;
  background: rgba(33, 150, 243, 0.05);
}
.alert-info::before {
  content: "ℹ️ INFO";
  color: #2196f3;
}

.alert-success {
  border-color: #4caf50;
  background: rgba(76, 175, 80, 0.05);
}
.alert-success::before {
  content: "✅ SUCCESS";
  color: #4caf50;
}

.alert-error {
  border-color: #f44336;
  background: rgba(244, 67, 54, 0.08);
}
.alert-error::before {
  content: "❌ ERROR";
  color: #f44336;
}

/* 暗色模式适配 */
[data-theme="dark"] .alert {
  background: rgba(255, 255, 255, 0.05);
}

[data-theme="dark"] .alert-note {
  background: rgba(30, 136, 229, 0.1);
}

[data-theme="dark"] .alert-warning {
  background: rgba(255, 152, 0, 0.1);
}

[data-theme="dark"] .alert-tip {
  background: rgba(0, 188, 212, 0.1);
}

[data-theme="dark"] .alert-important {
  background: rgba(233, 30, 99, 0.1);
}

[data-theme="dark"] .alert-caution {
  background: rgba(255, 87, 34, 0.1);
}

[data-theme="dark"] .alert-info {
  background: rgba(33, 150, 243, 0.1);
}

[data-theme="dark"] .alert-success {
  background: rgba(76, 175, 80, 0.1);
}

[data-theme="dark"] .alert-error {
  background: rgba(244, 67, 54, 0.1);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .alert {
    margin: 1rem 0;
    padding: 0.8rem 1rem;
  }
}

4. 配置 Butterfly 主题

_config.butterfly.yml中添加 CSS 注入:

1
2
3
4
inject:
  head:
    # 其他已有的CSS注入...
    - <link rel="stylesheet" href="/css/github-alerts.css">

5. 清理缓存并测试

1
2
3
hexo clean
hexo g
hexo s

创建测试文章验证效果:

1
2
3
4
5
6
7
8
> [!NOTE]
这是一个注意事项提示框

> [!WARNING]
这是一个警告提示框

> [!TIP]
这是一个技巧提示

文件结构总结

整体修改的文件结构如下所示:

1
2
3
4
5
6
7
8
hexo-blog/
├── scripts/
│   └── github-alerts.js          # 解析脚本
├── source/
│   └── css/
│       └── github-alerts.css     # 样式文件
├── _config.butterfly.yml         # 主题配置(注入部分)
└── package.json                  # 依赖项

Caution

要注意:

  1. 必须先更换渲染器,否则脚本不会生效
  2. 脚本文件必须在 scripts/目录下
  3. CSS 路径使用 /css/而不是绝对路径
  4. 每次修改配置后需要执行 hexo clean

效果示例:

Note

这是一个注意事项提示框。这种提示框通常用于展示一般性的提示信息。

Warning

这是一个警告提示框。这种提示框通常用于警告用户注意潜在的问题。

Tip

这是一个提示框。这种提示框通常用于提供有用的小技巧和建议。

© 2021 - 2026 古月月仔的博客

🌱 Powered by Hugo with theme Dream.

关于我
  • 我是古月月仔
  • Shimizu Tou||Ethan Hu
  • 分享技术学习笔记与生活感悟杂谈
  • 现居: 上海 中国
  • 家乡: 平遥 山西
日常效率工具
  • 📝Typora — Markdown极简编辑器。
  • 📓Notion — 一站式笔记工作空间。
  • 🔗N8N — 强大的节点编排工作流工具。
  • 🤖Gemini — 好用的智能 AI 助手。
  • IamgetoUrl - 快速将图片转为URL。
  • Anyconv - 多种文件格式转换在线工具。
  • PDF24 - PDF编辑操作在线工具箱。
内容与资源工具
  • Mixamo - Adobe提供的免费角色动画库。
  • Bunlock-Music - 用于解密.ncm,.qmc类音频格式的工具。
  • EasyGIT - 一款在线GIF动图制作工具。
  • Alphacoders - 海量高清壁纸和影视/游戏原图社区。
  • 哲风壁纸 -中文壁纸分享与交流社区。
  • 爱给网 - 免费音效、配乐、视频模板等海量媒体素材。
  • 模之屋 - 专注于分享各类3D模型的社区。
我的爱好
  • 🚀 喜欢折腾各种好玩的应用技术
  • 📸 业余摄影爱好者
  • 🎮 各类游戏玩家
  • 💻 数码产品折腾爱好者
  • 📚 正在阅读:《人类简史》
  • 🎬 经典重温:《爱乐之城》