fix: readme image regex error rescued

This commit is contained in:
yystopf 2022-03-28 11:25:13 +08:00
parent 4c69c4d20f
commit a5be4a9502
1 changed files with 34 additions and 30 deletions

View File

@ -59,38 +59,42 @@ module RepositoriesHelper
total_images = ss + ss_src + ss_src2 total_images = ss + ss_src + ss_src2
if total_images.length > 0 if total_images.length > 0
total_images.each do |s| total_images.each do |s|
image_title = /\"(.*?)\"/ begin
r_content = s[0] image_title = /\"(.*?)\"/
remove_title = r_content.to_s.scan(image_title) r_content = s[0]
# if remove_title.length > 0 remove_title = r_content.to_s.scan(image_title)
# r_content = r_content.gsub(/#{remove_title[0]}/, "").strip # if remove_title.length > 0
# end # r_content = r_content.gsub(/#{remove_title[0]}/, "").strip
path_last = r_content # end
path_current = "" path_last = r_content
# 相对路径处理 path_current = ""
if r_content.start_with?("../") # 相对路径处理
relative_path_length = r_content.split("../").size - 1 if r_content.start_with?("../")
path_pre = path.split("/").size - 1 - relative_path_length relative_path_length = r_content.split("../").size - 1
path_pre = 0 if path_pre < 0 path_pre = path.split("/").size - 1 - relative_path_length
path_current = path_pre == 0 ? "" : path.split("/")[0..path_pre].join("/") path_pre = 0 if path_pre < 0
path_last = r_content.split("../").last path_current = path_pre == 0 ? "" : path.split("/")[0..path_pre].join("/")
elsif r_content.start_with?("/") # 根路径处理 path_last = r_content.split("../").last
path_last = r_content[1..r_content.size] elsif r_content.start_with?("/") # 根路径处理
else path_last = r_content[1..r_content.size]
path_current = path else
end path_current = path
# if r_content.include?("?") end
# new_r_content = r_content + "&raw=true" # if r_content.include?("?")
# else # new_r_content = r_content + "&raw=true"
# new_r_content = r_content + "?raw=true" # else
# end # new_r_content = r_content + "?raw=true"
new_r_content = r_content # end
new_r_content = r_content
unless r_content.include?("http://") || r_content.include?("https://") || r_content.include?("mailto:") unless r_content.include?("http://") || r_content.include?("https://") || r_content.include?("mailto:")
# new_r_content = "#{path}" + new_r_content # new_r_content = "#{path}" + new_r_content
new_r_content = [base_url, "/api/#{owner&.login}/#{repo.identifier}/raw?filepath=#{path_current}/#{path_last}&ref=#{ref}"].join new_r_content = [base_url, "/api/#{owner&.login}/#{repo.identifier}/raw?filepath=#{path_current}/#{path_last}&ref=#{ref}"].join
end
content = content.gsub(/src=\"#{r_content}\"/, "src=\"#{new_r_content}\"").gsub(/src='#{r_content}'/, "src=\"#{new_r_content}\"")
rescue
next
end end
content = content.gsub(/src=\"#{r_content}\"/, "src=\"#{new_r_content}\"").gsub(/src='#{r_content}'/, "src=\"#{new_r_content}\"")
end end
end end