From 077b92be1d1daa1d4909f33619d26b1111c27c66 Mon Sep 17 00:00:00 2001 From: neveler <55753029+neveler@users.noreply.github.com> Date: Sat, 6 Dec 2025 14:26:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=20GitHub=20=E9=A3=8E?= =?UTF-8?q?=E6=A0=BC=E7=9A=84=20Markdown=20=E6=8F=90=E7=A4=BA=E5=9D=97?= =?UTF-8?q?=EF=BC=88Alert=EF=BC=89=E8=AF=AD=E6=B3=95=20(#344)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _data/plugins/auto_alert.yml | 15 ++++++++++++ _plugins/auto-alert.rb | 45 ++++++++++++++++++++++++++++++++++++ index.md | 7 +----- 3 files changed, 61 insertions(+), 6 deletions(-) create mode 100644 _data/plugins/auto_alert.yml create mode 100644 _plugins/auto-alert.rb diff --git a/_data/plugins/auto_alert.yml b/_data/plugins/auto_alert.yml new file mode 100644 index 0000000..daa43c4 --- /dev/null +++ b/_data/plugins/auto_alert.yml @@ -0,0 +1,15 @@ +note: + title: 注意 + class_name: notice--info +tip: + title: 提示 + class_name: notice--success +important: + title: 重要 + class_name: notice--primary +warning: + title: 警告 + class_name: notice--warning +caution: + title: 谨慎 + class_name: notice--danger diff --git a/_plugins/auto-alert.rb b/_plugins/auto-alert.rb new file mode 100644 index 0000000..ee0d1b8 --- /dev/null +++ b/_plugins/auto-alert.rb @@ -0,0 +1,45 @@ +require "nokogiri" + +Jekyll::Hooks.register [:pages, :documents], :post_convert do |doc| + next unless doc.output_ext == ".html" + site = doc.site + next unless site.data["plugins"] + alert_type = site.data["plugins"]["auto_alert"] + next unless alert_type + + fragment = Nokogiri::HTML::DocumentFragment.parse(doc.content) + # 遍历 HTML 中的所有 blockquote 标签 + fragment.css("blockquote").each do |item| + # 找出第一个子节点,用于判断是否含有 [!type] 标记 + first_child = item.at_css("*:first-child") + next unless first_child + next unless first_child.name == "p" + + text = first_child.text.downcase + + # 遍历所有 alert 类型 + alert_type.each do |type, data| + # 情况一:完整匹配 [!type] 形式

[!NOTE]

+ if text == "[!#{type}]" + # 将 alert 类型对应的 class 加入 blockquote + item['class'] = [item['class'], data["class_name"]].compact.join(" ") + + # 将

替换为

并插入标题 + first_child.name = "div" + first_child.inner_html = "#{data["title"]}" + break + + # 情况二:段落以 [!type]\n 开头

[!NOTE]\n\n other content

+ elsif text.start_with? "[!#{type}]\n" + # 将 alert 类型对应的 class 加入 blockquote + item['class'] = [item['class'], data["class_name"]].compact.join(" ") + # 在原段落前插入标题
提示

[!NOTE]\n\n other content

+ first_child.add_previous_sibling "
#{data["title"]}
" + # 移除段落内容开头的 [!type]\n
提示

\n other content

+ first_child.content = first_child.content.sub(/\A#{Regexp.escape("[!#{type}]\n")}/i, "") + break + end + end + end + doc.content = fragment.to_html +end diff --git a/index.md b/index.md index c188575..79b0119 100644 --- a/index.md +++ b/index.md @@ -13,13 +13,8 @@ author: wifi-left > You can also submit your suggestions here. - > [!NOTE] -> -> > 如果您遇到 BUG,请及时在 [HMCL/issues](https://github.com/HMCL-dev/HMCL/issues) 发送反馈。\ > 您也可以在这里提交您的建议。 -