Skip to content

Commit b5ddf31

Browse files
authored
重构 alert 插件
1 parent 391d12c commit b5ddf31

4 files changed

Lines changed: 40 additions & 64 deletions

File tree

Gemfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,3 @@ gem "http_parser.rb", "0.8.0", :platforms => [:jruby]
3333

3434
# plugin dependencies
3535
gem "nokogiri", "1.18.10"
36-
gem "addressable", "2.8.7"

_data/plugins/auto_alert.yml

Lines changed: 0 additions & 15 deletions
This file was deleted.

_plugins/alert.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
require "kramdown/converter/html"
2+
require "kramdown/element"
3+
4+
module Alert
5+
module HtmlExtension
6+
def convert_blockquote(el, indent)
7+
types = %w(note tip important warning caution)
8+
first = el.children.first
9+
return super unless first&.type == :p
10+
return super if first.children.empty?
11+
12+
target = first.children.first
13+
return super unless target&.type == :text
14+
15+
down = target.value.downcase
16+
types.each do |type|
17+
prefix = "[!#{type}]"
18+
19+
# case A: <p>[!NOTE]</p>
20+
if down == prefix
21+
el.attr["data-type"] = type
22+
first.children.shift
23+
break
24+
end
25+
26+
head = prefix + "\n"
27+
next unless down.start_with?(head)
28+
29+
# case B: <p>[!NOTE]\n some text</p>
30+
el.attr["data-type"] = type
31+
target.value = target.value[head.length..-1] || ""
32+
break
33+
end
34+
35+
super
36+
end
37+
end
38+
end
39+
40+
Kramdown::Converter::Html.prepend(Alerts::HtmlExtension)

_plugins/auto-alert.rb

Lines changed: 0 additions & 48 deletions
This file was deleted.

0 commit comments

Comments
 (0)