diff --git a/.github/workflows/pr-preview.yml b/.github/workflows/pr-preview.yml index dabeeb1c..afa03c72 100644 --- a/.github/workflows/pr-preview.yml +++ b/.github/workflows/pr-preview.yml @@ -12,6 +12,7 @@ concurrency: env: GITHUB_PR_NUMBER: ${{ github.event_name == 'issue_comment' && github.event.issue.number || github.event.pull_request.number }} JEKYLL_ENV: production + WEBP: enabled jobs: cache-refresh: if: ${{ !github.event.repository.fork && github.event_name == 'schedule' && github.event.schedule == '0 0 */6 * *' }} diff --git a/Gemfile b/Gemfile index 0baae100..f825a49b 100644 --- a/Gemfile +++ b/Gemfile @@ -32,5 +32,4 @@ gem "wdm", "0.2.0", :platforms => [:windows] gem "http_parser.rb", "0.8.0", :platforms => [:jruby] # plugin dependencies -gem "nokogiri", "1.18.10" -gem "addressable", "2.8.7" +gem "webp-ffi", "0.4.0" if ENV["WEBP"] == "enabled" || ENV["CI"] == "true" diff --git a/_config.yml b/_config.yml index d0fa6f76..88100539 100644 --- a/_config.yml +++ b/_config.yml @@ -87,7 +87,7 @@ whitelist: # Serving # detach: false # port: 4000 -# host: 127.0.0.1 +host: localhost baseurl: "" # show_dir_listing: false 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/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/_plugins/auto-link.rb b/_plugins/auto-link.rb deleted file mode 100644 index a2d4e867..00000000 --- a/_plugins/auto-link.rb +++ /dev/null @@ -1,30 +0,0 @@ -require "nokogiri" -require "addressable/uri" - -Jekyll::Hooks.register [:pages, :documents], :post_convert do |doc| - next unless doc.output_ext == ".html" - - site = doc.site - liquid_context = Liquid::Context.new({}, {}, { site: site }) - - process_uri = lambda do |path| - uri = Addressable::URI.parse(path) - if uri&.path - uri.path = Liquid::Template.parse("{% link #{uri.path[1..]} %}").render!(liquid_context) - end - uri.to_s - end - - fragment = Nokogiri::HTML::DocumentFragment.parse(doc.content) - fragment.css("[src^=\"/assets/\"],[src^=\"/\"][src$=\".md\"],[src^=\"/\"][src*=\".md#\"]").each do |item| - if item["src"] - item["src"] = process_uri.call(item["src"]) - end - end - fragment.css("[href^=\"/assets/\"],[href^=\"/\"][href$=\".md\"],[href^=\"/\"][href*=\".md#\"]").each do |item| - if item["href"] - item["href"] = process_uri.call(item["href"]) - end - end - doc.content = fragment.to_html -end diff --git a/_plugins/kramdown_enhancer.rb b/_plugins/kramdown_enhancer.rb new file mode 100644 index 00000000..36608b95 --- /dev/null +++ b/_plugins/kramdown_enhancer.rb @@ -0,0 +1,171 @@ +require "webp-ffi" if ENV["WEBP"] == "enabled" || ENV["CI"] == "true" + +module KramdownEnhancer + class << self + def webp + @webp ||= {} + end + + def file + @file ||= {} + end + + def baseurl + @baseurl + end + + def baseurl=(input) + @baseurl = + if input.is_a?(String) && !input.empty? + str = input.start_with?("/") ? input : "/#{input}" + str.chomp("/") + else + "" + 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 + def write(dest) + true + end + end + + module Html + def convert_a(el, indent) + if el.attr["href"].is_a?(String) && !el.options[:relative] + el.attr["href"] = relative_url(el.attr["href"]) + el.options[:relative] = true + end + + super + end + + def convert_img(el, indent) + if el.attr["src"].is_a?(String) && !el.options[:relative] + src = el.attr["src"] + el.attr["src"] = relative_url(src) + el.options[:relative] = true + + if KramdownEnhancer.webp[src] && !el.options[:webp] && !el.options[:picture] + webp_src = KramdownEnhancer.webp[src] + pic = Kramdown::Element.new(:html_element, "picture") + pic.children << Kramdown::Element.new(:html_element, "source", { "srcset" => relative_url(webp_src), "type" => "image/webp" }) + el.options[:picture] = true + el.options[:webp] = true + pic.children << el + return convert_html_element(pic, indent) + end + end + + super + end + + def convert_blockquote(el, indent) + p = el.children.first + return super if p&.type != :p || p.children.empty? + + first = p.children.first + return super unless first&.type == :text + + text = first.value.downcase + KramdownEnhancer.blockquote_types.each do |type, class_name| + prefix = "[!#{type}]" + prefix_with_newline = "#{prefix}\n" + + # case A:[!NOTE]
+ if text == prefix + el.attr["class"] = [el.attr["class"], class_name].compact.join(" ") + p.children.shift + break + # case B:[!NOTE]\n some text
+ elsif text.start_with?(prefix_with_newline) + el.attr["class"] = [el.attr["class"], class_name].compact.join(" ") + first.value = first.value[prefix_with_newline.length..-1] || "" + break + end + end + + super + end + + def convert_html_element(el, indent) + unless el.options[:relative] + if el.value == "a" && el.attr["href"].is_a?(String) + el.attr["href"] = relative_url(el.attr["href"]) + el.options[:relative] = true + elsif el.value == "img" && el.attr["src"].is_a?(String) + src = el.attr["src"] + el.attr["src"] = relative_url(el.attr["src"]) + el.options[:relative] = true + + if KramdownEnhancer.webp[src] && !el.options[:webp] && !el.options[:picture] + webp_src = KramdownEnhancer.webp[src] + pic = Kramdown::Element.new(:html_element, "picture") + pic.children << Kramdown::Element.new(:html_element, "source", { "srcset" => relative_url(webp_src), "type" => "image/webp" }) + el.options[:webp] = true + pic.children << el + return convert_html_element(pic, indent) + end + elsif el.value == "picture" + el.children.each do |child| + child.options[:picture] = true + end + end + end + super(el, indent) + end + + private + + def relative_url(input) + if input.is_a?(String) && input.start_with?("/") + input = input.start_with?("/") ? input : "/#{input}" + uri = Addressable::URI.parse(input) + if uri + if uri.path.length > 1 + file = KramdownEnhancer.file[uri.path[1..]] + uri.path = file.url if file + end + uri.path = "#{KramdownEnhancer.baseurl}#{uri.path}" + return uri.to_s + end + end + input + end + end +end + +Jekyll::Hooks.register :site, :post_read do |site| + KramdownEnhancer.baseurl = site.config["baseurl"] + webp_list = [] + webp_enabled = ENV["WEBP"] == "enabled" || ENV["CI"] == "true" + site.each_site_file do |file| + KramdownEnhancer.file[file.relative_path] = file + if file.is_a?(Jekyll::StaticFile) + url = "#{file.url}.webp" + source = "#{file.path}.webp" + destination = File.join(site.dest, url) + if File.exist?(source) + KramdownEnhancer.webp[file.url] = url + elsif webp_enabled && %w[.png .jpg .jpeg .tif .tiff].include?(file.extname.downcase) + FileUtils.mkdir_p(File.dirname(destination)) + WebP.encode(file.path, destination) + webp_list.push(KramdownEnhancer::WebpFile.new(site, site.dest, File.dirname(url), File.basename(url))) + KramdownEnhancer.webp[file.url] = url + end + end + end + site.static_files.concat(webp_list) + Kramdown::Converter::Html.prepend(KramdownEnhancer::Html) +end diff --git a/assets/img/docs/fabric_and_optifine_install/1.gif.webp b/assets/img/docs/fabric_and_optifine_install/1.gif.webp new file mode 100644 index 00000000..f1198989 Binary files /dev/null and b/assets/img/docs/fabric_and_optifine_install/1.gif.webp differ diff --git a/assets/img/docs/fabric_and_optifine_install/11.gif.webp b/assets/img/docs/fabric_and_optifine_install/11.gif.webp new file mode 100644 index 00000000..ac071d68 Binary files /dev/null and b/assets/img/docs/fabric_and_optifine_install/11.gif.webp differ diff --git a/assets/img/docs/java-download-pages/macos-arm64-1.gif.webp b/assets/img/docs/java-download-pages/macos-arm64-1.gif.webp new file mode 100644 index 00000000..72e88923 Binary files /dev/null and b/assets/img/docs/java-download-pages/macos-arm64-1.gif.webp differ diff --git a/assets/img/docs/java-download-pages/macos-x86_64-1.gif.webp b/assets/img/docs/java-download-pages/macos-x86_64-1.gif.webp new file mode 100644 index 00000000..dc1f22e7 Binary files /dev/null and b/assets/img/docs/java-download-pages/macos-x86_64-1.gif.webp differ diff --git a/assets/img/docs/java-download-pages/windows-x86-1.gif.webp b/assets/img/docs/java-download-pages/windows-x86-1.gif.webp new file mode 100644 index 00000000..e89e7b7e Binary files /dev/null and b/assets/img/docs/java-download-pages/windows-x86-1.gif.webp differ diff --git a/assets/img/docs/java-download-pages/windows-x86-2.gif.webp b/assets/img/docs/java-download-pages/windows-x86-2.gif.webp new file mode 100644 index 00000000..bba39fc7 Binary files /dev/null and b/assets/img/docs/java-download-pages/windows-x86-2.gif.webp differ diff --git a/assets/img/docs/java-download-pages/windows-x86-3.gif.webp b/assets/img/docs/java-download-pages/windows-x86-3.gif.webp new file mode 100644 index 00000000..f4b0cd17 Binary files /dev/null and b/assets/img/docs/java-download-pages/windows-x86-3.gif.webp differ diff --git a/assets/img/docs/java-download-pages/windows-x86_64-1.gif.webp b/assets/img/docs/java-download-pages/windows-x86_64-1.gif.webp new file mode 100644 index 00000000..d9918245 Binary files /dev/null and b/assets/img/docs/java-download-pages/windows-x86_64-1.gif.webp differ diff --git a/assets/img/docs/java-download-pages/windows-x86_64-2.gif.webp b/assets/img/docs/java-download-pages/windows-x86_64-2.gif.webp new file mode 100644 index 00000000..26d7e682 Binary files /dev/null and b/assets/img/docs/java-download-pages/windows-x86_64-2.gif.webp differ diff --git a/index.md b/index.md index cce2ccc1..8bd7d6ac 100644 --- a/index.md +++ b/index.md @@ -23,7 +23,7 @@ title: 新手导航 {% for group in site.data.navigation.docs -%} ## {{ group.title }} {% for item in group.children -%} -1. [{{ item.title }}]({{ item.url | relative_url }}) +1. [{{ item.title }}]({{ item.url }}) {%- if item.description %}\ {{ item.description }} {%- endif %}