readme.md支持图片相对路径
This commit is contained in:
parent
c0e97409cb
commit
2346b0f848
|
@ -45,7 +45,7 @@ module RepositoriesHelper
|
|||
end
|
||||
end
|
||||
|
||||
def readme_render_decode64_content(str, owner, repo, ref)
|
||||
def readme_render_decode64_content(str, owner, repo, ref, path)
|
||||
return nil if str.blank?
|
||||
begin
|
||||
content = Base64.decode64(str).force_encoding('UTF-8')
|
||||
|
@ -53,7 +53,7 @@ module RepositoriesHelper
|
|||
c_regex = /\!\[.*?\]\((.*?)\)/
|
||||
src_regex = /src=\"(.*?)\"/
|
||||
ss = content.to_s.scan(c_regex)
|
||||
ss_src = content.to_s.scan(src_regex)
|
||||
ss_src = content.to_s.gsub("'","\"").scan(src_regex)
|
||||
total_images = ss + ss_src
|
||||
if total_images.length > 0
|
||||
total_images.each do |s|
|
||||
|
@ -63,6 +63,20 @@ module RepositoriesHelper
|
|||
if remove_title.length > 0
|
||||
r_content = r_content.gsub(/#{remove_title[0]}/, "").strip
|
||||
end
|
||||
path_last = r_content
|
||||
path_current = ""
|
||||
# 相对路径处理
|
||||
if r_content.start_with?("../")
|
||||
relative_path_length = r_content.split("../").size - 1
|
||||
path_pre = path.split("/").size - 1 - relative_path_length
|
||||
path_pre = 0 if path_pre < 0
|
||||
path_current = path.split("/")[0..path_pre].join("/")
|
||||
path_last = r_content.split("../").last
|
||||
elsif r_content.start_with?("/") # 根路径处理
|
||||
path_last = r_content[1..r_content.size]
|
||||
else
|
||||
path_current = path
|
||||
end
|
||||
# if r_content.include?("?")
|
||||
# new_r_content = r_content + "&raw=true"
|
||||
# else
|
||||
|
@ -72,9 +86,9 @@ module RepositoriesHelper
|
|||
|
||||
unless r_content.include?("http://") || r_content.include?("https://") || r_content.include?("mailto:")
|
||||
# new_r_content = "#{path}" + new_r_content
|
||||
new_r_content = [base_url, "/api/#{owner&.login}/#{repo.identifier}/raw?filepath=#{r_content}&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(/#{r_content}/, new_r_content)
|
||||
content = content.gsub(/src=\"#{r_content}\"/, "src=\"#{new_r_content}\"")
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -97,7 +111,8 @@ module RepositoriesHelper
|
|||
def decode64_content(entry, owner, repo, ref, path=nil)
|
||||
if is_readme?(entry['type'], entry['name'])
|
||||
content = Gitea::Repository::Entries::GetService.call(owner, repo.identifier, URI.escape(entry['path']), ref: ref)['content']
|
||||
readme_render_decode64_content(content, owner, repo, ref)
|
||||
path = URI.escape(entry['path']).to_s.downcase.gsub("/readme.md","")
|
||||
readme_render_decode64_content(content, owner, repo, ref, path)
|
||||
else
|
||||
file_type = File.extname(entry['name'].to_s)[1..-1]
|
||||
if image_type?(file_type)
|
||||
|
|
Loading…
Reference in New Issue