diff --git a/Gemfile b/Gemfile index 3ba85cff..63857683 100644 --- a/Gemfile +++ b/Gemfile @@ -33,4 +33,3 @@ gem "http_parser.rb", "0.8.0", :platforms => [:jruby] # plugin dependencies gem "nokogiri", "1.18.10" -gem "addressable", "2.8.7" diff --git a/_data/plugins/auto_alert.yml b/_data/plugins/auto_alert.yml deleted file mode 100644 index daa43c46..00000000 --- a/_data/plugins/auto_alert.yml +++ /dev/null @@ -1,15 +0,0 @@ -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/alert.rb b/_plugins/alert.rb new file mode 100644 index 00000000..dcadad49 --- /dev/null +++ b/_plugins/alert.rb @@ -0,0 +1,37 @@ +module Alert + module HtmlExtension + def convert_blockquote(el, indent) + types = %w(note tip important warning caution) + first = el.children.first + return super unless first&.type == :p + return super if first.children.empty? + + target = first.children.first + return super unless target&.type == :text + + down = target.value.downcase + types.each do |type| + prefix = "[!#{type}]" + + # case A:
[!NOTE]
+ if down == prefix + el.attr["data-type"] = type + first.children.shift + break + end + + head = prefix + "\n" + next unless down.start_with?(head) + + # case B:[!NOTE]\n some text
+ el.attr["data-type"] = type + target.value = target.value[head.length..-1] || "" + break + end + + super + end + end +end + +Kramdown::Converter::Html.prepend(Alert::HtmlExtension) diff --git a/_plugins/auto-alert.rb b/_plugins/auto-alert.rb deleted file mode 100644 index bb8ce96c..00000000 --- a/_plugins/auto-alert.rb +++ /dev/null @@ -1,48 +0,0 @@ -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" - - inner_html = first_child.inner_html.downcase - - # 遍历所有 alert 类型 - alert_type.each do |type, data| - prefix = "[!#{type}]" - prefix_with_newline = "#{prefix}\n" - - # 情况一:完整匹配 [!type] 形式[!NOTE]
- if inner_html == prefix - # 将 alert 类型对应的 class 加入 blockquote - item["class"] = [item["class"], data["class_name"]].compact.join(" ") - - # 将替换为
[!NOTE]\n\n other content
- elsif inner_html.start_with? prefix_with_newline - # 将 alert 类型对应的 class 加入 blockquote - item["class"] = [item["class"], data["class_name"]].compact.join(" ") - # 在原段落前插入标题[!NOTE]\n\n other content
- first_child.add_previous_sibling "\n other content
- first_child.inner_html = first_child.inner_html[prefix_with_newline.length..-1] || "" - break - end - end - end - doc.content = fragment.to_html -end diff --git a/_sass/minimal-mistakes-plus.scss b/_sass/minimal-mistakes-plus.scss index 1d6d0b45..f33be05e 100644 --- a/_sass/minimal-mistakes-plus.scss +++ b/_sass/minimal-mistakes-plus.scss @@ -3,6 +3,26 @@ blockquote { margin-inline: 0; font-style: normal; + + &[data-type="note"] { + @extend .notice--info; + } + + &[data-type="tip"] { + @extend .notice--success; + } + + &[data-type="important"] { + @extend .notice--primary; + } + + &[data-type="warning"] { + @extend .notice--warning; + } + + &[data-type="caution"] { + @extend .notice--danger; + } } * {