自动识别 GP / GC 标记并生成对应链接 (#436)
continuous-integration/drone/push Build is passing

This commit is contained in:
neveler
2026-04-19 07:25:51 +08:00
committed by GitHub
parent 5101a59793
commit 8b05643a2f
5 changed files with 50 additions and 28 deletions
+33 -11
View File
@@ -3,6 +3,15 @@ begin
rescue LoadError; end
module KramdownEnhancer
GITHUB_LINK_REGEX = /\b(GP-\d+|GC-[0-9a-f]{7})\b/
BLOCKQUOTE_TYPES = {
note: "notice--info",
tip: "notice--success",
important: "notice--primary",
warning: "notice--warning",
caution: "notice--danger",
}
class << self
def webp
@webp ||= {}
@@ -25,16 +34,6 @@ module KramdownEnhancer
""
end
end
def blockquote_types
@blockquote_types ||= {
:note => "notice--info",
:tip => "notice--success",
:important => "notice--primary",
:warning => "notice--warning",
:caution => "notice--danger",
}
end
end
class WebpFile < Jekyll::StaticFile
@@ -81,7 +80,7 @@ module KramdownEnhancer
return super unless first&.type == :text
text = first.value.downcase
KramdownEnhancer.blockquote_types.each do |type, class_name|
KramdownEnhancer::BLOCKQUOTE_TYPES.each do |type, class_name|
prefix = "[!#{type}]"
prefix_with_newline = "#{prefix}\n"
@@ -128,6 +127,29 @@ module KramdownEnhancer
super(el, indent)
end
def convert_text(el, indent)
return super(el, indent) unless !el.options[:github_link] && KramdownEnhancer::GITHUB_LINK_REGEX.match?(el.value)
list = el.value.split(KramdownEnhancer::GITHUB_LINK_REGEX)
el.type = :html_element
el.value = "span"
el.children = list.reject(&:empty?).map do |item|
if KramdownEnhancer::GITHUB_LINK_REGEX.match(item)
href =
if item.start_with?("GP-")
"https://github.com/HMCL-dev/HMCL/pull/#{item[3..]}"
else
"https://github.com/HMCL-dev/HMCL/commit/#{item[3..]}"
end
link = Kramdown::Element.new(:a, nil, {"href": href, "target": "_blank"})
link.children = [Kramdown::Element.new(:text, item, nil, :github_link => true)]
link
else
Kramdown::Element.new(:text, item, nil, :github_link => true)
end
end
convert_html_element(el, indent)
end
private
def relative_url(input)