|
15 | 15 | next unless first_child |
16 | 16 | next unless first_child.name == "p" |
17 | 17 |
|
18 | | - text = first_child.text.downcase |
| 18 | + inner_html = first_child.inner_html.downcase |
19 | 19 |
|
20 | 20 | # 遍历所有 alert 类型 |
21 | 21 | alert_type.each do |type, data| |
| 22 | + prefix = "[!#{type}]" |
| 23 | + prefix_with_newline = "#{prefix}\n" |
| 24 | + |
22 | 25 | # 情况一:完整匹配 [!type] 形式 <p>[!NOTE]</p> |
23 | | - if text == "[!#{type}]" |
| 26 | + if inner_html == prefix |
24 | 27 | # 将 alert 类型对应的 class 加入 blockquote |
25 | | - item['class'] = [item['class'], data["class_name"]].compact.join(" ") |
| 28 | + item["class"] = [item["class"], data["class_name"]].compact.join(" ") |
26 | 29 |
|
27 | 30 | # 将 <p> 替换为 <div> 并插入标题 |
28 | 31 | first_child.name = "div" |
29 | 32 | first_child.inner_html = "<strong>#{data["title"]}</strong>" |
30 | 33 | break |
31 | 34 |
|
32 | 35 | # 情况二:段落以 [!type]\n 开头 <p>[!NOTE]\n\n other content</p> |
33 | | - elsif text.start_with? "[!#{type}]\n" |
| 36 | + elsif inner_html.start_with? prefix_with_newline |
34 | 37 | # 将 alert 类型对应的 class 加入 blockquote |
35 | | - item['class'] = [item['class'], data["class_name"]].compact.join(" ") |
| 38 | + item["class"] = [item["class"], data["class_name"]].compact.join(" ") |
36 | 39 | # 在原段落前插入标题 <div><strong>提示</strong></div><p>[!NOTE]\n\n other content</p> |
37 | 40 | first_child.add_previous_sibling "<div><strong>#{data["title"]}</strong></div>" |
38 | 41 | # 移除段落内容开头的 [!type]\n <div><strong>提示</strong></div><p>\n other content</p> |
39 | | - first_child.content = first_child.content.sub(/\A#{Regexp.escape("[!#{type}]\n")}/i, "") |
| 42 | + first_child.inner_html = first_child.inner_html[prefix_with_newline.length..-1] || "" |
40 | 43 | break |
41 | 44 | end |
42 | 45 | end |
|
0 commit comments